Godot Version
4.2
Question
Is there a way to get the collision point between a RigidBody3D and another PhysicsBody3D?
4.2
Is there a way to get the collision point between a RigidBody3D and another PhysicsBody3D?
Yes. You can get the contact point(s) of a RigidBody3D
via a reference to its state (PhysicsDirectBodyState3D
).
The state of a Rigidbody3D
can be obtained either through _integrate_forces()
where the state is a parameter, or by requesting it from the physics server (PhysicsServer3D
).
Once obtained, you can retrieve contact information from the state via various methods. Here is an example:
func _integrate_forces(state):
var contact_point = state.get_contact_collider_position(0)
Note that a contact_index
is required as an input since a physics body can either be in contact with several objects at once, or in contact with the same object in multiple places.
If you wish to get the contact point for a specific object, you first have to determine which contact index relates to that object. One way of doing this is to compare the RID of your other body to that of every contact body.
var target_body_id: RID # This would be the RID of your other PhysicsBody3D
var target_contact_idx = -1 # The contact index for your target body
var contacts = state.get_contact_count()
for i in range(contacts):
var id = state.get_contact_collider(i)
if id == target_body_id:
target_contact_idx = id
break
IMPORTANT: In order to retrieve contact information, the physics body you’re using (RigidBody3D
) must have it’s contact monitor set up correctly. This means setting contact_monitor
to true
and setting max_contacts_reported
to a value above 0
.
Hopefully, this helps you tackle your problem. Let me know if there is anything you need help with.
Relevant resources: