Jump to content

  • Log In with Google      Sign In   
  • Create Account


Maximum Velocity in 2D Platformer


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 noodlyappendage   Members   -  Reputation: 130

Like
0Likes
Like

Posted 11 March 2012 - 12:34 AM

I'm sure this will probably be something basic but I'm having some trouble figuring this one out. So I'm working on a relatively simple 2D platformer motion system. If I want to move an object I call an applyForce(f) function and give it a force vector. The problem I have is if, for example, the player is running, I want a maximum velocity so they don't turn into The Flash. But if I want to then apply another force to simulate a strong gust of wind, it should push the player to a faster rate than he can normally run, exceeding the maximum velocity. I've thought about assigning a max velocity property to each force, but after I take the wind gust force away, they should still have a momentum exceeding the normal maximum 'running' velocity, until friction brings them back down to normal speed.

Sponsor:

#2 AdrianC   Members   -  Reputation: 602

Like
1Likes
Like

Posted 11 March 2012 - 03:03 AM

Maybe not the best way to do it, but a fairly simple example:
if (speed < maxSpeed)
{
  speed += acceleration
  if (speed > maxSpeed)
    speed = maxSpeed;
}
else if (speed > maxSpeed)
{
  speed -= friction;
  if (speed < maxSpeed)
    speed = maxSpeed;
}

So if you're running slower then the max speed, then speed up and if you are faster then slow down.

Now for the gust of wind, temporarily increase the maxSpeed to your desired value, then drop it back down again at the end. The player will automatically adjust.

#3 noodlyappendage   Members   -  Reputation: 130

Like
0Likes
Like

Posted 11 March 2012 - 04:03 AM

Works great, thanks! It never even occurred to me to think of it as a "target" velocity rather than a maximum.

#4 kunos   Crossbones+   -  Reputation: 2205

Like
0Likes
Like

Posted 11 March 2012 - 04:23 AM

add a linear dampening force to the character.

addForce( velocity * linearDamp);

Tune linear damp to a negative value until you get the desired effect. This will look more physically correct than just capping the max speed to a given value, plus, it's just one line and the code is much more consistent.
Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS