set_position() being ignored on a RigidBody2D node

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

I am using Godot 3.0 and am using this statement in the script:
plane.set_position(Vector2(rand_range(0, 314), rand_range(0, -300)))
When I click on the plane it registers an increment to the score but does not move the plane to the random x and y positions. Any ideas?

:bust_in_silhouette: Reply From: KoBeWi

In Godot 3, instead of set_position(), use position =

plane.position = Vector2(randrange(0, 314), rand_range(0, -300))

I tried this and it still does not work. The script is attached to a parent Node2D. I called the RigidBody2D player and it has child nodes Sprite and a TextureButton node. I than attached a script to the RigidBody2D node with this statement position = Vector2D(rand_range(0, 314), rand_range(0, -300)).

Talsac | 2018-03-16 16:10

:bust_in_silhouette: Reply From: zdimaria

I believe you need to make a custom integrate_forces function to do this.

Called during physics processing, allowing you to read and safely modify the simulation state for the object. By default it works in addition to the usual physics behavior, but set_use_custom_integrator allows you to disable the default behavior and do fully custom force integration for a body.

:bust_in_silhouette: Reply From: eons

Rigid bodies are not meant to be moved manually but by the physics engine, the node position will be reset by the engine to the correct values according the data of the body physics state.

If you need to “teleport” a rigid body (which should be an exception), you can modify the transform’s origin of the body state.

The callback _integrate_forces gives you easy access to the state and can be used for that kind of operations (you can get the state via Physics2DServer too).

I wanted to pick up a rock(RigidBody2D)…i tried using the position property to place it my my characters hand ,but doesn’t work…How could i implement _integrate_forces on this?

Yoseph | 2019-08-13 06:29

:bust_in_silhouette: Reply From: Gregzenegair

Hi,

The best way I found was to get rid of the object and to rebuild it entirely.

The adventage is that if I have a Node2D containing multiple RigidBody2D, I can remove the Node2D tree and reinstanciate the entire tree where I want by positionning my newly created Node2D.