find the angle between two points

Started by
2 comments, last by Goishin 16 years, 11 months ago
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
Advertisement
You need to use the atan2() function.
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).
if ( youreHappyAndYouKnowIt ) clapYourHands;
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

This topic is closed to new replies.

Advertisement