# points of contact between collision and different planes

Hi,

I cannot correct this code which should determine 2 points of contact: the 1st between collision (horizontal plane) and object[0] and the 2nd between collision and objects[1]
objects are vertical planes intersecting collision

``````using System.Collections.Generic;
using UnityEngine;

{

public GameObject point;

GameObject monObjet;

List<GameObject> ListPoints = new List<GameObject>();
Vector3 echelle_plan;

void Start()
{ //objet auquel ce script est attachĂ©
monObjet = gameObject;

echelle_plan = monObjet.GetComponent<Transform>().localScale;

}

void OnCollisionEnter(Collision col)
{
// VĂ©rifiez le tag de l'objet avec lequel cet objet entre en collision
if (col.gameObject.CompareTag("plan1"))
{
// AccĂ©dez au premier point de contact avec Objet1 s'il y en a
if (col.contacts.Length > 0)
{
ContactPoint firstContact = col.contacts[0];
Vector3 contactPoint = firstContact.point;
GameObject newPoint = Instantiate(point);
newPoint.transform.position = contactPoint;
Debug.Log("Premier point de contact avec plan1 : " + contactPoint);

}
else
{
Debug.Log("Aucun point de contact avec plan1.");
}
}
else if (col.gameObject.CompareTag("plan2"))
{
// AccĂ©dez au premier point de contact avec Objet2 s'il y en a
if (col.contacts.Length > 0)
{
ContactPoint firstContact = col.contacts[0];
Vector3 contactPoint = firstContact.point;
GameObject newPoint = Instantiate(point);
newPoint.transform.position = contactPoint;
Debug.Log("Premier point de contact avec plan2 : " + contactPoint);

}
else
{
Debug.Log("Aucun point de contact avec plan2.");
}
}
}

}
``````

The y scale of each plane is 0.1f. The other scales (scale_x and scale_z are at 1)
Sphere has a SphereCollider
All objects have a Rigidbody with constraints checked and UseGravity unchecked

The result obtained after one play is disastrous!

I expect this result:

the expected contact points are framed in black

thanks

What is that game going to be?

If itâs a game played on a discrete grid like â4 winsâ then that doesnât need physics.

1 Like

Also donât misinterpret âcontact pointsâ to strictly mean points of intersection on the boundary of colliders. Contact points are where the physics system will apply impulses to separate the colliders in question; itâs not there as general purpose intersection library.

This is why contact points are not always on the boundary of colliders.

1 Like

what should I use then?

Hard to tell, I donât understand what is intersecting what. You describe âobjectsâ and âplanesâ only.

There are plenty of intersection algorithms out there, you donât need a game physics for a simple A vs B intersection test.

1 Like

Right. Mathematically two planes do not have intersection points but the intersection between two planes is always a line. 3 planes can intersect in a single point, but donât have to. As it was already mentioned, a physics system is not an intersection library and âcollision contactsâ are due to a movement in a certain direction which results in a collision and the physics system will apply forces at those points to separate potential overlaps in the opposite direction of the movement that caused the collision.

Note that you used the term âplaneâ but you rather talk about a mesh / mesh collider which does not necessarily represent a flat finite rectangular plane. Mathematically planes are infinite and do not have any boundary. If you want to determine the intersection points of your rectangular planes at the bounding lines, you can intersect each of the 4 bounding lines with the other plane. This gives you 4 intersection points. Since both planes seems to be finite, the calculated intersection point may be outside the boundary of the other plane, so that would need to be checked as well.

You can use Unityâs Plane struct to do the actual intersection. As for checking of a point is actually inside / on the plane boundary, it depends on how you defined those planes in the first place. Usually the easiest solution is to use InverseTransformPoint to convert a worldspace point into local space and check against the local boundaries which are usually easier to define as rotations and scaling would be eliminated this way and it becomes essentially just an axis aligned 2d problem which is trivial. Though as others have said, it depends on what the goal is. Whatâs the actual input? What things can change and in which way?

2 Likes