Jump physics not congruent with calculations

v4.3

Hey,

so I am designing my first game in godot and have implemented a jump.

My jump impulse is -200 and I am using get_gravity to calculate downwards movement implemented like this

var landing_time : int
var jump_time : int

var was_on_floor : bool = true

func _physics_process(delta: float):
if not is_on_floor():
velocity += get_gravity() * delta

if is_on_floor() and was_on_floor == false:
landing_time = Time.get_ticks_msec()
var hang_time = landing_time - jump_time
was_on_floor = true
print(hang_time)

if Input.is_action_just_pressed(“jump”):
if is_on_floor():
jump_time = Time.get_ticks_msec()
velocity.y = jump_impulse
was_on_floor = false

Now according to physics the total air time should be 2*jump_impulse/get_gravity() which would equal to 408 ms but when I calculate the air time in game I get 1267 ms. Can someone tell me where this difference comes from?

I assume it has something to do with frames, but I thought that would have been taken care of with using delta?

Your assumption is correct and I did a test with the values you provided and it’s exactly within the ballpark of 400ms as expected - you can see the print output every time the ball lands on the platform.


Here’s the code I used:

extends CharacterBody2D

var start: int
var was_on_floor: bool
var jump_impulse: float = -200

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_up"):
		start = Time.get_ticks_msec()
		velocity.y += jump_impulse

func _physics_process(delta: float) -> void:
	if is_on_floor() and not was_on_floor:
		print(Time.get_ticks_msec() - start, " ms")
	was_on_floor = is_on_floor()
	velocity += get_gravity() * delta
	move_and_slide()

Try to create an isolated environment to see if your result will be the same, because it should.
Maybe your gravity is changed somewhere in the settings or in the code?
Or your Engine.time_scale is changed?
There can be multiple of reasons.

1 Like

Ok, it was kind of my bad. Gravity was set to 980 as I confirmed with print statements and Engine.time_scale also returned 1.

I printed the jump_impulse and velocity.y values which I should have done in the first place and it turned out to be a wrong value.

This is still weird because I had it in the script as well as in the inspector set to -200 and it was still on 610 from an old trial.

Apparently I have to wrap my mind around when I have to reload scenes and stuff, but I think it is kind of misleading, that even after several saves and runs of the the scene, both displayed values from the script and the inspector are not what is used at runtime.

Edit:
@wchc Thank you for your quick and thorough help though!

1 Like