limiting player speed

Started by
3 comments, last by Dae 14 years, 1 month ago
Hey everyone, I have a question about how to limit a player's speed. I have implemented a simple physics system: every player has a mass, position, velocity, and net force that is being applied to them. At every frame, the player's velocity is added to its position and the player's net force divided by mass is added to its velocity. When a player wants to move, a force is applied (added to the net force) in the desired direction of movement. This is nice because it causes the player to smoothly accelerate and decelerate. However, if the player holds down a key, they will accelerate without limit. The obvious solution is to cap the player's velocity at some level, but I don't want to do this because the game will have external forces like gravity wells and explosions and I don't want the effects of these to be capped. One thing that has worked for me is to implement friction which naturally limits the player's motions if the friction force is proportional to the player's velocity. However I can't easily change how fast the player reaches their maximum velocity. One thing I can think of would be to have some variable which decreases while the movement key is being held, and then apply the movement force based on this variable. Another idea I had is more physically based: when people walk, they apply a force to move themselves periodically. My idea is to apply the movement force to the player in a periodic fashion to (hopefully) naturally limit it's speed. Any thoughts/ideas?
Advertisement
You could make the player unable to apply force to increase his own velocity when his velocity is already higher than a certain constant. Then explosions could propel a player at any rate but movement speed would not accelerate endlessly.
Quote:Original post by MilfredCubicleX
However, if the player holds down a key, they will accelerate without limit. The obvious solution is to cap the player's velocity at some level, but I don't want to do this because the game will have external forces like gravity wells and explosions and I don't want the effects of these to be capped.
Then you should limit the amount of acceleration that the user can apply. I would split up user applied force and externally applied force. Then you just need to cap the amount of user applied force and recombine the two forces when you update the velocity. You may be able to split up the forces, update them separately to get 2 speeds, and then limit the user applied speed if you wish to control that variable more directly. Then combine the two speeds. Something along those lines at any rate.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:Original post by DanX2002
You could make the player unable to apply force to increase his own velocity when his velocity is already higher than a certain constant. Then explosions could propel a player at any rate but movement speed would not accelerate endlessly.


I like this idea, I will give it some thought.

Quote:Original post by nobodynews
Quote:Original post by MilfredCubicleX
However, if the player holds down a key, they will accelerate without limit. The obvious solution is to cap the player's velocity at some level, but I don't want to do this because the game will have external forces like gravity wells and explosions and I don't want the effects of these to be capped.
Then you should limit the amount of acceleration that the user can apply. I would split up user applied force and externally applied force. Then you just need to cap the amount of user applied force and recombine the two forces when you update the velocity. You may be able to split up the forces, update them separately to get 2 speeds, and then limit the user applied speed if you wish to control that variable more directly. Then combine the two speeds. Something along those lines at any rate.


This is similar to a solution I have used for a while (except that I used two different velocity variables). I will think about this some more. Thanks to you both.
How about going back to basics and only applying the velocity change ON KEY DOWN, which shouldn't be called again until ON KEY UP. Use key states, checking for change.
010001000110000101100101

This topic is closed to new replies.

Advertisement