Jump to content
  • Advertisement

Archived

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

nickwinters

Rotating Vectors

This topic is 5354 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Although this most likely belongs in the math forum, I am using DirectX and DX has some specific simplified ways of rotation vectors, so I''ve decided to post here. As of right now, everything has a position vector3, direction vector3, and up vector3. Assuming the default model faces <0, 0, 1>, I''m attempting to rotate the model so that it is parallel to the direction vector. One algorithm I''m trying is to set all y coordinates to zero, find the angle between the direction and original vector, and then rotate it by the angle on the y axis. I then do the same for the x value and rotate it around the x axis. This is giving strange results at best. This is most likely due to the way I find the angle: I take the inverse cosine of (A Dot B)/((size of A)*(size of B). This gives the absolute value of the angle. I''ve tried fixes like if the direction x component is less than zero, make the angle negative, etc. This isn''t a real fix, since I can''t get consistancy, and after it''s rotate, once the camera gets right behind the ship and has the same direction vector, the ship appears tilted. Am I going about this all wrong? Is there a simpler way? I''m using
device.Transform.World = Matrix.Scaling(scale, scale, scale) * Matrix.Rotation(aroundX, aroundY, 0.0f) *  Matrix.Translation(x, y, z);
 
To do rotations. Thanks. -Nick

Share this post


Link to post
Share on other sites
Advertisement
What you''re asking is how to rotate one vector into another. In this case, you want to rotate (0,0,1) to your direction vector. This is a very common topic in the Math forum and I''m sure it would come up in a search, but I''ll go ahead and explain it here anyway.

To rotate one vector into another, you need to define an axis of rotation. You do this by computing the cross product of your start and end vectors:

rotationAxis = (0,0,1) X direction;

Normalize this and you now have an axis of rotation:

Normalize( rotationAxis )

Now you need the angle to rotate about this axis. You can calculate this by taking the arccos of the dot product of your start and end vectors:

angle = arccos( dot( (0,0,1), direction ) )

Now you need to construct a matrix that represents this rotation:

D3DXMatrixRotationAxis( &matRot, &rotationAxis, angle );

If you use this matrix to transform your model, it will face whatever direction you want.

neneboricua

Share this post


Link to post
Share on other sites
Thanks again for your help. It's working now, as long as I don't change my vertical direction. So I have something wrong when the up vector is changed. The ship as a whole moves in the right direction, but it rotates in a weird way. Any advice on how to fix this problem?

Thanks.

-Nick

By rotate, I mean like spinning on the axis into the screen. So if it's a cylinder where a base is facing you, you can't tell. However, once I use a teapot, it becomes clear something is wrong.

[edited by - nickwinters on April 14, 2004 9:37:37 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!