problem in setting position of a RigidBody2d

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

in my game when i want to reset my ball position to its default position, using body.set_position() function when user pushes R key, the position changes for a moment and then it returns to previous position when i release the key. in godot 2.1.4 everything was fine but in godot 3 i face this problem. here is my code:

extends RigidBody2D
onready var ball=get_node("/root/node2d/ball")
const ball_def_pos=Vector2(515,245)
func _set_def_pos():
func _physics_process(delta):
	if Input.is_action_pressed("restart"):

this code is attached to my character body and ball is a rigidbody, i tried to use _fixed_process(delta): instead of _physics_process(delta): but it returns “nonexistence function set_fixed_process in base RigidBody2D”

:bust_in_silhouette: Reply From: kidscancode

RigidBody2D can’t be set via position, it interferes with the physics simulation.

From the docs for RigidBody2D:

You should not change a RigidBody2D’s position or linear_velocity every frame or even very often. If you need to directly affect the body’s state, use _integrate_forces, which allows you to directly access the physics state.

And the description for _integrate_forces:

Allows you to read and safely modify the simulation state for the object. Use this instead of _physics_process if you need to directly change the body’s position or other physics properties.

For more examples and details, see this tutorial:

Also, fixed_process() was the name in 2.1, the function is named _physics_process() now.

I don’t want to change RigidBody2D’s position every frame, i want to change it only when gamer presses R key.however,if set_position not works, is there any other way to change rigidbody’s position? i tried putting function into _integrate_forces() or _process(), or putting rigidbody into sleep for one frame, none of them worked. still stuck with this problem.

lostact | 2018-01-31 22:25

As in the link, you have to use _integrate_forces() and change the position using the Physics2DDirectBodyState.transform.

kidscancode | 2018-01-31 22:35

thank you, it solved the problem.

lostact | 2018-01-31 22:41

It can be done in _physics_process by getting the body state via Physics2DServer (to change the transform) but not sure how reliable will be the result compared to the custom integrator.

eons | 2018-02-01 02:55

This is a misconception. Using _integrate_forces() just gives you safe access to the physics state. It has nothing directly to do with custom integration, which you can enable and also would be used in that function.

kidscancode | 2018-02-01 03:53

I see, thanks!
I always use it custom physics only.

eons | 2018-02-01 08:58

What about instancing the node. Then you can set the position of the rigidbody just before you add the child. I think that should also do it.

Enquest | 2018-02-28 19:56