• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

#1Arun Haridas  Members

153
Like
0Likes
Like

Posted 16 May 2012 - 02:16 AM

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

#2Oolala  Members

1102
Like
0Likes
Like

Posted 16 May 2012 - 03:12 AM

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.

#3mrjones  Members

612
Like
0Likes
Like

Posted 16 May 2012 - 04:36 AM

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


Edited by mrjones, 16 May 2012 - 04:36 AM.

#4Álvaro  Members

20266
Like
0Likes
Like

Posted 16 May 2012 - 05:00 AM

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.

#5way2lazy2care  Members

786
Like
0Likes
Like

Posted 16 May 2012 - 11:16 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.