Collision event help

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
nemisis83
Posts: 3
Joined: Thu Aug 25, 2011 2:22 am

Collision event help

Post by nemisis83 »

I have a triangle mesh being used for my world map and a cylinder for my player. using the box example from the getting started demo for a projectile i am trying to get an event to happen when the player collides with the projectile. lets say when the player collides with a projectile subtract 1 from a float for player health. i have added a collision event to the player entity using the line Player.CollisionInformation.Events.PairCreated += Events_PairCreated; and the event handler being

void Events_PairCreated(EntityCollidable sender, BEPUphysics.BroadPhaseSystems.BroadPhaseEntry other, INarrowPhasePair pair)
{
//playerHealth -= 1;
//throw new NotImplementedException();
var otherEntityInformation = other as EntityCollidable;
if (otherEntityInformation != null)
{
playerHealth -= 1;
}
}

the problem i run into is the player loses health no matter what the projectile collides with. are there any tutorials that may explain collision events a little more in detail than http://bepuphysics.codeplex.com/wikipag ... umentation. do i need to add a collision rule and if so how would i do this? i have read through http://bepuphysics.codeplex.com/wikipag ... umentation but im having trouble understanding this. if some one could point me in the right direction would be great.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Collision event help

Post by Norbo »

Collision rules govern whether or not given stages of collision detection and response should occur. They do not directly affect events. Events occur in any collision detection stage that the collision rules allow the pair to progress to.

So, instead of always subtracting, first check to see if the thing that you collided with is something that should hurt you- like a projectile. Alternatively, consider putting the event handler on the projectile. When the projectile hits something, determine if the thing that it hit was a player. If so, subtract HP. The projectile-centric method may be more direct if not many things hurt the player on impact.

From an event handler, you can determine gameplay information about a broad phase entry via its Tag. Put some gameplay object containing the relevant information in the Tag property of the Projectile's CollisionInformation or the player's CollisionInformation. You will have to cast the object to your gameplay type, since the Tag property returns an 'object.'

Note that an Entity has a Tag as well, but it's usually harder to get at than the BroadPhaseEntry passed directly into the event handler. That's why the BroadPhaseEntry has a Tag too. The entity.CollisionInformation returns an EntityCollidable which is a BroadPhaseEntry, which has a Tag. The Entity's Tag and the entity.CollisionInformation.Tag are separate.

For some general information, here's a description of how the common objects relate to each other.
-Entity covers all objects with position and velocity. Dynamic (respond to collisions) and kinematic (do not respond to collisions) entities exist. The Entity class itself manages the motion and dynamics, not the collision.
-BroadPhaseEntry lives in the collision detection pipeline, starting in the BroadPhase. The BroadPhase creates BroadPhaseOverlaps between BroadPhaseEntries.
-Collidable inherits from BroadPhaseEntry. Collidables are objects which generally create contact points with other objects during collisions.
-StaticMesh, Terrain, and InstancedMesh inherit from Collidable.
-EntityCollidable inherits from Collidable.
-Entity does not inherit from EntityCollidable. Entity has no parent class (other than Object). Entity has an EntityCollidable in the entity.CollisionInformation property. The entity.CollisionInformation object acts as the Entity's proxy in the collision pipeline.
Post Reply