Skip to main content
GameDev.net gamedev.net
🔒 Locked

Using atan2

Started by DirtyTed Apr 6, 2005 at 1:08 PM 10 replies 950+ views
Original Post
DirtyTed
DirtyTed
Hello all! I'm a bit stuck at the mo with a game I'm writing. It concerns using atan2. I remember years ago using the same method but I've completely forgotten how to do it! I have two points on the screen, and I want point1 to move towards point2 evenly on both axis, and I know I can achieve it using atan2 but I cant remember the arguments to use, I vaguely remember using something like: y = y + atan2(cos(x), y) or something like that! Any help would be greatly appreciated, thanks. Dirty Ted
arm
arm
Do you want an angle or (x,y) delta value?
DirtyTed
DirtyTed
Oh sorry! No I dont need the angle I just need the values, so I can move one point to the other. So, for example, if the first point is further up than it is across I cant just add 1 to each value, I need to add MORE to the y value (cos its further away than the x value):

________. (point1: further UP than LEFT)





_____________. (point2)
arm
arm
This is an easy one, since no trigonemtry is required if all you need is a delta value for each component:

diff.x = point2.x - point1.x;
diff.y = point2.y - point1.y;

You can then divide these difference values by the number of steps you want it to take to get from point one to point two.

Or, if you want to move from one to two with a predefined velocity, you should normalise diff and multiply by your velocity (assuming a constant time step).

-arm.
DirtyTed
DirtyTed
Ok I see what you mean, but could you elaborate a little? I want point1 to move equally in both directions, so how do I calculate how much to add each time?
arm
arm
I'm not sure I understand at this point. If you change x,y by the same value each time step (ie move eually in both directions) then point one will only intercept point two exactly if they are at 45-degree angles to one another.

Tell me a little more about the problem at hand.
DirtyTed
DirtyTed
Ok, so suppose I have the following:


_________________* Point1 (50, 50)






_________________________* Point2 (80, 300)


I want point1 to move towards point2 equally along the vertical/horizontal so that point1 eventually meets point2. If I was to take, say, 2 from the x and y each time, then point1's x would reach point2's x before the y's meet.
arm
arm
So let me get this straight in my head, you actually want the x components to meet before the y components do?

This will only happen if difference.y is greater than difference.x

Or do you just want 45degree movement? Which is what you get when you change both components by the same value.
DirtyTed
DirtyTed
No, I want the x and y to meet at exactly the same time! If it was a 45' angle I could just take 1 away each time, but it's not!!!
Agony
Agony
In your example, you have Point1 = (50, 50), and Point2 = (80, 300) (Point2.x - Point1.x, Point2.y - Point1.y). The difference between these is (30, 250). So let's say that you want Point1 to reach Point2 in 10 frames. Update Point1.x by 30/10 each frame (3), and Point1.y by 250/10 each frame (25). Point1 becomes:
(53, 75)
(56, 100)
(59, 125)
(62, 150)
(65, 175)
(68, 200)
(71, 225)
(74, 250)
(76, 275)
(80, 300)

Just take the difference between the two points' x values, and divide that by how many steps you want to take to get to your destination point. Do the same for the y values, independent of the x values. Remember how much you need to update the x values by (delta-x, or dx), and y values (delta-y, or dy), and then each frame, add this value to your current x and y values.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
DirtyTed
DirtyTed
ARGH! What a silly boy am I! Thanks guys, I got it working now! Nice one!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.