Setting position of a RigidBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By timoschwarzer
:warning: Old Version Published before Godot 3 was released.

Hi there!

I’m trying to set the position (to restore all elements back to their original position when the player dies) of a RigidBody2D with RigidBody2D::set_pos but unfortunately, it don’t doesn’t change the position.
Is there any other way than set_pos()?

Best regards,
Timo

(Not an answer, since its a workaround), but if you make the level a scene, and when the player dies, delete and reinstance the level node, you won’t have to return everything to original values.

Bojidar Marinov | 2016-03-31 12:21

BTW, are you using _process to update the positions?

Bojidar Marinov | 2016-03-31 12:23

I am using a save-anywhere mechanism, so I’m saving position and velocity of every object when saving the game. And no, I’m using a function which is called at the and of an animation played in an AnimationPlayer.

timoschwarzer | 2016-03-31 13:03

And btw doesn’t reinstancing the scene take loading time?

timoschwarzer | 2016-03-31 17:06

:bust_in_silhouette: Reply From: batmanasb

try set_global_pos()

:bust_in_silhouette: Reply From: Fales

Please make sure to always call set_pos and set_global_pos inside a fixed process for physics objects, even when resetting them. See the docs.

This is a very common pitfall when dealing with Godot physics. It tricked me more than once.

Example:

var should_die = false

func _ready():
    set_fixed_process(true)

func reset():
    should_reset = false
    set_global_pos(Vector2(0,0))

func _fixed_process(delta):
    if should_reset:
        # CORRECT
        # Will always work
        reset()

        # WRONG
        # Might not work
        # same goes if called from _process or input signal/_input callback
        call_deferred("reset") 

EDIT: The same apply for set_linear_velocity set_angular_velocity. Setter and getters alike (you might get incorrect values from getters outside of the fixed process).

:bust_in_silhouette: Reply From: skrx

When I try to set the position or rotation of a Rigidbody2D in the _physics_process function in Godot 3.0, the body immediately jumps back to its previous position.

It only works in the _integrate_forces function where I check if a reset variable was set to true and then I reset the body in this way:

func _integrate_forces(state):
	if reset:
		state.transform = Transform2D(start_angle, start_pos)
		state.linear_velocity = Vector2()
		reset = false

How do i move the rigidbody to a position2d using this?

Yoseph | 2019-08-13 08:56