How do I smooth jump and fall with lerp and clamp?

  • Hi,

    It's been a while since I've lurked in to construct. I want to make custom jump and fall using event's, but all the help I've got in the past got lost ( removing of pm's and windows failure requiring me to format hdd with copies of examples ). I frankly forgot how to properly use those expressions. So if someone could give me a code to get smooth jump and fall, as well as remind me why it works the way it does, I'd be very thankful.

    R0J0hound I know you know this very well :D

  • You mean like the platform behavior just with events instead?

    It's basically called parabolic motion, and you could use the bullet behavior with gravity if you wanted.

    Anyways to do it with just events you'd add two instance variables to the sprite : (vx and vy). Those would be the horizontal and vertical velocities. Then the event would be:

    every tick
    -- sprite: add 100*dt to vy
    -- sprite: set position to self.x+self.vy*dt, self.y+self.vy*dt

    I can't really think of a application of lerp or clamp with just that.

    You could clamp the vertical velocity so that it doesn't fall too fast?

    set vy to clamp(vy, -100, 100)

    I may have bungled the order of the parameters though.

    You could limit the y position for some kind of ground too:

    set y to min(self.y, 400)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You mean like the platform behavior just with events instead?

    It's basically called parabolic motion, and you could use the bullet behavior with gravity if you wanted.

    Anyways to do it with just events you'd add two instance variables to the sprite : (vx and vy). Those would be the horizontal and vertical velocities. Then the event would be:

    every tick
    -- sprite: add 100*dt to vy
    -- sprite: set position to self.x+self.vy*dt, self.y+self.vy*dt

    I can't really think of a application of lerp or clamp with just that.

    You could clamp the vertical velocity so that it doesn't fall too fast?

    set vy to clamp(vy, -100, 100)

    I may have bungled the order of the parameters though.

    You could limit the y position for some kind of ground too:

    set y to min(self.y, 400)

    Thanks! I will try it out. The game axis are xyz actually, and jump direction and strength can be controlled similar to Mario. I asked about Lerp and Clamp, because I want the longer the fall the faster you'd go and also I'd want to have jump height dependent on how long you press button with a limit. I would do it myself, but it would take me more events than necessary, and someone like you is very good at keeping things simple. :)

  • The math for motion can be done per axis which helps simplify things.

    We can just pick one axis as an example. You can do other axis in the same way.

    x is the position, and vx is the velocity.

    Constant speed:

    x = x + vx*dt

    acceleration:

    vx = vx + 100*dt //100 is the acceration

    x = x + vx*dt

    So for a mario style example you could do x with constant speed and y with acceration. Then to jump you'd set vy to -200 or something.

    On space pressed
    -- set vy -200

    You could also just apply the vy the while the button is pressed. Just use a timer too so you'd only set the velocity for so long.

    xyz stuff is mostly a matter of mapping the third axis onto xy.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)