Advertisement

Problem with velocity

Started by June 19, 2002 09:08 PM
2 comments, last by Zeraan 22 years, 7 months ago
In my asteroids-clone game, I''m trying to figure out how to do velocity and angle. This is the problem: There is a max velocity that my ship can get to. But once there, it can''t change angle of velocity because it''s at the max. The reason for this is: x is horizonal velocity. y is vertical velocity. if I want to change from 30 degree to 45 degree, I can''t because the game can''t add because it is at max and the x and y is added so how do I figure when it change angle to move? Sorry if I didn''t explain clearly. Thanks in advance. ______________________________________________ You know your game is in trouble when your AI says, in a calm, soothing voice, "I''m afraid I can''t let you do that, Dave"
You could relax your maximum velocity by letting the ship go faster than it. But when it does bring it back under the velocity.

E.g. you could every frame apply drag to the ship, by reducing it''s velocity by a fixed fraction over time. This combined with the (max) thrust gives a maximum velocit. E.g. if the drag is 10%/0.1 per sec and the thrust is 1 per sec the maximum speed/velocity is 10. The code for this might look like

{
fps = 60;
drag = 0.1;
thrust = 1;
vel.x = vel.x - (drag* vel / fps);
vel.y = vel.y - (drag * vel / fps);

vel.x += thrust * cos(bearing) / fps;
vel.y += thrust * sin(bearing) / fps;
}

the drag and thrust values are the same as given in the text, while fps = 60 assumes this is called 60 times a second. bearing is the direction the ship is pointing.

This is not the only way to do drag: you could have none for speeds < max and then massive amounts for speeds >= max to quickly bring it back within limits. Do it quickly enough and the user won''t notice the maximum is exceeded.
John BlackburneProgrammer, The Pitbull Syndicate
Advertisement
Hi!

Calculate the resulting Speed (sqrt(SpeedX^2+SpeedY^2))
if(Speed>MaxSpeed)
Divide SpeedX and SpeedY by Speed
Multiply SpeedX and SpeedY with SpeedMax


cLE
Ah I see how it works, thanks cLE, that was what I was looking for . Johnb, I can see how it works, but I don''t have drag. Thanks anyway.

______________________________________________
You know your game is in trouble when your AI says, in a calm, soothing voice, "I''m afraid I can''t let you do that, Dave"

This topic is closed to new replies.

Advertisement