#Does anyone have some tips on RigidBody2D? Making horizontal ping pong.
14 messages · Page 1 of 1 (latest)
Yeah I have!
Barely does anything from what I can see
Make sure to set the Linear and Angular damp modes to Replace (as mentioned in the tooltip for Bounce)
Sadly, no changes
@export var speed: float = 300.0
@export var angle_variation: float = 35.0 # Maximum angle deviation in degrees
var bounce_intensity = 10 # Adjusted bounce intensity
func _ready() -> void:
# Set an initial velocity with an upward component
apply_impulse(Vector2.ZERO, Vector2(speed, -speed).rotated(randf_range(0, PI * 2)))
func _on_body_entered(body: Node) -> void:
if body.is_in_group("paddle"):
var normal = (position - body.position).normalized()
var reflection = linear_velocity.bounce(normal).normalized()
var variation = randf_range(-angle_variation * deg_to_rad(1), angle_variation * deg_to_rad(1))
reflection = reflection.rotated(variation)
# Adjust velocity directly
linear_velocity = reflection * speed * bounce_intensity
# Ensure upward movement but limit height
linear_velocity.y = max(-speed, linear_velocity.y) # Limit max downward speed
# Push the ball away from the paddle slightly
position += normal * 2 # Small push to separate from paddle
elif body.is_in_group("borders"):
var normal = (position - body.position).normalized()
linear_velocity = linear_velocity.bounce(normal).normalized() * speed * bounce_intensity
# Push the ball out of the border
position += normal * 1 # Small push to separate from the border
func _physics_process(_delta: float) -> void:
# Keep the speed consistent
linear_velocity = linear_velocity.normalized() * speed```
heres the code for it
Bump
any reason it needs to be a rigidbody? characterbodies are easier control with basic bouncing
I mean, i assumed it would be a rigid body cause it's a object I wouldn't control
The naming choice of the Characterbody wasn't great for godot 4. Previously it was known as a KinematicBody, which basically means a body you move via script and it's not actively simulated otherwise.
That's usually what you're looking for in these arcade games. There are a multiple pong tutorials for godot 4 you can check out and they all use CharacterBodies afaik
I agree. If you're using a set speed and manually handling bounces, a character body is much better suited
There are pong tutorials but the ones I found must've been for a earlier version of it cause some of the coding just didn't work. But I will definitely take what you said about the character body in account