Godot Version
Godot 4.7
Question
I have a scene with a rigidbody2d ('Ball' and a animatablebody2d(Launchpad). I want to be able to teleport the ball to just above the launchpad with one key, or teleport the launchpad just below the ball.
extends Node2D
var loaded = false
#if the ball isn't loaded, I load it above the launchpad
#or if up has been pressed
func _physics_process(_delta: float) -> void:
if loaded == false or Input.is_action_just_pressed("ui_up"):
var spot = $AnimatableBody2D.transform.origin + Vector2(0,-40)
#to avoid fighting the rigidbody2d physics when teleporting the ball:
PhysicsServer2D.body_set_state(
$ball.get_rid(),
PhysicsServer2D.BODY_STATE_TRANSFORM,
Transform2D.IDENTITY.translated(spot)
)
loaded = true
#if down is pressed, I move the launchpad
if Input.is_action_just_pressed("ui_down"):
$AnimatableBody2D.global_position = $ball.global_position+Vector2(0,40)
#Launchpad doesn't mind being teleported it seems.
The code works fine and does what I want it to, until I enable bounce on the ball. Bounce works fine so long as I don’t move my launchpad, but, if I teleport my launchpad higher, the ball bounces waaay to high. When teleporting the launchpad lower, the ball will sink into/through the launchpad. I have the same problem if I use a characterbody2d for the launchpad but not if I use staticbody2D. My best guess is its related to how animatablebody2d&characterbody2d’s positions are accessed for bounce, but I’m not closer to resolving it without redesigning my game. I’ve tried using .translate(offset) on the Launchpad, as well as the PhysicsServer2D.body_set_state code used on the ball with no success. Any help would be appreciated!
Scene Tree:
2d node
ball(rigidbody2d)
collision
animatablebody2d(launchpad)
collision