My plan:
1. Calculate mouse direction [x, y] [success]
I my Mouse Move event:
int directionX = lastPosition.x - position.x;
int directionY = lastPosition.y - position.y;
2. Calculate angles [theta, phi] [success]
float theta = fmod(lastTheta + sensibility * directionY, M_PI);
float phi = fmod(lastPhi + sensibility * directionX * -1, M_PI * 2);
Edit {
bug fix:
float theta = lastTheta + sensibility * directionY * -1;
if (theta < M_PI / -2)theta = M_PI / -2;
else if (theta > M_PI / 2)theta = M_PI / 2;
float phi = fmod(lastPhi + sensibility * directionX * -1, M_PI * 2);
}
Now I have given theta, phi, the centerpoint and the radius and I want to calculate the position and the rotation [that the camera look at the centerpoint]
3. Calculate position coordinates [X,Y,Z] [failed]
float newX = radius * sin(phi) * cos(theta);
float newY = radius * sin(phi) * sin(theta);
float newZ = radius * cos(phi);
Solution [by meowgoesthedog]:
float newX = radius * cos(theta) * cos(phi);
float newY = radius * sin(theta);
float newZ = radius * cos(theta) * sin(phi);
4. Calculate rotation [failed]
float pitch = ?;
float yaw = ?;
Solution [by meowgoesthedog]:
float pitch = -theta;
float yaw = -phi;
Thanks for your solutions!
Your attempt was almost (kinda) correct:
As the diagram shows, in OpenGL the "vertical" direction is conventionally taken to be Y, whereas your formulas assume it is Z
phi and theta are in the wrong order
Very simple conversion: yaw = -phi, pitch = -theta (from the perspective of the camera)
Fixed formulas:
float position_X = radius * cos(theta) * cos(phi);
float position_Y = radius * sin(theta);
float position_Z = radius * cos(theta) * sin(phi);
(There may also be some sign issues with the mouse deltas but they should be easy to fix.)
Related
I've been working on a project for some time and Needed something that could from a Vector3 representing rotation in the XYZ axis make Forward, Right and Up vectors. I was looking through a lot of stuff and after some time I figured out I had to implement Quaternions (I have my own Math Libary but this same thing happened with glm) and Here is the code for calculating the Forward Vector: (quaternion is the rotation Quaternion member in my class and Quaternion::Euler is a static function that returns a Quaternion from Euler Angles)
quaternion = Quaternion::Euler(rotation);
Vector3 ret = quaternion * Vector3(0.0f, 0.0f, 1.0f);
when the rotation is 0, 0, 0 the function returns 0, 0, 1 as it should, but if I try something like 0, 180, 0 it should return 0, 0, -1, but instead I get -8.74228e-08, 0, -1. After some investigation I figured out that the Quaternion::Euler function returns a Quaternion where the w part is messed up. In the case where the rotation is 0, 180, 0 the Quaternion the Quaternion::Euler function returns is 0, 1, 0, -4.37113883e-08 which is almost exactly half of the random number the Forward functions returns. Here is Quaternion::Euler:
float x = Radians(euler.x);
float y = Radians(euler.y);
float z = Radians(euler.z);
x = x / 2;
y = y / 2;
z = z / 2;
return Quaternion(cos(z) * cos(y) * sin(x) - sin(z) * sin(y) * cos(x), //X
cos(z) * sin(y) * cos(x) + sin(z) * cos(y) * sin(x), //Y
sin(z) * cos(y) * cos(x) - cos(z) * sin(y) * sin(x), //Z
cos(z) * cos(y) * cos(x) + sin(z) * sin(y) * sin(x));//W
and Honestly, I stole this function from an article of a guy that was making his own Math Engine, in his case this seemed to work. Here is the Quaternion Vector Multiplication function, that I "borrowed" from the Unity Implementation: (in this case it's inside the Quaternion struct so this is a pointer to the quaternion from the Quaternion Vector multiplication)
inline Vector3 operator*(const Vector3& other) {
float x = this->x * 2.0f;
float y = this->y * 2.0f;
float z = this->z * 2.0f;
float xx = this->x * x;
float yy = this->y * y;
float zz = this->z * z;
float xy = this->x * y;
float xz = this->x * z;
float yz = this->y * z;
float wx = this->w * x;
float wy = this->w * y;
float wz = this->w * z;
Vector3 ret;
ret.x = (1.0f - (yy + zz)) * other.x + (xy - wz) * other.y + (xz + wy) * other.z;
ret.y = (xy + wz) * other.x + (1.0f - (xx + zz)) * other.y + (yz - wx) * other.z;
ret.z = (xz - wy) * other.x + (yz + wx) * other.y + (1.0f - (xx + yy)) * other.z;
return ret;
}
Does anyone know what might be wrong ? I tried to do this with glm:
glm::quat quat(glm::vec3(glm::radians(rotation.x), glm::radians(rotation.y), glm::radians(rotation.z)));
glm::vec3 v = quat * glm::vec3(0.0f, 0.0f, 1.0f);
but it's the same thing, the vector is the same and the quaternion is the same too, I've been reading into things a lot about this and couldn't find a fix, always when I tried to search implementation for the Quaternio::Euler function it just came up with how to use a math library. It would be best if the solution wouldn't require me to use glm, because I have to use my own Math Library, but honestly I will try anything to at least understand what is wrong.
We are trying to convert from a local space incremental rotation in Euler (X,Y,Z) = Pitch Yaw Roll to an absolute world space rotation Quaternion.
We are doing this by converting each incremental rotation to a quaternion and accumulating (through multiplication) Quaternion rotations to give a world space quaternion.
However our result rotations show the object rotating around world space axes rather than local object axes.
I am following this pseudocode loop and c++, and have built the same in Unity3D (which works correctly since the Quaternion operations are aready provided).
Does anyone have any pointers as to what is going wrong here?
Quaternion qacc;
Quaternion q1;
loop
{
quacc = quacc * q1.degreeToQuaternion(xRot, yRot, zRot);
}
...
void Quaternion::degreeToQuaternion( double yaw, double pitch, double roll) // yaw (Z), pitch (Y), roll (X)
{
yaw = yaw * M_PI / 180.;
pitch = pitch * M_PI / 180.;
roll = roll * M_PI / 180.;
radianToQuaternion(yaw, pitch, roll);
}
void Quaternion::radianToQuaternion( double yaw, double pitch, double roll) // yaw (Z), pitch (Y), roll (X)
{
double cy = cos(yaw * 0.5);
double sy = sin(yaw * 0.5);
double cp = cos(pitch * 0.5);
double sp = sin(pitch * 0.5);
double cr = cos(roll * 0.5);
double sr = sin(roll * 0.5);
this->w = cy * cp * cr + sy * sp * sr;
this->x = cy * cp * sr - sy * sp * cr;
this->y = sy * cp * sr + cy * sp * cr;
this->z = sy * cp * cr - cy * sp * sr;
}
Quaternion operator * (Quaternion q0, Quaternion q1)
{
Quaternion q2;
double mag;
q2.w = q0.w * q1.w - q0.x * q1.x - q0.y * q1.y - q0.z * q1.z;
q2.x = q0.w * q1.x + q0.x * q1.w + q0.y * q1.z - q0.z * q1.y;
q2.y = q0.w * q1.y - q0.x * q1.z + q0.y * q1.w + q0.z * q1.x;
q2.z = q0.w * q1.z + q0.x * q1.y - q0.y * q1.x + q0.z * q1.w;
mag = sqrt (q2.w * q2.w + q2.x * q2.x + q2.y * q2.y + q2.z * q2.z);
q2.w /= mag;
q2.x /= mag;
q2.y /= mag;
q2.z /= mag;
return q2;
}
I am attempting to draw a sphere from scratch using OpenGL. The function must be defined as void drawSphere(float radius, int nSegments, int nSlices), must be centred at the (0, 0, 0) origin and must be created using GL_QUADS.
Firstly, are the "slices" the sort of tapered cylinder shapes that are stacked on top of each other to create the sphere, and the "segments" are the quads that are generated in a circle to generate the wall/side of each of these tapered cylinder slices?
Secondly, I cannot seem to find any algorithms or examples of how to make the calculations to generate this sphere using quadrilaterals - most example seem to be generated from triangles instead.
EDIT
Here is what I have just tried, which is definitely in the right direction, but my coordinate calculations are off somewhere:
void drawSphere(float radius, int nSegments, int nSlices) {
/*
* TODO
* Draw sphere centered at the origin using GL_QUADS
* Compute and set normal vectors for each vertex to ensure proper shading
* Set texture coordinates
*/
for (float slice = 0.0; slice < nSlices; slice += 1.0) {
float lat0 = M_PI * (((slice - 1) / nSlices) - 0.5);
float z0 = sin(lat0);
float zr0 = cos(lat0);
float lat1 = M_PI * ((slice / nSlices) - 0.5);
float z1 = sin(lat1);
float zr1 = cos(lat1);
glBegin(GL_QUADS);
for (float segment = 0.0; segment < nSegments; segment += 1.0) {
float long0 = 2 * M_PI * ((segment -1 ) / nSegments);
float x0 = cos(long0);
float y0 = sin(long0);
float long1 = 2 * M_PI * (segment / nSegments);
float x1 = cos(long1);
float y1 = sin(long1);
glVertex3f(x0 * zr0, y0 * zr0, z0);
glVertex3f(x1 * zr1, y1 * zr1, z0);
glVertex3f(x0 * zr0, y0 * zr0, z1);
glVertex3f(x1 * zr1, y1 * zr1, z1);
}
glEnd();
}
}
I'm not seeing radius being used. Probably a simple omission. Let's assume that for the rest of your computation the radius is 1. You should generate your 4 values by using 0-1 on both (x, y), and (z, zr), but not mix within those tuples.
So x1 * zr1, y1 * zr1, z0 is not right because you're mixing zr1 and z0. You can see that the norm of this vector is not 1 anymore. Your 4 values should be
x0 * zr0, y0 * zr0, z0
x1 * zr0, y1 * zr0, z0
x0 * zr1, y0 * zr1, z1
x1 * zr1, y1 * zr1, z1
I'm not too sure about the order since I don't use Quads but triangles.
I am given a Line PQ and a boundary. I have to find two parallel lines to the given line but lines should intersect the boundary. Also i know the distance between the parallel lines to the given line. I need to find the P'Q' and P"Q".
Please any one give a simple solution.
float vx = x2 - x1;
float vy = y2 - y1;
float mag = sqrt(vx * vx + vy * vy);
float t = (mag / 2.0) / mag;
float px = (1 - t) * x1 + t * x2;
float py = (1 - t) * y1 + t * y2;
I just found the centre point of PQ by the above code. Further i planned to draw a perpendicular line thru (px, py) with the known distance, then drawing lines perpendicular to that new line(those lines will be parallel to PQ), thru the end points of that new line. But i could not achieve it. can anyone help me or suggest me a way who know maths
Finally i got the solution.
The steps are.
First i am getting the center point of PQ.
POINT find_a_point_in_distance(float x1, float y1, float x2, float y2, float len = 0) {
float vx = x2 - x1;
float vy = y2 - y1;
float mag = sqrt(vx * vx + vy * vy);
float t = len == 0 ? ((mag / 2.0) / mag) : (len / mag);
float px = (1 - t) * x1 + t * x2;
float py = (1 - t) * y1 + t * y2;
POINT res = { px, py };
return res;
}
here (px, py) is center point of PQ.
Then i am finding the perpendicular line through (px, py).
Already i have mentioned in the question that i know distance between PQ and P'Q' also PQ and P"Q". So i am getting two points in that perpendicular line using that distance. Finally i know the angle of the line PQ, so P'Q' and P"Q" should be in that same angle, using these details i can get the lines P'Q' and P"Q" what ever length i want. Here in below code i am getting the line P'Q' and P"Q" with the length of the diagonal of the rectangular box.
POINT res = find_a_point_in_distance(x1, y1, x2, y2);
POINT res2 = find_a_point_in_distance(res.x, res.y, x2, y2, halflen);
float cosA = acos((res2.x - res.x) / halflen) * 180 / PI;
float sinA = asin((res2.y - res.y) / halflen) * 180 / PI;
float cosAngle = cos((cosA + 90.0) * PI / 180.0);
float sinAngle = sin((sinA + 90.0) * PI / 180.0);
float cx1 = res.x + halflen * cosAngle;
float cy1 = res.y + halflen * sinAngle;
float cosAngle2 = cos((cosA - 90.0) * PI / 180.0);
float sinAngle2 = sin((sinA - 90.0) * PI / 180.0);
float cx2 = res.x + halflen * cosAngle2;
float cy2 = res.y + halflen * sinAngle2;
float diagonal = sqrt(width * width + height * height);
float halfdiagonal = diagonal / 2.0;
float cosAngleT = cos(cosA * PI / 180.0);
float sinAngleT = sin(sinA * PI / 180.0);
float cosAngleTD = cos((cosA + 180) * PI / 180.0);
float sinAngleTD = sin((sinA + 180) * PI / 180.0);
float cx10 = cx1 + halfdiagonal * cosAngleT;
float cy10 = cy1 + halfdiagonal * sinAngleT;
float cx11 = cx1 + halfdiagonal * cosAngleTD;
float cy11 = cy1 + halfdiagonal * sinAngleTD;
float cx20 = cx2 + halfdiagonal * cosAngleT;
float cy20 = cy2 + halfdiagonal * sinAngleT;
float cx21 = cx2 + halfdiagonal * cosAngleTD;
float cy21 = cy2 + halfdiagonal * sinAngleTD;
here (cx10, cy10) and (cx11, cy11) is line P'Q' and
(cx20, cy20) and (cx21, cy21) is line P"Q".
then finally im finding the intersect point of P'Q' and P"Q" with all sides of rectangle
The problem is when I face my camera down the z axis for example and pitch this works fine however, after I have finished the pitch and would like to yaw on this new axis it begins to roll for some unknown reason =s.
void FrustumCamera::xAxisRotation(float angle)
{
// angle = angle * (double)degToRad;
Vector3<float> x = m_orientation.getXAxis();
Vector3<float> y = m_orientation.getYAxis();
Vector3<float> z = m_orientation.getZAxis();
y.rotateAroundAxis(x,angle);
x = m_orientation.getXAxis();
z.rotateAroundAxis(x,angle);
m_orientation.setYAxis(y);
m_orientation.setZAxis(z);
}
void FrustumCamera::yAxisRotation(float angle)
{
// angle = angle * (double)degToRad;
Vector3<float> x = m_orientation.getXAxis();
Vector3<float> y = m_orientation.getYAxis();
Vector3<float> z = m_orientation.getZAxis();
x.rotateAroundAxis(y,angle);
y = m_orientation.getYAxis();
z.rotateAroundAxis(y,angle);
m_orientation.setXAxis(x);
m_orientation.setZAxis(z);
}
void FrustumCamera::zAxisRotation(float angle)
{
Vector3<float> x = m_orientation.getXAxis();
Vector3<float> y = m_orientation.getYAxis();
Vector3<float> z = m_orientation.getZAxis();
x.rotateAroundAxis(z,angle);
z = m_orientation.getYAxis();
y.rotateAroundAxis(z,angle);
m_orientation.setXAxis(x);
m_orientation.setYAxis(y);
}
template <class Type>
void Vector3<Type>::rotateAroundAxis(Vector3<Type> axis, const float angle)
{
float radians = static_cast<Type>(angle * degToRad);
Type sinAngle = static_cast<Type>(sin(radians));
Type cosAngle = 0.0;
if (angle == 90 || angle == -90)
cosAngle = 0.0;
else
cosAngle = cos(radians);
normalise(axis); // normalise the axis
Type oneMinusCos = 1 - cosAngle; // (1 - cos(theta))
// construct the rotation matrix
Type tempMatrix[3][3];
tempMatrix[0][0] = (axis.x * axis.x) * oneMinusCos + cosAngle;
tempMatrix[0][1] = (axis.x * axis.y) * oneMinusCos + axis.z * sinAngle;
tempMatrix[0][2] = (axis.x * axis.z) * oneMinusCos - axis.y * sinAngle;
tempMatrix[1][0] = (axis.x * axis.y) * oneMinusCos - axis.z * sinAngle;
tempMatrix[1][1] = (axis.y * axis.y) * oneMinusCos + cosAngle;
tempMatrix[1][2] = (axis.y * axis.z) * oneMinusCos + axis.x * sinAngle;
tempMatrix[2][0] = (axis.x * axis.z) * oneMinusCos + axis.y * sinAngle;
tempMatrix[2][1] = (axis.y * axis.z) * oneMinusCos - axis.x * sinAngle;
tempMatrix[2][2] = (axis.z * axis.z) * oneMinusCos + cosAngle;
Vector3<Type> temp(*this);
Vector3<Type> result;
result.x = (temp.x * tempMatrix[0][0]) + (temp.y * tempMatrix[1][0]) + (temp.z * tempMatrix[2][0]);
result.y = (temp.x * tempMatrix[0][1]) + (temp.y * tempMatrix[1][1]) + (temp.z * tempMatrix[2][1]);
result.z = (temp.x * tempMatrix[0][2]) + (temp.y * tempMatrix[1][2]) + (temp.z * tempMatrix[2][2]);
*this = result;
}
void OpenGLRenderer::startDraw(unsigned long mask)
{
//sortBuffer(); // sort draw queue
clearBuffers(mask); // clear buffers
loadIdentity();
glTranslatef(-1*m_frustumCamera->getViewMatrix().getTranslationAxis().x,-1*m_frustumCamera->getViewMatrix().getTranslationAxis().y,-1*m_frustumCamera->getViewMatrix().getTranslationAxis().z);
glMultMatrixf(m_frustumCamera->getViewMatrix().getMatrix());
glTranslatef(m_frustumCamera->getViewMatrix().getTranslationAxis().x,m_frustumCamera->getViewMatrix().getTranslationAxis().y,m_frustumCamera->getViewMatrix().getTranslationAxis().z);// load identity
//
// push matrix stack
matrixStackPush();
}
You might be experiencing Gimbal Lock; this can happen if you pitch all the way up or down so your look vector becomes parallel with your up vector, In which case a yaw will be the same as a roll.
This can be a downside of constructing rotations piecemeal via Euler angles. You may want to look into quaternions. (Note that you cant rotate with Euler angles; they are just a representation for rotation (you need to convert it to matrix or quats), but the way you are tackling it is very much an 'Euler angle' way of thinking about it)
The strength of matrix multiplication is that any sequence of multiple rotations can be represented (and concatenated) as a single rotation matrix. What you need to be doing is something like this:
void Transformable::yaw(float angle)
{
float4x4 rot; // temp rotation matrix
float3 translate(&_transform._41); // save our translation
float3 up(&_transform._21); // y axis
// build the rotation matrix for rotation around y
MatrixRotationAxis(&rot, &up, angle);
// multiply our transform by the rotation matrix
// note that order of multiplication matters and depends on
// if your matrices are column-major or row-major
MatrixMultiply(&_transform, &_transform, &rot);
// write back our original translation
memcpy(&_transform._41, &translate, sizeof(float3));
// might want to reorthogonalise every now and then
// to make sure basis vectors are orthonormal
// or you will probably get matrix creep after a few operations
}
instead of trying to rotate one basis vector at a time. In this case _transform would be a 4x4 homogenous matrix representing the transformation matrix. (rotation and translation). The topleft 3x3 submatrix is simply the basis vectors of the orientation space.