Jump to content
  • Advertisement
Sign in to follow this  
Goishin

find the angle between two points

This topic is 4150 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

Hello, I hope I'm posting in the right forum. If I'm not, please correct me. My problem: I'm trying to figure out how to get the angle between two points. For example: ' ' ' ' ' | ' ' ' ' ' ' ' ' ' ' | ' ' ' ' ' the target ' ' ' ' ' | ' ' ' ' ' -----+----- the origin ' ' ' ' ' | ' ' ' ' ' ' ' ' ' ' | ' ' ' ' ' ' ' ' ' ' | ' ' ' ' ' ' ' ' ' ' | ' ' ' ' ' edit: it's so hard to get the ascii to line up. Just image it's an x/y graph please I would like to be able to determine the angle from the origin (0,0) to the x. I'm pretty sure there's a formula out there, but I don't seem to be able to find it. What it's for: I'm working on a bit of a game, using c++ and allegro. It's got a little space ship in it, and I've enabled a warp button that randomly changes it's location. I've disconnected the screen from the location of the ship so that the screen will "chase" the ship around the playing field (which is much larger than the screen). This gives it a nice effect when the ship warps as it's got to scroll through the entire playing field to catch up with the ship. But it doesn't move naturally. For example, if the ship moves 40 x and 100 y, the screen will finish the 40 in the x direction before the 100 in the y direction, so it's got another 60 to go in just the y direction. It looks very mechanical. By getting the angle to the ship, I can make the screen move along the angle's vector and have x and y finish at the same time. Thanks, - goishin

Share this post


Link to post
Share on other sites
Advertisement
Trigonometry on wikipedia. Also: Vectors.

Actually, you don't need the angle at all, if I understand your problem correctly. Just make a vector between the two points, and translate along that.

Using your example, your vector would be [x,y] = [40,100]. If you wanted to make the translation in 10 frames, you would translate 0.1*[40,100] = [4,10] each frame. If, however, you want it to move at a set velocity, you would have to make some function to predict your framerate, and scale your translation accordingly.

Hope this helps (and was at all understandable, it's late and I'm sleepy).

Share this post


Link to post
Share on other sites
Thanks guys, this gives me two different options to try. I think I'm going to try the atan2 option first because I've made the screen into just another sprite, and update it with the same plumbing as all the other sprites. I just need the angle to move it in.

If that doesn't work, then I can try the vector approach.

Thanks,

- Goishin

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!