0 votes

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 setfixedprocess in base RigidBody2D"

in Engine by (15 points)

1 Answer

0 votes

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 linearvelocity every frame or even very often. If you need to directly affect the body’s state, use _integrateforces, which allows you to directly access the physics state.

And the description for integrateforces:

Allows you to read and safely modify the simulation state for the object. Use this instead of physicsprocess 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.

by (20,895 points)

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

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

thank you, it solved the problem.

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.

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.

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

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.

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.