Followers 0

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

## 4 posts in this topic

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)
[b][Mathematical equation][/b]

Thank you
0

##### Share on other sites
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.
0

##### Share on other sites
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:

[code]
x2 = x1 + cos(angle) * sp
y2 = y1 + sin(angle) * sp
[/code]

For example moving from (0, 0) at an angle 0 to distance 5 would give the following result:
[code]
x2 = 0 + 1 * 5 = 5
y2 = 0 + 0 * 5 = 0
[/code] Edited by mrjones
0

##### Share on other sites
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
[code]x2 = x1 + dx * sp;
y2 = y1 + dy * sp;[/code]

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

##### Share on other sites
[quote name='alvaro' timestamp='1337166051' post='4940648']
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
[code]x2 = x1 + dx * sp;
y2 = y1 + dy * sp;[/code]

I recommend doing that and avoiding angles as much as possible.
[/quote]

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.
0

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account