#Does anyone have some tips on RigidBody2D? Making horizontal ping pong.

14 messages · Page 1 of 1 (latest)

dim merlin
#

These are all separate things to be considered.
Simplest thing is probably making it bounce high enough.

#

Have you tried modifying its Physics Material?

true horizon
#

Barely does anything from what I can see

dim merlin
#

Make sure to set the Linear and Angular damp modes to Replace (as mentioned in the tooltip for Bounce)

true horizon
#

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

true horizon
#

Bump

gritty anchor
#

any reason it needs to be a rigidbody? characterbodies are easier control with basic bouncing

true horizon
gritty anchor
dim merlin
#

I agree. If you're using a set speed and manually handling bounces, a character body is much better suited

true horizon