[Beginner] Needing help with collisions between two moving objects

Godot Version

Godot 4.3

Question

I’m very new to Godot but after several hours of troubleshooting and researching I’m needing help with a very simple task.

I’m making a simple ping pong game. When the ball collides with a paddle, I want to reverse it’s velocity on the z plane.

I’ve imported models from blockbench as .gltf files (Node3D). I’ve set up the ball physics as simply as possible:

# Update velocity
velocity.y += gravity * delta
velocity.y = max(velocity.y, -max_fall_speed)  # Cap fall speed

# Update position
position += velocity * delta

# Bounce logic
if position.y <= 0.08:  # Assume table is at y = 0
	position.y = 0.08
	velocity.y = -velocity.y * energy_loss  # Reverse y-velocity with energy loss

if abs(velocity.y) < 0.25:  # Stop bouncing if energy is too low
	velocity.y = 0

That seems to be working well. The issue is with collision. I’ve tried every combination of area and collision nodes I’ve read about, but no matter what I try the ball phases right through the paddle. Currently I have the paddles set up as Node3D > RigidBody3D > CollisionShape3D. The collision shape is a cylinder that matches the paddle face size.

In the ball’s script I have:

@export var paddle_player: Node3D

and

	
	if body == paddle_cpu or body == paddle_player:
		# Reverse the Z velocity on paddle hit
		velocity.z *= -1

And I’ve tried what feels like 50 other variations on this, yet nothing seems to work. If someone could recommend the most simple way to do this, I don’t mind what way but whatever is simple.

I should also note that I have a player paddle and a cpu paddle that tracks the ball’s x and y coords.

Thank you for the help!

You are trying to recreate physics, when Godot has already a built in physics engine that you can use with e.g. RigidBody2D.
See my little demo that I set up within maybe 3 minutes, that uses 0 code, just built-in physics behavior.

Here’s the scene structure.

You can tweak all the physics values (like bounciness, friction, velocity) to fit your project.

Thank you for the reply. Does it make a difference if im trying to build a 3D game? I am aware of the built-in physics engine, but I was having a lot of difficulty with it, specifically with controlling the ball’s bounce. I was finding more control and more success with setting up my own code. If that’s the approach I’d like to take, should I set things up differently? Or should I instead try and tackle the issues I was having with bounce on the table and paddle via the in-game engine?

I would suggest you to use rhe built in physics.
2D or 3D doesn’t matter.
If you want the ball to have a perfect bounce, you need change the dampening to 0.0 either in the ProjectSettings


or the RigidBody settings, by changing the damp_mode to Replace