Trying to program a good jump...

I’m making a game about a frog and I’m having trouble making his jump look/feel right. Here’s the code:

function jump( event ) if ( canJump == true ) then canJump = false frog:applyLinearImpulse( 0, -15, frog.x, frog.y ) vx, vy = frog:getLinearVelocity() gx, gy = physics.getGravity() local result = 2 \* -vy / gy print(result) end

But the result I’m getting is about 51 seconds, even though the frog is only off the ground for about 1 second. How can I find the amount of time the frog is actually in the air?

It could be a problem of how you wrote the equation:

It could be doing this:

(2 \* -vy) / gy

Or this: 

2 \* (-vy / gy)

Try switching the orders of the variables in the equation around and see if you get a more accurate answer.

I’m not sure I understand the question or the intent of that code.

The units for gravity and velocity in the real world are not the same.

Metric Velocity ==> Meters Per Second (m/s)

Metric Gravity (Acceleration) ==> Meters Per Second Squared  (m/s^2)

Also, please define “Feels Right”.   Does

“Feels Right” == “Super Mario Brothers” 

  • OR - 

“Feels Right” == “Like the Real World”

If the answer is “Super Mario Brothers” that is a very elaborate jump algorithm, not just a single impulse and gravity combination.

You might enjoy this video:

https://www.youtube.com/watch?v=hG9SzQxaCm8

If you want something approximating a real world arc, it is pretty straight-forward:

physics.getGravity( 0, 9.8 ) local jumpMagnitude = -15 function jump( event ) if ( canJump == true ) then canJump = false frog:applyLinearImpulse( 0, jumpMagnitude \* frog.mass , frog.x, frog.y ) end end

Also, be aware that getting the velocity directly after an impulse may not ‘get’ the entire velocity of the impulse.  It may simply get the velocity at the time ‘t’ when the impulse will be applied.

I haven’t verified this, but my sense is this is true.

If someone has tested this and I am wrong, please correct me here.

Try this example I wrote up: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/04/betterjump.zip

https://www.youtube.com/watch?v=2tz7I1j8WvI&feature=youtu.be

Bottom character gets increased gravity scale as soon as ‘jump touch’ is released.  Gravity scale is increased via transition.

In the video:

  • First series of jumps, I hold touch to show jumps are same.
  • Second series of jumps, I release early and the gravity scale logic kicks in for bottom player.

Can confirm, result does not change.

To clarify, I’m trying to calculate the duration of a jump for any given impulse and gravity value. As for the feeling, I’d like it to be realistic.

It could be a problem of how you wrote the equation:

It could be doing this:

(2 \* -vy) / gy

Or this: 

2 \* (-vy / gy)

Try switching the orders of the variables in the equation around and see if you get a more accurate answer.

I’m not sure I understand the question or the intent of that code.

The units for gravity and velocity in the real world are not the same.

Metric Velocity ==> Meters Per Second (m/s)

Metric Gravity (Acceleration) ==> Meters Per Second Squared  (m/s^2)

Also, please define “Feels Right”.   Does

“Feels Right” == “Super Mario Brothers” 

  • OR - 

“Feels Right” == “Like the Real World”

If the answer is “Super Mario Brothers” that is a very elaborate jump algorithm, not just a single impulse and gravity combination.

You might enjoy this video:

https://www.youtube.com/watch?v=hG9SzQxaCm8

If you want something approximating a real world arc, it is pretty straight-forward:

physics.getGravity( 0, 9.8 ) local jumpMagnitude = -15 function jump( event ) if ( canJump == true ) then canJump = false frog:applyLinearImpulse( 0, jumpMagnitude \* frog.mass , frog.x, frog.y ) end end

Also, be aware that getting the velocity directly after an impulse may not ‘get’ the entire velocity of the impulse.  It may simply get the velocity at the time ‘t’ when the impulse will be applied.

I haven’t verified this, but my sense is this is true.

If someone has tested this and I am wrong, please correct me here.

Try this example I wrote up: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/04/betterjump.zip

https://www.youtube.com/watch?v=2tz7I1j8WvI&feature=youtu.be

Bottom character gets increased gravity scale as soon as ‘jump touch’ is released.  Gravity scale is increased via transition.

In the video:

  • First series of jumps, I hold touch to show jumps are same.
  • Second series of jumps, I release early and the gravity scale logic kicks in for bottom player.

Can confirm, result does not change.

To clarify, I’m trying to calculate the duration of a jump for any given impulse and gravity value. As for the feeling, I’d like it to be realistic.