+1 vote

Whats the best way to create a paddle for a Air Hockey Game?
I'm using a rigid body and here's my current code. But it looks really ugly when playing, because it moves only after the distance to player is bigger than 20 px. And if I remove that distance check, its starts to bounce on that point, like jittering.

extends RigidBody2D

const SPEED = 2000

var destination = null

func _integrate_forces(_state):
    if destination != null:
        var direction = (destination - global_position).normalized()
        var distance_to_player = global_position.distance_to(destination)
        if distance_to_player > 20:
            linear_velocity = direction * SPEED
        elif distance_to_player > 3:
            linear_velocity = direction * SPEED * 0.20
        else:
            linear_velocity = Vector2.ZERO
asked Mar 1 in Engine by dulvui (31 points)

1 Answer

0 votes

I found the solution for now by multiplying the distance to touch position to the linear velocity.extends RigidBody2D

export var touch_rect:Rect2

const SPEED = 30

var destination = null

func _integrate_forces(_state):
    if destination != null:
        var direction = (destination - global_position).normalized()
        var distance_to_player = global_position.distance_to(destination)
        linear_velocity = direction * SPEED * distance_to_player


func _input(event):
    if touch_rect.has_point(event.position):
        destination = event.position
answered Mar 1 by dulvui (31 points)

This "solution" has one tiny drawback though: the paddle will move with different speeds depending on the distance towards the destination. See my answer over here for a solution without that problem (it's for a KinematicBody2D though).

This is interesting. I think I can learn from this.

Yes you're right. Thanks for your solution!
I'll try your solution too, maybe it's perfect to create the CPU, because with my movement the CPU doesn't play well because if the speed problem you mentioned.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.