# what does collisionEvents.Normal returns?

I’m curious about this because I need to get the face normal directions of the collider that touches my player object, which is a sphere.

I need it for the controls. So what does it returns?

does it return the direction of the contact points between entityA and entityB?

Collision detection finds the closest points on the two colliders. The normal is the direction from the point on collider B to the point on collider A. Another way to think about it is, if you want to increase the distance from A to B as fast as possible, you should move A in the direction of the normal. Mathematically, it’s the normalized gradient of the distance between the colliders as a function of their relative translation.

If your sphere is colliding with a box for example, the closest point might be on a face of the box, in which case the collision normal will be the face normal. But the closest point might be on an edge or corner of the box instead. Here is an example:

The closest points are in red and the normals are in green.
If you want a face normal, you could use ConvexHull.GetSupportingFace(). This function looks at all of the faces containing the closest point and returns the one whose normal is closest to the collision normal.

3 Likes

Hi, do you know how GetSupportingFace is supposed to be used? For my use case I’m trying to find the the face normal for a collision in an attempt to stop phantom collisions with collider edges. I thought I might be able to use this to figure that out but I keep getting face normals that are really small, I had thought that normals would have a magnitude of 1.

In particular, once I find the colliders involved, I pass the contact normal given by the ModifiableContactHeader.Normal from an IContactsJob to the respective ConvexHull.GetSupportingFace() method. The small normals I’m getting don’t work when used in the ModifyNormalsJob in https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/UnityPhysicsSamples/Assets/Demos/5.%20Modify/Scripts/ModifyNarrowphaseContactsBehaviour.cs because the dot product always returns some small number which basically results in objects falling through the colliders.

In addition, the ConvexHull type is internal to the physics package. I’ve solved this for now by modifying the source code to make it visible, however this obviously isn’t a great solution.

Any help would be appreciated.

Hi, GetSupportingFace() expects a direction in the ConvexHull’s local space and, optionally, an index into ConvexHull.Vertices. From ModifiableContactHeader, the direction should just be the normal rotated into local space (and negated if you’re getting a face from body A), but the vertex index is tricky, you might try without it to begin with. GetSupportingFace() returns an index into ConvexHull.Planes. ConvexHull.Planes[GetSupportingFace()].xyz is the face normal and should always be unit length. You can see some examples of usage in ConvexConvexManifold.cs.

Thanks, I’ve rotated the contact normal into the plane’s local space by multiplying the normal by the plane’s orientation. However, the plane isn’t actually rotated so the resultant normal is the same.

Regarding the plane normals being a unit length, when I loop over all of the Planes exposed by the ConvexHull I find that there are 2 and they both have weird normals like (0f, -4.169755E-07f, 1.485376E-43f) and (0f, 0f, -4.169755E-07f). Additionally, the values seem to change slightly each time I check. I’m not sure why there are 2, this is a plane converted by the normal physics shape authoring into a PolygonCollider. I thought it could be 2 triangles instead of a single quad but it is a single quad with 2 planes. But still, the normals should have length 1, right? Furthermore, I thought that the normals of both planes should just be (0, 1, 0) since it’s a flat plane.

Sounds strange, I think the elements of Planes should all have unit length xyz. Could be a bug, or it could be a problem on your side, eg. if you copied a collider by value then you get garbage. That’s a tricky thing about colliders, if you for example make a function like void foo(ConvexCollider c) it will not work, because the collider is passed by value.

If that’s not it could you share your code?

1 Like

It would make sense if I was getting garbage considering the weird values I’m seeing. Can I just ask if I can pass around a pointer, i.e. Collider*, by value? For example I do:

``````private unsafe bool TryFindSurfaceNormalFromConvexHull(
Collider* collider,
ColliderKey colliderKey,
float3 contactNormal,
out float3 surfaceNormal
) {
``````

I haven’t worked with pointers before this so it’s a bit new to me.

Passing pointers is fine, it’s only passing the struct itself that won’t work.

``````    private struct ModifyNormalsJob : IContactsJob {
public PhysicsWorld PhysicsWorld;
public ComponentDataFromEntity<ModifyNarrowphaseContacts> ModifyNarrophaseContacts;

private float distanceScale;

var updateNormal = TryFindSurfaceDataByConvexHull(ref contactHeader, out var surfaceNormal);
if (updateNormal) {
if (contactPoint.Index == 0) {
var newNormal = surfaceNormal;
}
contactPoint.Distance *= distanceScale;
}
}

public IndexAndKey(int bodyIndex, ColliderKey colliderKey) {
BodyIndex = bodyIndex;
ColliderKey = colliderKey;
}
}

