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ā);
}
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.
Configure āIs Kinematic = trueā in both Rigidbodies. Now they are static and donāt move.
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.
@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.