Page 1 of 1

Events.InitialCollisionDetected with compound bodies

Posted: Mon Jun 27, 2011 1:40 pm
by Spankenstein
I have a CompoundBody and I drop a Box inside inside it. The Box entity 'CollisionInformation.Events.InitialCollisionDetected' is hooked up to an event that detects initial contact creation.

The problem is that this event fires off more than once per frame, due to (I presume) the fact there are multiple shapes that comprise the CompoundBody.

Should this be happening?

Should I ignore subsequent triggers in the same frame if a contact for the CompoundBody Collidable has already been recorded, or should I create an average from each contact with this CompoundBody Collidable?

Also, if I am casting the pair involved in the collision as follows:

Code: Select all

            Collidable a = pair.BroadPhaseOverlap.EntryA as Collidable;
            Collidable b = pair.BroadPhaseOverlap.EntryB as Collidable;
How can I then determine which Collidable is which if there are more than one CompoundBody types in the scene?

Re: Events.InitialCollisionDetected with compound bodies

Posted: Mon Jun 27, 2011 4:17 pm
by Norbo
The multiple InitialCollisionDetected triggers are expected, because the CompoundCollidable parent re-issues the events of children. However, the the pair associated with the event will not be the top-level pair, but rather a pair between a child collidable and the other object. If you want only one notification, then you can look to see if the pair includes the top-level compound collidable.
How can I then determine which Collidable is which if there are more than one Box types in the scene and more than one CompoundBody types in the scene?
The BroadPhaseEntry class, which is the parent of Collidable, has a Tag which can be used to easily identify or link information in collision events. You can set the tag of compound children, if necessary, by using the CompoundChildData based constructor (or setting them after the fact).

I hope I didn't make any mistakes in this post, I'm in a rush at the moment- I'll be back later to double check :)

Re: Events.InitialCollisionDetected with compound bodies

Posted: Thu Jun 30, 2011 2:52 pm
by Spankenstein
So can I deal with any type of object in the physics space by using 'BroadPhaseEntry'?

Code: Select all

            CollidablePairHandler collidablePair = pair as CollidablePairHandler;

            if (collidablePair == null)
            {
                return;
            }

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

            //Collidable a = pair.BroadPhaseOverlap.EntryA as Collidable;
            //Collidable b = pair.BroadPhaseOverlap.EntryB as Collidable;
I can't cast to a CompoundBody or CompoundChild though:

Code: Select all

            if (a is CompoundBody || b is CompoundChild)
            {

            }
because they both don't derive from BroadPhaseEntry :(

If I set a CompoundBody.Tag to "Hello I'm a CompoundBody, my name is CBFoo"

then it is never picked up with:

Code: Select all

            if (a.Tag != null || b.Tag != null)
            {
                Console.WriteLine("This object has a tag");
            }
Where have I gone wrong?

Re: Events.InitialCollisionDetected with compound bodies

Posted: Thu Jun 30, 2011 4:18 pm
by Norbo
CompoundBody is an Entity prefab type, which just means that it's a convenience subclass of entity that allows you to see the type of its CollisionInformation property (some specific subclass of EntityCollidable) and that collidable's Shape property. The EntityConstructionDemo in the BEPUphysicsDemos project shows the relationship between all of these.

So, if you wanted to know if a broad phase entry is used by a compound body, check to see if it is a CompoundCollidable instead of a CompoundBody.
If I set a CompoundBody.Tag to "Hello I'm a CompoundBody, my name is CBFoo"

then it is never picked up
The Entity.Tag property is separate from the BroadPhaseEntry.Tag property. The Entity is not a BroadPhaseEntry, but the Collidable in its CollisionInformation property is. Setting the compoundBody.CollisionInformation.Tag to something will then be visible in the collision event by checking the entry.Tag property.

Similarly, you can give tags to the compound's child Collidables. They will be of the type BroadPhaseEntry in the collision event, so you can check the tag still.

Re: Events.InitialCollisionDetected with compound bodies

Posted: Thu Jun 30, 2011 11:41 pm
by Spankenstein
That's cleared that up for me. Thank you for explaining.