Sign in to follow this  
Matt Aufderheide

LookAt rotation in increments

Recommended Posts

I'm not so great in maths; normally when i want to make an object look at (rotate to face) another I use a library matrix like D3DXMatrixLookAtLH (I use D3D). This wont exactly work for what I need however. What I'm trying to do is make an object turn to face a point but only by precise increments along yaw and pitch. To be specific, my object that rotates is a sphere at 0,0,0, and the point to face is an arbitrary 3d point (in my case the camera position--camera will always be OUTSIDE the sphere). What i was originally trying is something like this: //measure if current angle is different by enough D3DXVECTOR3 tempang=camera.pos; D3DXVECTOR3 tempang2=lastpos; //last position the sphere is facing D3DXVec3Normalize(&tempang,&tempang); D3DXVec3Normalize(&tempang2,&tempang2); float tempdegree=D3DXVec3Dot(&tempang,&tempang2); if (tempdegree<0.9999f) //arbitrary minimum difference { lastang=camera.pos; } then I just do the lookat matrix using the new cam pos (if has moved by enough). This does rotate in increments, but I am not sure if it really works in the way I want, because i haven't been able to figure out how to derive the correct increment number---i use 0.99999 just for a very small number.. how does this translate to degrees/radians, and how do I equate with a number that is based on a real measurement (say the increment of only 8 world units)? ------------------------------------------------------------------------------ I hope this is reasonably clear... Is this a workable method? Is there is a much better way?

Share this post


Link to post
Share on other sites
If all you care about is aligning the forward vector with some target vector, you can just take the cross product of the two vectors to extract the axis of rotation and the angle between them (recall that the cross product A x B is equal to length(A) * length(B) * sin(theta) * R, where 'theta' is the angle between the two vectors and R is a unit vector perpendicular to both of them). You can then use the D3DXMatrixRotationAxis() function to build a matrix that will rotate your object around the desired axis by whatever angle you want. So if you want to rotate halfway, for example, you would call D3DXMatrixRotationAxis( outMatrix, R, 0.5*theta ), and then apply 'outMatrix' to your object.

Share this post


Link to post
Share on other sites
Thanks for this, I tried it, and while it does rotate to face the direction, it's not really what I need. The rotation axis changes, essentially changing the "roll" orientation; I need it to always have a roll of 0.

Also, the incremental rotation needs to occur with yaw and pitch separately.

I know i can apply separate yaw and pitch using a quaternion for instance, but I don't know how to decide what yaw and pitch are to face a vector...does this make sense? I wish I could describe this better...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this