How come this simple falling equation does not seem to work very precisely?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nwgdusr999
func _physics_process(delta):
if falling:
	fallingVector.y += GRAVITY * delta
	var collision = move_and_collide(fallingVector);

I’m using the above as a falling formula for KinematicBody2Ds stacked on top of one another, and even if I make sure to have a few pixels between them and have them fall starting from the bottom, they still sometimes collide with one another even though they are using the exact same formula…

I just thought that maybe the function is called really intermittently, but I printed the values of delta and it seems 100% stable (0.016667), yet by reducing significantly the gravity, I can clearly see that they are not moving at the same speed… What could be happening?

:bust_in_silhouette: Reply From: Dlean Jeans

You should multiply the fallingVector with delta:

var collision = move_and_collide(fallingVector * delta)

according to the docs of move_and_slide:

linear_velocity is the velocity vector (typically meters per second). Unlike in move_and_collide, you should not multiply it by delta — the physics engine handles applying the velocity.

Not sure I understand… Delta = 0.016667. The Vector when an object starts to fall is 0,0. I set a variable for gravity (ex; =30) as KinematicBody2D is not affected by gravity, without using the “var gravity” and just multiplying by delta, they’d be slowing down…

Ok no I think you’re missing the point of the formula; wasn’t clear from the code… fallingVector is not used to set a constant falling speed, it’s holding the increasing falling velocity; the acceleration. Each frame the character is falling faster and faster. So the fallingVector is getting greater and greater the longer an item falls. What you seem to describe is a constant fall speed where fallingVector would contain the constant gravity value / falling speed.

What I don’t get is that if they all start falling at the same time and fall at the same rate, why would they be colliding with one another? Something doesn’t quite add up…

nwgdusr999 | 2019-05-31 15:56

:bust_in_silhouette: Reply From: nwgdusr999

Could it be because the physics engine isn’t … how to say… Doesn’t truly synchronize every scene elements? Might be an order issue? Ex:

You add element 1 to the scene and accelerate it down.
You add element 2 to the scene and accelerate it down.
both accelerate, both accelerate, both accelerate…
At one point, element 2 is accelerated before element 1 (for whatever reason) and because it now travels faster than element 1, it causes a collision…

I guess the engine doesn’t accelerate every object at the same time, so once it gets to a high velocity, even with a few pixels of safety, if the acceleration order isn’t exactly the same as initially, a collision occurs. That seems pretty similar to what I’m experiencing… Could explain it… Thoughts? Or it ‘predicts’ a collision? Yeah that doesn’t seem to make much sense… I’d think the physics engine computes all new locations before colliding… Grr…

Hi. Is the motion / sync_to_physics property of the kinematicbody2d activated? According to its description, it must be deactivated when used with move_and_collide

estebanmolca | 2019-05-31 21:03

It’s not activated… :\ It’s very weird you can actually see some of them traveling at different speeds (higher up seem faster…) Maybe it’s something else somewhere in my code, maybe I’ll try a bit later to make a simple test case project for it! Thanks!

nwgdusr999 | 2019-05-31 21:46