Why is physics stepping two frames late?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By huhund

Physics stepping seems to happen two frames after applying velocity or force. Is this intended?

I would assume it would happen on the next frame. Not two frames late.

I have the following example. I apply a velocity at 10, but the body doesn’t hasn’t moved until frame 12. I would hope it would have moved already at frame 11.

 func _physics_process(delta):
    	match frame:
    		10:
    			linear_velocity = Vector3(1,0,0)
    		12:
    			linear_velocity = Vector3(0,0,0)
    
    	force_update_transform()
    	print("frame: " + str(frame) + " vel " + str(linear_velocity) +" " + str(transform.origin))
    
    	frame += 1

And it gives me the following output

frame: 9 vel (0, 0, 0) (0, 0, 0)
frame: 10 vel (1, 0, 0) (0, 0, 0)
frame: 11 vel (1, 0, 0) (0, 0, 0)
frame: 12 vel (0, 0, 0) (0.1, 0, 0)
frame: 13 vel (0, 0, 0) (0.2, 0, 0)
frame: 14 vel (0, 0, 0) (0.2, 0, 0)

Note: The behaviour is the same even if I apply a force to the body.

I wrote the equivalent script for Unity. In Unity the rigid body is moving the next frame after setting velocity. This makes me think that this is some sort of bug in Godot.

huhund | 2020-04-08 12:11

Probably a bug. Because 2D physics is updated on the next frame.

huhund | 2020-04-08 17:52