How to find the position of object after move to an angle

Started by
3 comments, last by way2lazy2care 11 years, 11 months ago
Hello friends,
i just started to game developing.
please tell me how to move an object in any angle (maths)?

If
angle = angle to move
x1 = current x position
y1 = current y position
sp = distance to move

then what will be the position of the object after moving 'sp' distance of angle 'angle' from position (x1,y1)
[Mathematical equation]


Thank you
Advertisement
I'm not 100% sure I follow you correctly, but... Assuming sp is a vector, and not just a distance, the general equation would be roughly <x1, y1> + M * sp where M is the 2D rotation matrix given by {{cos(angle), -sin(angle)},{sin(angle), cos(angle)}}. If sp is a "distance", rotation has no meaning. You can only rotate relative to some reference frame.
I think you might want this equation, if sp is just distance, not a vector. Moving at angle 0 means moving on x-axis in this equation:


x2 = x1 + cos(angle) * sp
y2 = y1 + sin(angle) * sp


For example moving from (0, 0) at an angle 0 to distance 5 would give the following result:

x2 = 0 + 1 * 5 = 5
y2 = 0 + 0 * 5 = 0
If instead of using an angle you were to use a vector (dx, dy) of length 1 to indicate the same information, the code would simply be
x2 = x1 + dx * sp;
y2 = y1 + dy * sp;


I recommend doing that and avoiding angles as much as possible.

If instead of using an angle you were to use a vector (dx, dy) of length 1 to indicate the same information, the code would simply be
x2 = x1 + dx * sp;
y2 = y1 + dy * sp;


I recommend doing that and avoiding angles as much as possible.


This. Vectors carry a significant amount of information in a small package and have surprisingly simple functions for getting pretty much everything that's important once you understand them. It takes a while to wrap your head around vectors and matrices, but once you do you'll be much happier.

This topic is closed to new replies.

Advertisement