🔒 Locked
Using atan2
Advertisement
Original Post
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
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)
________. (point1: further UP than LEFT)
_____________. (point2)
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.
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.
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.
_________________* 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.
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.
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.
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.
(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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement