0 votes
func _on_RespawnBallTimer_timeout():
    print ("We left the screen for too long, respawning at ", spawn_position)
    set_mode(RigidBody.MODE_STATIC)
    collision_shape.disabled = true
    position = spawn_position
    set_mode(RigidBody.MODE_RIGID)
    collision_shape.disabled = false

The above does not work. In the console:

Ball detected outside play area. Starting Respawn Timer
Ball is respawning!
We left the screen for too long, respawning at (512, 300)

It stays top left of the screen. The intended coordinates should be correct given where I originally placed the ball but it does not appear to be going there. Disabling the setmode back to rigid did not appear to succeed either, it just stopped collisions altogether (which makes sense since if I'm not mistaken static bodies act "idle" and won't trigger collisions themselves)

What is the best practice to respawn a rigid body 2d object by specifying specific locations?

An alternative route was to simply free and re-create the object at a set location, the results were the same, the object doesn't move from it's original scene position (0,0):

var new_ball = ball_res.instance()
var ball_pos = Vector2(512,300)
new_ball.position = ball_pos
add_child(ball_res.instance())
in Engine by (43 points)
edited by

1 Answer

+1 vote
Best answer

I kept digging figuring there was some way of doing this that wouldn't be mind numbingly difficult and it turns out someone provided a response for "teleportation" which is technically what this would be.

Answer in question:
https://godotengine.org/qa/16627/how-to-teleport-a-rigidbody2d

For my specific use, I'm re-spawning a simple pong style ball so the following sufficed:

onready var spawn_position = get_viewport_rect().size / 2
onready var reset_position = true
func _integrate_forces(state):
    state.linear_velocity = state.linear_velocity.clamped(max_speed)
    if reset_position:
        linear_velocity = Vector2()
        state.transform.origin = spawn_position
        reset_position = false
        print ("Position has been reset.  Check game")

func _on_RespawnBallTimer_timeout():
    print ("We left the screen for too long, respawning at ", spawn_position)
    reset_position = true
by (43 points)
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.