Jump to content
  • Advertisement
Sign in to follow this  
Ganoosh_

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.

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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by blaze02
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.
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 Rockoon1
Why 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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!