Advertisement

turn model to face direction of movement

Started by July 11, 2004 12:39 PM
22 comments, last by cippyboy 20 years, 4 months ago
Suppose I have a model that is originally facing (0.0f, 0.0f, 1.0f), but its direction of movement is say (0.5f, 0.5f, 0.0f). What is the easiest way to determing how the object is to rotate in order to face the direction that it is moving? thanks for any help
One way would be to build "look-at" matrix and extract angles from that. Matrix FAQ has just about all formulas you need for this.
You should never let your fears become the boundaries of your dreams.
Advertisement
Another way would be to query the feedback buffer, but it's a bit hard on arbitrary meshes with arbitrary camera.
interesting question. would like to know the answer too :D
ok, i have a camera class that implements its own look-at function.

I tell it:
1)where the camera should be
2)where the camera is looing
3)up

from this i create a matrix that i can pass to directx/opengl as the view matrix, and everything works great.

--------------------------------------

now, can't i modify this same code and tell it:

1)where the object is
2)where the object is going
3)up

then, instead of passing it to directx/opengl as the view matrix, i pass it as the world/model matrix.

this is what i am trying and it doesnt appear to be working as expected.
Few questions :
1- how do you load the matrix ? Using glLoadMatrix or smtg like that ?

2- does your object "look" towards the Z-axis ? This should be the case if you use the same math for the camera matrix setup and the object matrix setup.

3- What means "it doesnt appear to be working as expected" ? Can you post screenshots and/or source code, if useful, or describe the problem with other words ?
Advertisement
1)I'm trying to get this to work in directx, so i am using

SetTransform(D3DTS_WORLD, &(D3DMATRIX)matrix);

to set the world/model matrix


2) my model is facing (0.0f, 0.0f, 1.0f);

3) the code seems to work if the object is only rotated or translated about one axis. once multiple axis are involved, one seems to cancel the other out.
I don't know D3D very well but I think it works like OpenGL, and in that case the behaviour you describe seems logical to me, assuming that SetTransform does "overwrite" the current world matrix.
If you have to handle multiple axis, don't use a SetTransform. Rather, start with a LoadIdentity call (or whatever it's called in D3D) at the point you have no axis, and for each axis multiply with the current matrix instead of replacing it. In OpenGL, it means you call glMultMatrix instead of glLoadMatrix.
@vincoof: but it's not simpler to compute the angle between the vector at start position and the vector at end position of the model? i'm asking this because i'm trying this way... and works QUITE good o_O


using the matrix system, how should i compute this matrix ?
Why not normalize the vectors, compute the dot product, get the acos of the dot which will give you the angle between the vectors, convert to radians and build a rotation matrix and then rotate the whole model?

This topic is closed to new replies.

Advertisement