Collision detection code style

Posted by Marian Ivanov on Game Development See other posts from Game Development or by Marian Ivanov
Published on 2012-12-08T21:24:47Z Indexed on 2012/12/08 23:37 UTC
Read the original article Hit count: 298

Not only there are two useful broad-phase algorithms and a lot of useful narrowphase algorithms, there are also multiple code styles.

Arrays vs. calling

  1. Make an array of broadphase checks, then filter them with narrowphase checks, then resolve them.

     function resolveCollisions(thingyStructure * a,thingyStructure * b,int index){
          possibleCollisions = getPossibleCollisions(b,a->get(index));
          for(i=0; i<possibleCollitionsNumber; i++){
               if(narrowphase(possibleCollisions[i],a[index])) {
                    collisions->push(possibleCollisions[i]);
               };
          };
          for(i=0; i<collitionsNumber; i++){
               //CODE FOR RESOLUTION
          };
     };
    
  2. Make the broadphase call the narrowphase, and the narrowphase call the resolution

     function resolveCollisions(thingyStructure * a,thingyStructure * b,int index){
          broadphase(b,a->get(index));
     };
    
     function broadphase(thingy * with, thingy * what){
          while(blah){
             //blahcode
              narrowphase(what,collidingThing);
          };
     };
    

Events vs. in-the-loop

  1. Fire an event. This abstracts the check away, but it's trickier to make an equal interaction.

    a[index] -> collisionEvent(eventdata);
    
    //much later
    
    int collisionEvent(eventdata){
         //resolution gets here
    }
    
  2. Resolve the collision inside the loop. This glues narrowphase and resolution into one layer.

       if(narrowphase(possibleCollisions[i],a[index])) {
            //CODE GOES HERE
       };
    

The questions are: Which of the first two is better, and how am I supposed to make a zero-sum Newtonian interaction under B1.

© Game Development or respective owner

Related posts about collision-detection

Related posts about events