# max velocity/speed

This topic is 2983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a spaceship in my game with physics applied to it. When i want to move the ship forward (by pressing the W key) i simply apply a force to the ship. The problem here is that the ship keeps accelerating aslong as i press and hold the forward button. My question is, how to set a maximum limit to the speed/velocity?? So that it accelerates up to a certain speed, no matter how big the velocity is i applied to the ship.

##### Share on other sites
Where you apply the force(Psuedocode):
curr_speed += acceleration;if(curr_speed > max_speed)   curr_speed = max_speed;

?

##### Share on other sites
A couple of ways, one would be to simply add a regulator (basically something like velocity = min(maxVelocity, velocity);) The other way would be to check the current velocity and then determine if it is under the max velocity before applying further thrust (though unless you also regulate it, chances are it would go a little or a lot under depending on how much thrust you generate.) Both of these are really based on the same idea and both are simple logic problems... Like really simple.

This seems like kind of a weirdly simple problem, maybe I'm missing something.

##### Share on other sites
You could introduce some kind of drag, make it proportional to the current velocity but applied in the opposite direction.

dragCoeff = 0.1;
drag = -velocity*dragCoeff;
velocity += acceleration;
velocity += drag;

This can get overly complicated and perhaps the other ideas presented would be more suitable but it does provide a slightly more realistic effect as things approach their max velocity. Also the ship will slow down when no acceleration is applied.

Unfortunatly dragCoeff,acceleration and max velocity are now all linked so setting the max velocity is not as straight forward. You want it such that when max velocity is reached then drag = -acceleration.

The dragCoeff value can be found by doing acceleration/maxSpeed.

If you are going to use values above 1 then you have tomake sure the drag is <= velocity otherwise it will cause the velocity to suddently change direction.

##### Share on other sites
Nanoha's solution will work well-- I used something basically like it in my last game and it provided a good "feel" because ships coasted for a while after thrust stopped, and slowed down gradually.

Another option would be to set a max speed and have thrust increase velocity as a percentage of the difference. With this method ships don't automatically slow down when you stop accelerating.

##### Share on other sites
Another way is to just clamp the velocity of an object after all forces are applied. Simply:

if (velocity.Length() > maxSpeed)
{
velocity.normalize();
velocity *= maxSpeed;
}

That way you can accumulate a bunch of physics forces (multiple thrusters, drag, impacts with other objects, etc.) then just clamp it to maxVelocity at the end of the motion update.

##### Share on other sites
You are aware that in space there is no maximum speed except the speed of light in the vacuum, right? If you keep accelerating in some direction, you will end up going very fast.

##### Share on other sites
I've tried playing around with the velocity settings. That does seems to be working but the downside is that i can climb any mountain, even if its almost vertically up...

Guess i'll just stick with that method for now.

But i'm also wondering how other games do it in general. Like a game as Call Of Duty. It also has physics on their characters. Do they also play with the velocity settings to move them around??

##### Share on other sites
Project the velocity onto the surface plane:

The easiest way to get that is:
float reduction = Dot( characterVelocityTopDown, terrainNormal );
vec3 projectedVel = characterVelocity + terrainNormal * reduction;

Where characterVelocity is the 2d top down velocity managed by the joystick input. So as the normal displaces from vertical, the player will slow down in the top down view. This is also assuming that the vertical velocity component will be unused and that the height will be set to the terrain.

You can also use the dot between the normal and the up reference to calculate a "slow down" factor to reduce the players speed so that they aren't super human.

Should also note that if the characters speed is zero if he runs exactly vertical, these calculations are nonsensical.

*i need some coffee, rough night

##### Share on other sites
Quote:
 Original post by w00I've tried playing around with the velocity settings. That does seems to be working but the downside is that i can climb any mountain, even if its almost vertically up...

Mountains make me believe your spaceship is not evolving in void space, so there could be a justification for atmospheric forces on your ship. In which case Nanoha's solution will give you the most realistic results (simulating viscous forces applied to your ship trying to move through the air).

About vertical velocity : A force you could apply to reduce the climbing speed is simply gravity. Gravity would apply on your ship as a steady acceleration orientated downwards.

To avoid that the ship buries under the ground under the effect of gravity, simply negate the vertical component of the speed of your ship at some distance over the ground if this component would bring the ship lower than it already is, representing either wheels-touching-the-ground reaction, or any form of hovercraft reaction (for a Star Wars Speeder looking effect). A more realistic approach would be to apply the reaction as acceleration directed as a normal of the terrain below (instead of clamping vertical speed) but this could prove more difficult to adjust.

• 10
• 16
• 9
• 13
• 41