Sign in to follow this  
xyuri

Maths problem with moving object at angle.. (Cos/Sin)

Recommended Posts

I am making an asteroids game and cant get my ship to move correctly ... I have revised on some algebra and mostly understand it but not completely how to apply it. My little ship gains momentum but does not slow down for now (this is intentional) This is how it gains momentum:
object.XVel += cos(object.angle) * (ACCELERATION * TimeFrame);
object.YVel += sin(object.angle) * (ACCELERATION * TimeFrame);
And this is how I move the ship aroumd:
object.XPos += cos(object.angle) * (object.Velocity * TimeFrame);
object.YPos += sin(object.angle) * (object.Velocity * TimeFrame);
The only problem is when I change the angle of the ship it changes the direction it is traveling in to what seems like a random direction .... This is how it turned out: http://www.xyuri.com/OpenGL Test.exe Can anyone help me out with this one? EDIT: also, if I do (sin(30) * 1) on my calculator I get (0.5) but using the functions in c++ I get (-0.988032) Why is this? [Edited by - xyuri on December 4, 2004 5:24:32 PM]

Share this post


Link to post
Share on other sites
First of all, C math functions take their arguments in radians, so try that and see if it fixes the problem.

Also, if you go to the trouble of updating XVel and YVel, why not just add them directly to XPos and YPos? And where does object.Velocity come from? Maybe I'm seeing things out of context and there's no problem there, but I thought I'd mention it.

Share this post


Link to post
Share on other sites
Remember to convert to and from radians. Programming languages typically use radians in their math functions, but people generally think in degrees. 360 degrees = 2*pi radians, so to convert to radians multiply by pi/180; divide by that to convert back.

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