# HOW TO MAKE A RIGIDBODY STICK ON A ROTATING KINEMATICBODY AT THE POINT OF COLLISION OR CONTACT

Attention Topic was automatically imported from the old Question2Answer platform.

AM CREATING A GAME WHERE RIGID-BODY FIRED SHOULD STICK AT THE POINT OF COLLISION OR CONTACT WITH A KINEMATIC-BODY ROTATING IN THE CENTER WITH THE RIGID-BODY ROTATING WITH THE KINEMATIC-BODY

SO FAR THE CODES I HAVE HERE MAKES THE RIGID-BODY STICK AT THE CENTER OF THE ROTATING KINEMATIC-BODY INSTEAD OF THE POINT OF COLLISION OR CONTACT

# Get the rigid body on which the ball will stick

body_on_which_sticked = body_state.get_contact_collider_object(0)

# from the world coordinate system to the ball coordinate system

var tr_ci_world_to_ball = get_global_transform()

# from the world cs to the collider cs

var tr_ci_world_to_collider = body_on_which_sticked.get_global_transform()

# collider->ball = collider->world then world->ball = inverse(world->collider) then world->ball

tr_ci_collider_to_ball = tr_ci_world_to_collider.inverse() * tr_ci_world_to_ball

# In other words: “world->collider (at latest news), and then, collider->ball (like at the collision instant)”.

global_transform = body_on_which_sticked.get_global_transform() * tr_ci_collider_to_ball

extends RigidBody2D

var is_sticking = false

# rigid body on which the ball will stick

var body_on_which_sticked

# (used when sticked) Transform (tr) at the collision instant (ci) from the collider to the ball

var tr_ci_collider_to_ball = Transform2D()

# Enable the logging of 5 collisions.
set_contact_monitor(true)
set_max_contacts_reported(5)

``````# Apply Godot physics at first
set_use_custom_integrator(false)
``````

func _integrate_forces( body_state ):

``````# We turn on the "sticking mode" as soon as the ball collides
if is_sticking == false && body_state.get_contact_count() == 1 :
is_sticking = true

# Ignore Godot physics once the ball sticks
set_use_custom_integrator(true)

# Get the rigid body on which the ball will stick
body_on_which_sticked = body_state.get_contact_collider_object(0)

# For debug, check on which we are sticking
print("The ball is sticking on a ", body_on_which_sticked.get_name())

# Some transforms (tr) at the collision instant (ci)

var tr_ci_world_to_ball = get_global_transform() # from the world coordinate system to the ball coordinate system

var tr_ci_world_to_collider = body_on_which_sticked.get_global_transform() # from the world cs to the collider cs

tr_ci_collider_to_ball = tr_ci_world_to_collider.inverse() * tr_ci_world_to_ball
``````

# Because: collider->ball = collider->world then world->ball = inverse(world->collider) then world->ball

``````# Behavior when sticking

if is_sticking :

# We take the last transform of the moving collider, and we keep the same relative position of the ball to the collider it had at the collision instant.

# In other words: "world->collider (at latest news), and then, collider->ball (like at the collision instant)".

global_transform = body_on_which_sticked.get_global_transform() * tr_ci_collider_to_ball
``````