• Announcements

Archived

This topic is now archived and is closed to further replies.

Rotating Forward Vector

Recommended Posts

jonbell    100
I am storing an Up, Right and Forward vector for all my game entities. I want to be able to rotate the forward vector so that a player can look up and down (FPS style) but cant get the trig right. Can someone please help. Cheers

Share on other sites
meink    133
What trig are you using at the moment?
Wouldn''t it be the same as rotating left and right, just around a different axis?

- Xavier
http://www.noreality.net

Share on other sites
jonbell    100
To rotate around the Y axis i use :

X = (x * cosf(a)) + (z * sinf(a));
Z = (x * -sinf(a)) + (z * cosf(a));

However when i rotate around the X axis i get the result where if I am looking down the Z axis the it looks correct but if i look down the X axis the view rotates in a circle about it (which is pretty much what i would expect after some thought).

Share on other sites
jimmynelson    100
Why are you storing those vectors? Why not just a rotation matrix that can instantly create all those vectors?

So you are wanting to do a fps type view system? There are all kinds of ways of doing this. For instance if you have orthogonal matrices rotation you can simply mutiply them. But if you don''t know what that is you can simply it.

You can search online and build matrices from Euler angles (yaw, pitch,roll) - usally in radians. So what you can do is create a ratio of what you mouse is returning in offset. And use this too manipulate pitch (up and down) and yaw (left right). Also ignore time on this occasion, it takes care of itself. So if you mouse gives you an offset of 0.12 from last frame. You ratio says is 1:1, then just use that and add it too radians. Also you will want to correct your radians to stay in a certain bound. In (angles > 180)angles-=360, (angles < 180) angles += 360. But you will be using radians pi*2 for a full revolution.

The ratio will allow for mouse sensitivity. Confused? I will pseudo code to clearify. Mouse offset should be what it changed from last frame, should be automatic.

MouseX offsetyaw
MouseY offsetpitch

//set for how sensitive
ratio=0.5;

pitch+=ratio*offsetpitch
yaw+=ratio*offsetyaw

(pitch>pi)pitch-=pi*2
(pitch
(yaw>pi)yaw-=pi*2
(yaw
This keep pitch and yaw between -pi and pi.

Share on other sites
owl    376
I use only one vector for direction, which is also the velocity vector.

Share on other sites
Lord_Jake    122
A general rotation matrix which rotates a vector "v" around a vector pAxis by a degree "dAlpha" is set up like this:

void createRotationMatrix ( double*  pAxis,                            double** pMatrix,                            double   dAlpha ){  // calculate cosinus and sinus (in radiant, not in degree!)  double fCos = cos ( dAlpha * PI / 180);  double fSin = sin ( dAlpha * PI / 180);  // build the matrix  pM[0][0] =   pAxis[0] + fCos * (pAxis[1] + pAxis[2]);  pM[0][1] = - pAxis[2] * fSin;  pM[0][2] = - pAxis[1] * fSin;  pM[1][0] =   pAxis[2] * fSin;  pM[1][1] =   pAxis[1] + fCos * (pAxis[0] + pAxis[2]);  pM[1][2] = - pAxis[0] * fSin;  pM[2][0] =   pAxis[1] * fSin;  pM[2][1] =   pAxis[0] * fSin;  pM[2][2] =   pAxis[2] + fCos * (pAxis[0] + pAxis[1]);}

If you have the rotation matrix, just multiply your direction vector with this matrix:

V(new) = Matrix * V(old)

Lord Jake

y = rx ( 1 - x ) ; r > 3.5699
... and chaos may begin ...

[edited by - Lord_Jake on March 24, 2004 10:12:01 AM]

Share on other sites
Guest Anonymous Poster
Lord_Jake,

That rotation matrix works great. I was looking for just that
recently. But could you explain the derivation for this?
Or point me in the right direction?

The most obvious way (to me at least) to do this involves a lot more complicated trig - how do you come up with such an elegant solution?

Thanks!