Catch Collisionevents

Catch Collision Events

It took me a while to understand this part of JigLibX. It's neither complete or the best way, but i figured out that this works. Feel free to comment, correct, edit and add your own ideas. i don't mind :)

What it's all about?

This small advice is about handling if two objects collide. Well, JigLibX does it's part and simulates physical behavior, but for a game or other program you will need to do something. If a spaceship collides with an enormous alien-cockroach or cars hit each other, I figured out that there are at least two ways. Unfortunately i can't say which one has better performance or is better for your application (here someone who has mastered JigLibX could edit a bit ).

1. Use callback functions

Use this if you want to set collision behaviour for single or multiple objects. A function is created which will be applied to one or more objects and is executed each time a collison occurs.

1.1 Create callback-function

This function needs to return a boolean value. If you return "true", phyicengine will process the upcoming collision, else it is ignored as if collidee is something like a ghost; which you can pass through. It needs two parameters which will be given by callback. First is your object(=owner) and second is the object it's colliding with (=collidee). You can specify behaviour for special objects or classes of objects.

public bool handleCollisionDetection(CollisionSkin owner, CollisionSkin collidee)
    // here is handled what happens if your Object collides with another special Object (= OtherObject)
    if (collidee.Equals(OtherObject.Skin)) {
        // YourObject hits OtherObject ( or vice versa) and Collision is processed
        return true;
    else if (collidee.Equals(GhostObject.Skin)){
        // this time you'll be able to walk through Ghost-like object
        return false;
    // all other collisions will be handled by physicengine
    return true;

1.2 Apply callback-function to an object

Now you can use this function for all objects you want. That's it.

YourObject.Skin.callbackFn += new CollisionCallbackFn(handleCollisionDetection);

2. Use CollisionInfos

This time, we will specify a list of objects to check for collision. Using callbackfunction gives you an event; however there is a lack of information like how deep is intersection… Here you can do much more than in the previous example.

2.1 Preparation

We need a CollisionFunctor, which does checking for collision-pairs ( it tests for all possible pairs just one time), a list of Bodys, which shall be tested and another list to store for each collision a CollisionInfo.

List<Body> bodyList = new List<Body>(); // add all bodies, which should be checked to this list
List<CollisionInfo> collisionInfos = new List<CollisionInfo>(); // this is filled by collisionfuntor
CollisionFunctor collisionFunctor = new BasicCollisionFunctor(collisionInfos); // as parameter pass CollisionInfo-List

2.2 Detect Collisions

Detect collisions in an update routine ( e.g. world update).

collisionSystem.DetectAllCollisions(bodyList, collisionFunctor, null, 0.05f);

Last step is to do something with collected collisionInfo. In Update() add something like this:

if (collisionInfos.Count > 0) // check if there are new collisionInfos
                foreach (CollisionInfo info in collisionInfos)
                     // e.g. you can output number of collisionPoint, and Seperation Velocity. You can get much more infos of CollisionInfo!
                    Console.WriteLine(info.NumCollPts + " " + info.PointInfo[0].MinSeparationVel);
                // clear list, else you cannot decide which CollisionInfos are new or old

i hope this helps a bit.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License