I am using OnCollisionEnter2D(Collision2D hitObject) and then use hitObject.GetContact().point.x and y to get locations of the contacts. Is it normal that these location doesn’t match exactly against visual?
In the attached image, the white dots are location of GetContact. How do i get the locations of circles in red?
Am I misunderstanding something?
Thank you.
Below is the code i use to test
public class Collision2DGetContactTest : MonoBehaviour
{
public GameObject whiteDot;
private void OnCollisionEnter2D(Collision2D hitObject)
{
for (int i=0; i < hitObject.contactCount; i++)
Instantiate(whiteDot, new Vector3(hitObject.GetContact(i).point.x, hitObject.GetContact(i).point.y, 0), new Quaternion(0, 0, 0, 0));
}
}
Those contacts come from Box2D but they are the locations of where the physics engine will apply a force to keep things separated. They are not guaranteed to be specific surface intersection points as they are not for that. They can be inside one of the colliders for instance.
That said, they follow a fairly simply pattern so two intersecting boxes would be on the surfaces of those boxes.
Note that you can go into “Project Settings > Physics 2D > Gizmos > Show Collider Contacts” to see the contact points and arrows indicating the contact normals. This’ll save you having to draw them yourself using instantiated objects. See here (scroll down to “Gizmos”). It’s not clear if that arrow in your image is this, maybe so.
Below link is the sample project of two 2D colliders touching each other that I want to get the locations where they intersect. My ultimate goal is a 2D shooting game where I want to spawn particles at the location of impact when a laser (2DColliderA) hitting enemies (2DColliderB). My approach is to figure the locations of the colliders cover lapping so i can calculate a proper location. Hopefully i am in the correct direction trying to do this.
I was just telling you that you’re using it wrong is all. It seems like you’re using it to indicate “no rotation”. No rotation is Quaternion.identity and not “new Quaternion(0, 0, 0, 0)” which is a degenerate/unnormalized Quaternion. Identity has the W component as 1 in the same way an identity matrix isn’t all zeros.
So I took a look at your project and can confirm that the contact points are correct. As I said above, contact points are where forces are applied, they are not there for information of edge intersection for overlaps because they physics system would’t need this. . It sounds to me like you’re using physics to calculate polygon clipping i.e. something like Clipper.
I don’t understand the impact part of your requirement. Typically impacts are on the surfaces of colliders; you get this information. You’ve got a situation where you’re extremely overlapped.
FYI: The exact code that produces the above (Polygon/Polygon collision) is here.
If those boxes were just touching then the contact points are on the edges touching or at least close. As they overlap, the contact points move inbetween both shapes. This is where the impulses would be applied to both bodies, in opposite directions along the collision normal.
If you move one box with respective to another you can see these changing.