Search code examples
c++collisionpulse

C++ pulse counter


I'm writing a shoot 'em up game on an Arduino and I'm using AABB collision detection -

ref: Bounding Box Collision Detection

My problem is with the time objects collide with each other, I'm currently using a basic trigger which is true for the duration two objects intersect. I think what I need is best described by the issue of pulse counting, where regardless of 'pulse length' (or intersection duration) only 1 count is detected: enter image description here

How may I implement this in C++? the function I'm currently using:

bool CollisionTest( xPlayer, yPlayer, xAlien, yAlien, width, height)
{
  if( ((xPlayer+width) >= xAlien)  && (xPlayer <= (xAlien+width))  &&
      ((yPlayer+height) >= yAlien) && (yPlayer <= (yAlien+height)) )
    return true;
  else
    return false;
}

Solution

  • You need an edge detector!

    bool collision = false;
    int pulses = 0;
    
    //For each frame..
    if(!collision && CollisionTest(....)) {
        pulses++;
    }
    collision = CollisionTest(....);
    

    Not sure why you need to count them, but this should do it.