How to use _integrate_forces to change rigidbody position?

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

I’m trying to have my character equip a rock, which visually it does but it actually is on the on the ground. When i try to throw it, it starts form the ground not the hand…i searched about this and people said you have to use _integrate_forces() to move a rigidbody but i don’t know how to make it work.
Anyone who has ideas on how to position my RigidBody2Drock on my characters Position2Dhand…please help.

This doesn’t really answer your question, but why don’t you just delete the RigidBody from the ground and then add a KinematicBody2D version of the rock in the character’s hands? When you want to throw the rock, you can either swap it with the RigidBody version again or use move_and_slide(). So the whole sequence is broken down into 3 parts:

  1. Pick up the rock. The rock disappears from the ground and a KinematicBody2D version of the rock appears in your character’s hands,

  2. When about to throw the rock, delete the KinematicBody2D and spawn a RigidBody rock in its place.

  3. Apply forces to the RigidBody rock so that it moves forward or whatever.

johnygames | 2019-08-13 12:51

How do you spawn things in one’s place?

Yoseph | 2019-08-13 15:23

:bust_in_silhouette: Reply From: kidscancode

You need to remove the rigid body from physics processing while it’s being held. When picked up, change its mode to MODE_STATIC.

When you want to drop it, change the mode back to MODE_RIGID. Note that when you do this it will be in the sleeping state, so you must apply some force or impulse.

You can see an example of this using mouse drag and drop here:

The VR tutorial in the docs also does this to handle picking up/throwing objects:

Okay…i can now pick the ball and throw it but…my character is glitching…like he is controlled by the rock…whenever i run his hands swing back and forth i.e the stone,with this the character himself goes back and forth…any solutions?
P.S i think it has something to do with the global_transform.origin
And thanks for the help.

Yoseph | 2019-08-13 17:21

You’re probably colliding with it. Set your collision layers/masks so that the player and rock don’t collide or set collision_layer = 0 on pickup (and remember to set it back when you drop).

kidscancode | 2019-08-13 17:28

Thanks man…It worked.

Yoseph | 2019-08-13 17:39