Gravity

Why wouldn’t using clamp() work here ?

The Jump Jam (Mobile) Tutorial uses the following code to restrict player.velocity.y after applying GRAVITY = 15.0 and with a Global variable var max_fall_velocity = 1000.0

if velocity.y > max_fall_velocity:
		velocity.y = max_fall_velocity

I tried using clamp as follows

velocity.y = clamp(velocity.y, GRAVITY, max_fall_velocity)

If I try to print velocity.y in both cases it is restricted to 1000, but player.jump() only works with the code shown in the tutorial. Can someone explain why ?

Correct me if I am wrong

print([velocity.y, clamp(velocity.y, gravity, max_fall_velocity)]) showed that clamp overwrites the jump velocity and sets it to the MIN of the clamp (GRAVITY) where as the If condition does not alter the MIN of velocity.y

so using the clamp would be the equivalent of the following if condition ?

if velocity.y > max_fall_velocity:
		velocity.y = max_fall_velocity
else:
                velocity.y = GRAVITY

Morning,

The clamp function will make sure that a value is kept in between the Minimum and Maximum thresholds so it stays within that limits.

I’m not at my pc at the moment but the equivalent if statement to the clamp you have there would be something like


if velocity.y > max_fall_velocity:
		velocity.y = max_fall_velocity
elif velocity.y < GRAVITY:
                velocity.y = GRAVITY

Just to keep it within bounds.

But to get the clamp method working we would need to set the lower bounds to be the jump velocity to set the minimum.

Don’t have the values to hand as I’m only on phone atm

Thnx

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms