How to correctly handle the repositioning of a RigidBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BlueCheese

I’m making a very basic Billiards game as my first Godot project. I’ve managed to create a basic play area of “table”, “pockets”, and “cushions” and the various balls all bounce around as expected.

The issue I’m facing is when I try to design some functionality around the “pocketing” of the balls.
i.e. when a Ball collides with a Pocket, it should momentarily disappear from the screen. Then, once all of the balls have stopped moving, any pocketed balls should be replaced on their starting position.

The way that I’ve coded it up does not work, however, because the Ball appears to be re-positioned (for what seems to be a single frame) to the correct spot, it instantly hops back to the Pocket in which is had been “sunk”.

I believe this is probably related to the ordering of physics events, and I need to defer setting the Position property until after all the “physics” has happened but I feel like I may be going about this the wrong way to begin with.

Any help would be much appreciated.

enter image description here

:bust_in_silhouette: Reply From: SQBX

Add this function in your RigidBody2D script:

func move_to(location):
    set_mode(MODE_STATIC)
    position = location
    set_mode(MODE_RIGID)

You can call it whenever you need to set the RigidBody2D’s position:

move_to(wherever_you_want_to_move)

Thanks for the suggestion, unfortunately this has not solved the problem.
I converted your suggestion to the C# equivalent (below) but I still see the same results: The ball is moved back to the correct position for what seems like one frame of animation, and then is immediately moved back to a location on top of the “pocket”.

internal void Reset()
{
	Stop();
	GD.Print($"Setting {this} Position to {StartPosition}");
	Mode = ModeEnum.Static;
	Position = StartPosition;
	Mode = ModeEnum.Rigid;
	//SetDeferred("position", StartPosition);
	GD.Print($"{this} Position: {Position}");
	Visible = true;
	IsPocketed = false;
}

BlueCheese | 2023-01-09 15:44