+1 vote

in godot 2 it was possible to change position of a rigidbody2d inside other nodes script using RigidBody2D.set_pos() but in godot 3 set_pos() not works because of interferes with the physics simulation...
the other way is changing Transform2D.origin using state.set_transform(Transform2D) inside _integrate_forces(state) and the problem is you can't get a rigidbody's state outside of _integrate_forces(state). is there any function to get state of other objects? or is it possible to change a rigidbody's position without using state.set_transform()?

in Engine by (15 points)

1 Answer

+4 votes

You're confusing separate issues. set_pos() was not removed, it was changed to use the position member variable. All Node2D-derived nodes can now use

position = Vector2(x, y)

to set position.

RigidBody2D behavior has not changed. It was never a good idea to set the position directly.

Why not send a signal and/or call a function on the target body to tell it you want it to teleport, and then it does so in its integrate_forces() call?

by (21,920 points)

i know pos changed to position and i used rigidbody.position=vector2(x,y) but it changes the position for only one frame and then it returns. i tried changing a variable of the target body as a signal and it worked fine, thank you.

Thanks for mentioning this issue.
I had the very same situation:
"i used rigidbody.position=vector2(x,y) but it changes the position for only one frame and then it returns."

I simply created a new instance from my object to the new position.
I am Noob. :)

That's because as the RigidBody documentation explicitly says, you can't set its position directly and expect it to work.

See http://docs.godotengine.org/en/latest/tutorials/physics/physics_introduction.html for an introduction on how to work with RigidBody2D correctly.

I got it now.
Thank you for that video KidCanCode.

Thanks. Is there any best practice to move the RigidBody2d to a specific position within 1 frame.? In short:
I am looking for a way to Teleport rigidbody2d.

To safely and reliably teleport a rigid body, you have to use the Physics2DDirectBodyState. I have an example with explanation here: http://kidscancode.org/blog/2017/12/godot3_kyn_rigidbody1/

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 Frequently asked questions and 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 [email protected] with your username.