Page 1 of 1

Finding the correct direction of the surface normal?

Posted: Tue Apr 26, 2011 10:37 pm
by Spankenstein
I'm firing a projectile sphere at a triangle surface and I wish to know the normal of the collision based on the triangle but inside the event attached to the sphere.

If I find the contact normal in this event then it can often be in the opposite direction in relation to the triangle. Should I detect which of the two entities in the pair is the same as the one for which the event is attached and swap the direction of the normal as appropriate?

Code: Select all

        protected void Events_InitialCollisionDetected(EntityCollidable info, BroadPhaseEntry entry, INarrowPhasePair pair)
        {
            CollidablePairHandler collidablePair = pair as CollidablePairHandler;

            if (collidablePair == null)
            {
                return;
            }

            Vector3 normal = collidablePair.Contacts[0].Contact.Normal;
            Vector3 position = collidablePair.Contacts[0].Contact.Position;

            EntityCollidable a = pair.BroadPhaseOverlap.EntryA as EntityCollidable;
            EntityCollidable b = pair.BroadPhaseOverlap.EntryB as EntityCollidable;

            if (a.Entity == this.Entity)
            {
		normal = -normal;   // Surface normal for triangle
            }
        }


Or is there a better way to approach this?

Re: Finding the correct direction of the surface normal?

Posted: Tue Apr 26, 2011 10:48 pm
by Norbo
If you need to consider the normal to be relative to an arbitrary object in a pair, then yes, you'll need to calibrate it to fit your needs. For reference, the Contact object's own normal should not be reversed; it would cause the collision to fail.

Not all pair handlers necessarily have to point their normals the same way (i.e. convex-triangles point from convex to triangle, but other pairs may point A to B or B to A so long as they are internally consistent).

Re: Finding the correct direction of the surface normal?

Posted: Tue Apr 26, 2011 10:57 pm
by Spankenstein
Not all pair handlers necessarily have to point their normals the same way
Are you saying there is there no rule of thumb such as "Reverse surface normal if the event for this entity has this entity as EntryA?"

Code: Select all

EntityCollidable a = pair.BroadPhaseOverlap.EntryA as EntityCollidable;

            if (a.Entity == this.Entity)
            {
                       normal = -normal; 
            }

Re: Finding the correct direction of the surface normal?

Posted: Tue Apr 26, 2011 11:13 pm
by Norbo
Are you saying there is there no rule of thumb such as "Reverse surface normal if the event for this entity has this entity as EntryA?"
Correct. There is nothing in the engine than enforces a rule like that. The BroadPhase determines the order of the entries, and it can go either way. Each pair handles it consistently internally. In the latest development version (available on the codeplex development fork), the CollidablePairHandler has CollidableA and CollidableB (and EntityA and EntityB) properties. Those are grabbed from the internally consistent representation, so you can know which way the normal is pointing if you know how that pair type handles it. I may try to ensure consistency in the future, though I am working on some higher priority things at the moment. It can be done with some modifications to the initialization process for the involved pair types.

You can also calibrate the normal without dealing with all that design stuff. If you define an entity to be the 'source' (i.e. the normal should be pointing from the source to the other collidable), you can do a little math on the contact normal/position to calibrate it if you know the object is convex. If the offset from the source world transform position to the contact position dotted against the normal is negative, reverse it. In other words;

Code: Select all

if (Vector3.Dot(contact.Position - someSourceConvexEntityCollidable.WorldTransform.Position, contact.Normal) < 0)
    contact.Normal = -contact.Normal;

Re: Finding the correct direction of the surface normal?

Posted: Tue Apr 26, 2011 11:16 pm
by Spankenstein
Thanks for the clarification Norbo. I'll use the calibration method in the meantime :)