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
bodyonwhichsticked = bodystate.getcontactcollider_object(0)
Some transforms (tr) at the collision instant (ci)...
from the world coordinate system to the ball coordinate system
var trciworldtoball = getglobaltransform()
from the world cs to the collider cs
var trciworldtocollider = bodyonwhichsticked.getglobal_transform()
compute the transform form the collider to the ball.
collider->ball = collider->world then world->ball = inverse(world->collider) then world->ball
trcicollidertoball = trciworldtocollider.inverse() * trciworldtoball
We take the last transform of the moving collider, and we "add" 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)".
globaltransform = bodyonwhichsticked.getglobaltransform() * trcicollidertoball
extends RigidBody2D
var is_sticking = false
rigid body on which the ball will stick
var bodyonwhich_sticked
(used when sticked) Transform (tr) at the collision instant (ci) from the collider to the ball
var trcicollidertoball = Transform2D()
func ready():
# Enable the logging of 5 collisions.
setcontactmonitor(true)
setmaxcontactsreported(5)
# Apply Godot physics at first
set_use_custom_integrator(false)
func integrateforces( 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