private unsafe bool TryFindSurfaceDataByConvexHull(
out float3 surfaceNormal
) {
IndexAndKey? indexAndKey = default;
indexAndKey = new IndexAndKey(
);
indexAndKey = new IndexAndKey(
);
}

if (indexAndKey.HasValue) {
var contactNormalInColliderSpace = math.mul(
PhysicsWorld.Bodies[indexAndKey.Value.BodyIndex].WorldFromBody.rot,
);
return TryFindSurfaceNormalFromConvexHull(
PhysicsWorld.Bodies[indexAndKey.Value.BodyIndex].Collider,
indexAndKey.Value.ColliderKey,
contactNormalInColliderSpace,
out surfaceNormal
);
} else {
surfaceNormal = default;
return false;
}
}

private unsafe bool TryFindSurfaceNormalFromConvexHull(
Collider* collider,
ColliderKey colliderKey,
float3 contactNormal,
out float3 surfaceNormal
) {
switch (collider->Type) {
case ColliderType.Mesh: {
var meshCollider = (MeshCollider*) collider;
meshCollider->GetLeaf(colliderKey, out var childCollector);
return TryFindSurfaceNormalFromConvexHull(
childCollector.Collider,
colliderKey,
contactNormal,
out surfaceNormal
);
}
case ColliderType.Compound: {
var compoundCollider = (CompoundCollider*) collider;
compoundCollider->GetLeaf(colliderKey, out var childCollider);
return TryFindSurfaceNormalFromConvexHull(
childCollider.Collider,
colliderKey,
contactNormal,
out surfaceNormal
);
}
default: {
return TryFindSurfaceNormalFromConvexHull(collider, contactNormal, out surfaceNormal);
}
}
}

private static unsafe bool TryFindSurfaceNormalFromConvexHull(
Collider* collider,
float3 contactNormal,
out float3 surfaceNormal
) {
ConvexHull convexHull;
switch (collider->Type) {
case ColliderType.Convex:
convexHull = ((ConvexCollider*) collider)->ConvexHull;
break;
case ColliderType.Box:
convexHull = ((BoxCollider*) collider)->ConvexHull;
break;
case ColliderType.Cylinder:
convexHull = ((CylinderCollider*) collider)->ConvexHull;
break;
case ColliderType.Capsule:
convexHull = ((CapsuleCollider*) collider)->ConvexHull;
break;
case ColliderType.Sphere:
convexHull = ((SphereCollider*) collider)->ConvexHull;
break;
case ColliderType.Triangle:
convexHull = ((PolygonCollider*) collider)->ConvexHull;
break;
default:
surfaceNormal = default;
return false;
}

var supportingFaceIndex = convexHull.GetSupportingFace(contactNormal);
var plane = convexHull.Planes[supportingFaceIndex];

for (var planeIndex = 0; planeIndex < convexHull.NumFaces; planeIndex++) {
Debug.Log(\$"Plane {planeIndex} Normal: {convexHull.Planes[planeIndex].Normal}");
}

surfaceNormal = plane.Normal;
return true;
}
}
``````

That’s what I’m currently working with. When a dynamic entity, in this case a sphere, collides with an entity tagged with ModifyNarrowphaseContacts I am trying to modify the collision so that it uses a face normal instead of an edge normal. I think that should work based on the physics sample I linked, although I haven’t got far enough to properly test it.

I believe I’m doing the right thing with the ColliderKey here, at any rate I’m not actually hitting that code path in my test case since the plane is just a Quad rather than a Mesh or Compound collider. But in the future I’d like this to work with meshes.

Please let me know if I’m making any mistakes here.

Like Collider, ConvexHull can’t be copied by value, you have to pass around pointers/refs instead, so the copy in the last overload of TryFindSurfaceNormalFromConvexHull() is the problem.

It is safe to cast any Collider* with CollisionType.Convex to ConvexCollider*. So you can remove the switch statement and just do ConvexCollider* convexCollider = (ConvexCollider*)collider; convexCollider->ConvexHull.GetSupportingFace(); etc.

I also noticed while reading your code, you are doing contactNormalInColliderSpace = WorldFromBody.rot * contactHeader.Normal. That should be WorldFromBody.rot.inverse(), because contactHeader.Normal is already in world space, and you are trying to rotate it into body space. Also, if you are getting the surface normal on body A, you should negate the normal, since it points away from B and towards A. GetSupportingFace() returns the face whose normal is closest to the direction you pass in, and face normals point outwards from the shape, so if you are looking for a face normal from A that points towards B, you need to pass in a direction that points from A to B.

Hope that helps

Thank you so much. That was the problem, I didn’t even think about the fact that I was making a local copy of the convex hull.