Skip to main content
GameDev.net gamedev.net
🔒 Locked

Vector Rotation Around Axis

Started by CJWard Mar 16, 2011 at 1:45 AM 5 replies 1.3k views
Original Post
CJWard
CJWard
int CD3D9Camera::RotateY(float p_fAngle)
{
float x = m_Right.m_fX;

m_Right.m_fX = cos(p_fAngle) * m_Right.m_fX - sin(p_fAngle) * m_Right.m_fZ;
m_Right.m_fZ = cos(p_fAngle) * m_Right.m_fZ + sin(p_fAngle) * x;

x = m_Forward.m_fX;

m_Forward.m_fX = cos(p_fAngle) * m_Forward.m_fX - sin(p_fAngle) * m_Forward.m_fZ;
m_Forward.m_fZ = cos(p_fAngle) * m_Forward.m_fZ + sin(p_fAngle) * x;

ResetViewMatrix();

return 0;
}


I want this method to rotate the right and forward vectors around the Y axis by an angle. I have been trying for a few evenings to get this working but all I end up with is camera movement that swings back and forth if I continuously rotate by the same angle. I understand the basic mathematics behind what I am doing, in so far as a rotation matrix is required and that is multiplied by the vector and these are the vector values that are changing when rotating around the Y axis. If anyone can point me in the right direction or can offer a hand that would be great. I've been studying lots of articles but I'm failing to see where I am going wrong.
Zakwayda
Zakwayda
I didn't proof your code carefully, but it looks like you're applying a rotation about the world up axis, correct?

Is this for a typical first-person-style controller? If so, there shouldn't be any need to rotate vectors manually as you're doing. (Actually, it's not really necessary to do that for any type of camera, although it makes more sense for some types than others.)

If it is a first-person controller, I'd recommend representing the orientation using yaw and pitch angles, and building a matrix from them as needed. You can then extract the direction vectors from the matrix (if you need them for, say, linear motion).
CJWard
CJWard
For this method I simplified the rotation around an arbitrary axis matrix with one that simply rotated around the Y axis; believing I would get a different result. However, the result of the wavey camera remained. I am still unsure where to go from here except to take a completely different direction, either using quaternions or storing the yaw/pitch/roll individually.
BuffaloJ
BuffaloJ
Can you show [color=#660066]ResetViewMatrix()[color=#666600]?
[color=#666600]

[color=#666600][color=#000000][font=arial, verdana, tahoma, sans-serif]

I suspect the problem is in building the view matrix. Like the concatenation order is off. Could be you need to reverse the order and values of the translation and rotation. I think of building a camera matrix as the opposite of building a world object matrix - reverse the order of concatenation and negate the values. Like instead of translating(10,10) and then rotating by 10.0 degrees you would first Rotate by -10 and then translate by (-10,-10). Another way to do this is to build the matrix as you would a normal world object usual and then calc the inverse of it to use as the camera matrix.[/font]

CJWard
CJWard


int CD3D9Camera::ResetViewMatrix()
{
CVector Z = (m_Forward - m_Position);
Z.Normalise();

CVector X = m_Up.CrossProduct(Z);
X.Normalise();

CVector Y = Z.CrossProduct(X);

m_View.m_f11 = X.m_fX; m_View.m_f12 = Y.m_fX; m_View.m_f13 = Z.m_fX; m_View.m_f14 = 0.0f;
m_View.m_f21 = X.m_fY; m_View.m_f22 = Y.m_fY; m_View.m_f23 = Z.m_fY; m_View.m_f24 = 0.0f;
m_View.m_f31 = X.m_fZ; m_View.m_f32 = Y.m_fZ; m_View.m_f33 = Z.m_fZ; m_View.m_f34 = 0.0f;
m_View.m_f41 = -X.DotProduct(m_Position); m_View.m_f42 = -Y.DotProduct(m_Position); m_View.m_f43 = -Z.DotProduct(m_Position); m_View.m_f44 = 1.0f;

return 0;
}


This method appears to have been working fine for a couple weeks, I'm not sure how it could be causing problems. I've been working on this all this evening too and I'm still getting nowhere. I'm not far off making a quaternion camera just to overcome this.
Burnt_Fyr
Burnt_Fyr



...



This method appears to have been working fine for a couple weeks, I'm not sure how it could be causing problems. I've been working on this all this evening too and I'm still getting nowhere. I'm not far off making a quaternion camera just to overcome this.


Assuming m_Forward is a forward vector, then i don't believe you want to calculate Z the way you are, this may have worked if your camera was at <0,0,0> (or any where on the -Z axis), but if you put pen to paper with any other position, you should see that this is wrong... If you are already storing forward, up, and right vectors in world space, you can use them for ZYX respectively. The assignments to m_View look accurate otherwise.
CJWard
CJWard
Solved thanks, the view matrix was incorrect. Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.