How can I find the point where two 3D objects collide?

I am trying to find the point where two 3D objects intersect. I am currently using the following, but the point of contact does not appear in the right place because closestPoint tends to the centre of ModelB instead of the point where they intersect.

Is there any way to get this point?

       // Bounds
        var ModelA_Bounds = ModelA_Collider.bounds;
        var ModelB_Bounds = ModelB_Collider.bounds;

        // check if the bounds intersect
        if (ModelB_Bounds.Intersects(ModelA_Bounds))
        {
            // get the closest point between the two objects
            Vector3 closestPoint = ModelA_Collider.ClosestPointOnBounds(ModelB_Bounds.center);

            // check if the closest point is inside the other collider
            if (ModelA_Collider.bounds.Contains(closestPoint))
            {
                Debug.Log(ā€˜The closest point is inside the other collider’);
            }

Why not use the built in collision methods and check the contact points?’

2 Likes

Thanks for your answer! My problem is that my two objects are static, so it seems that the collision is not detected without the objects moving.

Two static objects cant collide. Atleast one needs a rigidbody and is dynamic

edit: is this some kind of editor time thing you are trying todo?

I am trying to get the point where two objects, which do not move, intersect each other. At run time.
For example, to know if two walls in my scene intersect at some point, I want to know that point.

If you just need to test where a stick intersects a rectangle then you can do a raycast along the length of the stick. Although keep in mind that the stick could potentially overlap a corner of the rectangle with only the middle of the stick being inside the rectangle. This means there would be two intersecting points and so you’ll need to do two raycasts from either end of the stick.

But if you want to want to get the overlap point of two rectangles of varying sizes then it’s not possible as there isn’t a single point, but rather an area of overlap.

If you’re testing for overlap with the goal of depenetrating the objects then you can use Physics.ComputePenetration.

There’s a solution for that:

  1. Add Rigidbodies to both objects
  2. Configure ā€œIs Kinematic = trueā€ in both Rigidbodies. Now they are static and don’t move.
  3. In Project Settings > Physics, configure ā€œContact Pairs Mode = Enable All Contact Pairsā€.

Now the collisions work normally as with dynamic rigidbodies, and you can use the standard methods to retrieve all the contact information: OnCollisionStay, Collision.GetContacts, etc.

1 Like

@Edy Hello, thank you for your reply.
I have tried what you told me, and I am creating some dots in the contacts. As you can see in the image I can’t get the contact points to be precise.

@zulo3d Hello, thank you very much for your help.
I’ve tried the method that you tell me (ComputePenetration), because I’m trying to find the insertion point between two walls.
This method doesn’t return me the exact point, it only gives me the information of how much I would have to move the object to separate the two objects and in which direction.

Unfortunately that’s the way the internal physics engine resolves this kind of contacts. It’s designed to figure out the optimal way to de-penetrate the elements, not to calculate precise intersections between them.

You may calculate the intersection yourself using third-party tools. For example, this is a Computational Geometry library for Unity:

Using the methods available in this library you might be able to get the precise contact points. I don’t know the details, as I’m not familiar with this library, but it looks a good starting point to me.