The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

–1 vote

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.
set
contactmonitor(true)
set
maxcontactsreported(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
Godot version 3.5
in Engine by (13 points)

Could you reformat your question to make it readable?

Please log in or register to answer this question.

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.