I do not understand physics in godot

This is a script for a ball that bounces on the floor

func _physics_process(delta: float) -> void:

	velocity.y += gravity
	var collision: KinematicCollision2D = move_and_collide(velocity * delta)
	if collision:
		var reflect = collision.get_remainder().bounce(collision.get_normal())
		print(reflect)
		velocity = velocity.bounce(collision.get_normal()) 
		move_and_collide(reflect)

However, I have no idea how the bounce() function and the get_remainder() functions work and what exactly they return. I would appreciate if someone broke this down into simple terms so I can understand.

get_remainder() returns the remaining velocity of the moving object after the collision.
For example, if the current velocity was Vector2(5, 10) and a collision happens at 80% of the attempted movement, get_remainder() would return the remaining 20% of Vector2(5, 10), which would be Vector2(1, 2).

bounce(normal) returns the reflection of a Vector from a surface with the given normal. To calculate this, the bounced Vector gets inverted and rotated by two times the angle to the normal.
You can imagine this as a ray of light that reflects on a mirror.

1 Like

I took the other same thread :smiley:

Well, two answers are better than none!

Lol. Not always.