Sign in to follow this  
Mavix_2008

Moving an object in a given direction

Recommended Posts

How do I move an object in a given direction? I have the rotation of the object (in degrees) stored in a float. When I used MDX what I did was I put the distance that I wanted the object to move in a variable X, then got the Y component using some maths using Tan() and the objects rotation. What I then had was how many units to move along the X axis, and how many to move along the Y axis. It would then move in the direction it was facing. I tried to use this code in XNA, but it does not want to work. Does anyone know how I can do this?

Share this post


Link to post
Share on other sites
The normalized direction is (cos a, sin a). Multiply this direction by the distance you wish to travel and add it to the start position to get the end position.

Share this post


Link to post
Share on other sites
Thanks, ToohrVyk, it works! At first it wouldn't work properly, because the object would move sideways instead of forwards. So when it draws I just add 90 degrees to its rotation and it works perfectly. Its actually the same problem that I had in my previous code, but I didn't think of rotating it. Besides, the code Im now using is 10 times shorter and less buggy.
Once again, thanks!

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this