# max velocity/speed

## 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.

##### Share on other sites
I'll correct my post above : clamping the vertical component right over the ground would work only on a flat terrain. You should instead subtract the velocity by a normal from the terrain below rescaled to a right amount (something like a dot product of the velocity against it) But deciding which normal from the terrain to use when the ship is moving some distance above the ground might prove tricky :x

Quote:
 Original post by w00But 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??

The characters I don't know. I believe that some engines like Valve Source applies physics on dead bodies, but leave the living characters motion in the hands of pre-calc anims (as long as they aren't jumping or falling).
I'm pretty sure though that physics is applied as soon as some vehicle enters the picture, including gravity, friction, collisions... and of course every force applied to the vehicle has an impact on its velocity vector.

Any wheeled vehicle top-speed is also maxed out because of the ability of the vehicle's engine to apply an acceleration to it, ability which is dependent on the current turn/min of the engine, which we could simplify as the vehicle speed (you've certainly seen acceleration/speed charts in racing games already).

##### Share on other sites
If your ship is moving in space and therefore no drag is applied you should think about calculation like in Einsteins theorie. Those equations limit the maximum speed to the speed of light.
Quote:
 Original post by alvaroYou are aware that in space there is no maximum speed except the speed of light in the vacuum, right?

If you do not want your ship to travel that fast you only have to replace the constant for the speed of light in Einsteins equations by your desired maximum speed.

There is one feature that comes along with this equations that you probably do not want. Einsteins equations are working by increasing the mass of a moving object. By reaching the speed of light this mass gets infinite. So the effect would be that slowing down your ship from high velocity will take far longer if you are near your maximum speed.

But I am afraid that those equations are far to complicated and have to high computational cost (they involve several square-root operations) to be applied in a game, though they would give you the most realistic behavior.

##### Share on other sites
Quote:
 Original post by alvaroYou 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.

Checking the pilot's rough inputs to keep the craft in an acceptable state is what avionics are for; nobody is suggesting attrition, only deliberate control systems.

A combat spacecraft has to be as slow as possible to reduce turning radius, slow enough to avoid crashing into obstacles, and hopefully fast enough to outrun or evade missiles and enemies; accelerating into oblivion is a very bad idea.

##### Share on other sites
Quote:
 Original post by LorenzoGattiChecking the pilot's rough inputs to keep the craft in an acceptable state is what avionics are for; nobody is suggesting attrition, only deliberate control systems.

That makes sense, and it suggests one possible implementation: Interpret the user's input as a desired velocity, and apply acceleration to try to get there. That might result in intuitive controls.

##### Share on other sites
Thats the method i use for my controls. It lends it self nicely to AI control.

##### Share on other sites
Quote:
 Original post by w00But 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??

In situations like that, it's the animation that defines the speed a character moves at.

That works by having the animators put translation info into the animation, so that running an animation actually moves a character. The engine looks at the translation info and uses it accordingly in the physics stuff so then it's completely controlled by the animation.

Another option though is to figure out what speed the animation is traveling at, and then adjust the rate of the animation playback based on how fast you actually want to move.

That makes it so that if you want a character to walk slowly somewhere, you can use the same animation as when you want them to walk quickly in another situation. The playback rate will just be different.

And lastly...

this doesn't apply to your game since it isnt character based but if you wanted to you could set up a system like this (pseudo code)...

void MovePlayerAtSpeed(float fSpeed){  if(fSpeed < MaximumWalkSpeed)    MoveUsingWalkAnimation(fSpeed);  else if(fSpeed < MinimumRunSpeed)    MoveByBlendingWalkAndRunAnimations(fSpeed);  else    MoveUsingRunAnimation(fSpeed);}

That will make it so if you tell a character to move slowly, they will walk, and if you tell them to move medium speed they will jog (blending the walk and run animations) and if you tell them to move fast, they will run.

HTH! And yes the examples are a little simplified but hopefully you get the idea (:

##### Share on other sites
Quote:
 Original post by alvaroYou 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.

The actual laws of physics would make for terrible space games. :)

##### Share on other sites
Quote:
Original post by justkevin
Quote:
 Original post by alvaroYou 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.

The actual laws of physics would make for terrible space games. :)

I know. However, the way the OP was worded ("with physics applied to it"), it was unclear to me that he understood that you need to redefine physics in order to get the gameplay you want.

##### Share on other sites
Quote:
 Original post by alvaroHowever, the way the OP was worded ("with physics applied to it"), it was unclear to me that he understood that you need to redefine physics in order to get the gameplay you want.

"Redefining physics" is a great way to cause all sorts of artifacts and disorientation.
Real physics has been worked out in detail, "redefined" physics has enormous reserves of unexpected behaviour and inconsistencies.

##### Share on other sites
Quote:
Original post by LorenzoGatti
Quote:
 Original post by alvaroHowever, the way the OP was worded ("with physics applied to it"), it was unclear to me that he understood that you need to redefine physics in order to get the gameplay you want.

"Redefining physics" is a great way to cause all sorts of artifacts and disorientation.
Real physics has been worked out in detail, "redefined" physics has enormous reserves of unexpected behaviour and inconsistencies.

You can try to make a space game with no drag mechanics, with ships that don't necessarily face the direction they are moving in and using practically no indirect illumination in rendering (really black shadows). Oh, and make sure you don't hear any explosions or nearby ships. I wonder how many people would want to play that game...

I was thinking of something like using a fake "atmospheric" drag in space, not just making up new rules of Physics.

Anyway, games redefine Physics (as in "the rules that govern the world") all the time. People don't grow when they eat a mushroom, there aren't coins hanging in mid air that you can collect by touching them... If a player wants the exact rules of Physics to apply to a game, why does he need a simulation at all? Just go outside and play.

##### Share on other sites
Quote:
 Original post by alvarothere aren't coins hanging in mid air that you can collect by touching them

No, but we do expect "jumping" to result in a parabolic path. When part of the game is supposed to be a metaphor for a real-world action (which in turn is done to make the gameplay more intuitive), it's expected to have vaguely real-world physics. The coins are allowed to levitate because they're not real-world things.

Similarly, your life counter is metaphorical (in the "a cat has 9 lives" sense), referring to chances to do something. You can't really gain more lives in the real world, but in the game, it's still a *counter*. You don't have powerups that double your remaining lives or traps that reduce them to the integer square root of the current value; because the idea of losing a life is "you messed up; try again".

Basically, you have to know what's excused by genre convention (e.g. small amounts of drag in space because it'd be impossible to learn how to maneuver properly otherwise) and what isn't.

Alternatively, you could scale the numbers up and see if you can use relativistic effects as an excuse somehow. ;)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628305
• Total Posts
2981967

• 9
• 13
• 11
• 12
• 11