# Rotating one vector to face another in 2D

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

## Recommended Posts

I'm having a really tough time with this. I need to rotate a vector to face another vector (for example, having an image rotate to face the location of the mouse). Here's a picture of what I mean: With 0 rotation facing to the right (radians). How do I get that angle of rotation given the x and y of the 2 points? Thanks in advance

##### Share on other sites
Quote:
 Original post by Ganoosh_How do I get that angle of rotation given the x and y of the 2 points?
angle = atan2(target.y - pos.y, target.x - pos.x);

##### Share on other sites
Ah, thanks that got it, I was using atan and it wasn't working, not sure why it's basically the same thing. Maybe my points were screwed up. I don't know. But it's working fine now, thanks

##### Share on other sites
Typically you don't ever want the exact angle. You'll want the sin and cos of that angle. You can get the cos from the dot product and the sin from the cross product. If you do however need the exact angle, just use acos the value returned by the dot product. Note: be sure to normalize before you do dot or cross product.

##### Share on other sites
Quote:
 Original post by blaze02Typically you don't ever want the exact angle. You'll want the sin and cos of that angle. You can get the cos from the dot product and the sin from the cross product. If you do however need the exact angle, just use acos the value returned by the dot product. Note: be sure to normalize before you do dot or cross product.
There are situations where the angle itself can be useful. Furthermore, in those cases it's usually better to use atan2() rather than acos().

##### Share on other sites
Quote:
 Original post by Ganoosh_Ah, thanks that got it, I was using atan and it wasn't working, not sure why it's basically the same thing. Maybe my points were screwed up. I don't know. But it's working fine now, thanks

atan is a simple arctangent.

The arctangent is only defined for + or - 90 degrees before it resets. What that means is that 91 degrees (from +X) gives the same result as 271 degrees.

atan2 takes the quadrant into account and is valid for the full 360 degrees.

##### Share on other sites
Why not subtract the target position from the source position, and scale this vector by the length you want? Instant vector pointing to target.

##### Share on other sites
edit:
Ignore this post... I misunderstood the previous poster and -basically- I'm saying the same thing.
/edit

Quote:
 Original post by Rockoon1Why not subtract the target position from the source position, and scale this vector by the length you want? Instant vector pointing to target.

It needs to be normalized first, and the distance needs to be known (the sqrt is just as bad the sin()/cos()) but sure...

Keep this part only:
Quote:
 Why not subtract the target position from the source position (?)

:)

If the resulting vector has to be a fixed length, multiply by (new length)/(old lngth)

[Edited by - someusername on March 10, 2006 6:53:18 AM]

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×