- Home /
Unexpected position of contact point from sphere-sphere or sphere-capsule colliders
I need to know the shortest distance between a point and other surfaces, in any direction. The image shows my setup: the selected object represents the point, which has a sphere collider, and has a mesh that can be ignored. The other objects have their collider meshes tight to their render meshes. The yellow lines are rays from the contact points in the direction of the normals. The red line is the shortest distance from the sphere center. The spheres are Z-aligned.
I've set my collision detection to happen between a kinematic rigidbody with sphere collider (the point), and the other body has a default rigidbody. As I want the collider meshes to overlap, I don't want the physics engine to process the collision. So I've set all the six freeze flags from the second object.
I can get the contact point from the argument of OnCollisionStay, that I receive. A sample of the script:
void FixedUpdate() {
foreach (var contact in differentObjectsCollisions) {
Debug.DrawRay(contact.point, contact.normal * 3, Color.yellow);
}
lastContact = FindClosestContactPoint(differentObjectsCollisions, transform.position);
differentObjectsCollisions.Clear();
Debug.DrawLine(transform.position, lastContact.point, Color.red);
}
void OnCollisionStay(Collision other) {
differentObjectsCollisions.Add(
other.contacts.Length == 1 ?
new ContactInfo(other.gameObject, other.contacts[0].point, other.contacts[0].normal) :
FindClosestContactPoint(other, transform.position));
}
My problem happens depending on the collider of the second object. When the collider is box or mesh, the contact point is correctly on the surface of the second object even when colliders overlap. When it is sphere or capsule, the contact point appears inside the second object, when the colliders overlap. In the image, the problem is happening in the unselected sphere, as the contact point is inside it, instead of over its surface.
I expected a contact point always in the surface of the second body, but, in the unexpected cases, it appears to be in a midpoint of the overlapping volume of the two colliders.
Is this a bug or the expected behaviour?
Your answer
Follow this Question
Related Questions
Unity Colliders are Overlapping/Intersecting 2 Answers
Rigidbody gets stuck in wall after falling 0 Answers
Overlap detection 1 Answer
detection cube face while rotating 0 Answers