Angle from Vector

Started by
1 comment, last by MrD 588 16 years ago
Hey, I've been trying to work this out for a while, done some searching and am now more confused than ever. Essentially, I am working in 2D and have a set of objects. These objects have two 2D Vectors, one storing their position, and one storing their velocity. What I would like to do is somehow create an angle that can be used to rotate the object around the Z axis so that visually, it's heading is the same direction as it's velocity (so it points where in the direction it's moving). I have tried various combinations of atan2, dot product, etc with no result. Can anyone help?
Advertisement
Quote:Original post by MrDoom
Hey, I've been trying to work this out for a while, done some searching and am now more confused than ever.

Essentially, I am working in 2D and have a set of objects. These objects have two 2D Vectors, one storing their position, and one storing their velocity. What I would like to do is somehow create an angle that can be used to rotate the object around the Z axis so that visually, it's heading is the same direction as it's velocity (so it points where in the direction it's moving).

I have tried various combinations of atan2, dot product, etc with no result.

Can anyone help?
The standard solution would be:
angle = atan2(direction.y, direction.x);
This assumes that your objects point down the positive x axis in their default orientation (recommended); if not, you have to modify the arguments to and/or the return value from atan2().

Another option is to create the orientation matrix directly from the direction vector.
Ah ha.

Thanks a lot. I was sooo close, it's just that in XNA the default orientation is along the Y axis, I just had to compensate for it.

Thanks.

This topic is closed to new replies.

Advertisement