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

Started by
3 comments, last by Zakwayda 19 years, 4 months ago
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]
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Advertisement
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.
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.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
Yay, it works!

Thank you very much fella's :-)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
You're welcome :-)

This topic is closed to new replies.

Advertisement