Responding to collision

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Neomex
Posts: 2
Joined: Sat May 05, 2012 9:25 pm

Responding to collision

Post by Neomex »

What is the easiest way to respond to collision between terrain and object? What exactly do I have to pass as delegate?
And is it possibile to check collisions without an event system? I hate events so much... :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Responding to collision

Post by Norbo »

What is the easiest way to respond to collision between terrain and object? What exactly do I have to pass as delegate?
Sorry, I'm not sure what you mean. If you mean regular old dynamic collision response, then all you need to do is add a dynamic entity to the space along with the terrain and they'll collide and respond to collisions. Check out the BEPUphysicsDemos in the main source download for more examples; the TerrainDemo specifically might help.
And is it possibile to check collisions without an event system? I hate events so much... :)
Yup. Every Collidable has a Pairs list which contains every CollidablePairHandler associated with that Collidable. A CollidablePairHandler is created between two Collidables when their bounding boxes overlap. Each CollidablePairHandler has a Contacts list. If there exists a contact with nonnegative penetration depth in that list, then the pair is colliding.

An Entity is not a Collidable, but the entity's CollisionInformation property returns an EntityCollidable associated with the entity. That EntityCollidable acts as the entity's collision proxy in the collision pipeline. So, the pairs associated with an entity can be found in the entity.CollisionInformation.Pairs list.

The in-development v1.2 also adds some convenience properties to the CollidablePairHandler to make it easier to identify objects in a pair (CollidableA, CollidableB, EntityA, EntityB). In v1.1, the BroadPhaseEntry objects in the pair's overlap must be analyzed to determine what the objects are.
Neomex
Posts: 2
Joined: Sat May 05, 2012 9:25 pm

Re: Responding to collision

Post by Neomex »

Should it look like this?

Code: Select all

CollidablePairHandler a = sphere.CollisionInformation.Pairs.FirstOrDefault();
if( a.Contacts.FirstOrDefault().Contact.PenetrationDepth >= 0 )
{ }
It throws null reference exception on if statement.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Responding to collision

Post by Norbo »

FirstOrDefault is a Linq extension method which returns the first element in the enumerable, or the default value for the type if no elements exist. So, if the pairs list has no entries, 'a' will be null because the default value of the CollidablePairHandler is null (it is a reference type). Attempting to use the null pair will throw an exception.

Similarly, the Contacts.FirstOrDefault method will return a default value struct if there are no contacts. Attempting to read the Contact out of that default struct will result in a null contact. Attempting to read the penetration depth from a null contact will throw an exception.

Iterating through the list with a regular for loop is probably easier to handle/more direct if you're not familiar with the fancier extension methods.
Post Reply