Advertisement

rotate / turn to face

Started by December 16, 2002 09:18 PM
2 comments, last by zerodot 22 years, 2 months ago
I''m an experienced programmer, but a 3d math newbie so I thought this forum would be more appropriate for this particular question. I want to rotate an object to face another object, no matter where it is in 3d space. In other words, a 3d arrow that points to an object no matter where you move it. Or, a camera that circles around an object yet is constantly looking at it. I''ve looked into quaternions, and have found some decent references, but no solid,concise code or answers. I''m looking for a straightforward way to make an object face another object, whether its a function, or a simple forumla. The answers I''ve found have been far too broad for me to successfully implement my own function. And I haven''t found code specifically suited for this supposedly very common feature of almost every game I''ve ever seen. Any help would be really appreciated.
Slerp, spherical linear interpolation, I believe is what you are looking for. There were a couple of responses here giving formulas for it. An alternative is to use an axis angle rotation matrix. The before and after vectors define a plane passing through the origin. Their cross product gives you an axis of rotation. The dot product will give you the angle, but not the sign, but you can get the sign by checking which side of the plane defined by the axis of rotation and your before vector. It seems like if you went through that symbolically you would pretty well have to end up with the same equation. If you actually need a transform matrix is most likely the better way.

Generally one direction isn''t going to cut it. You generally need to rotate an entire coordinate space for lack of better terminology. Basically you need to know what the other two coordinate axes are doing. Since given two you can find the third you only really need two, but except for special cases you do need two.
Keys to success: Ability, ambition and opportunity.
Advertisement
May be you need functin LookAt( ) ?
Suppose you want to look in the direction of a vector v1, but you are now looking in the direction of a vector v2.

Begin rotating the object arccos(v1 dot v2) radians around v1 x v2, assuming v1 and v2 are normalized.

Then you probably want to rotate the object around v1 to get it rolled the way you want it. Suppose the object''s up is in the direction of a vector v3 orthogonal to v2, and you want v3 to point as much as possible in the direction of a vector v4 (v4 would probably be the z axis).

Since you are rotating around v1, v3 can only end up in the plane orthogonal to v1. The closest position to v4 is the along the vector formed by projecting v4 onto this plane: v5 = v4 - v1(v4 dot v1).

Now construct a rotation from v3 to v5 as you did from v2 to v1. Combine the rotations and you''re done. Just make sure to check that v1 and v4 are not parallel -- then you can ignore the rolling since it''s impossible anyway.

(Note that this can probably be simplified quite a lot.)

Or you could use a LookAt() function in some API

This topic is closed to new replies.

Advertisement