Sign in to follow this  

limiting player speed

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this