Question

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;
}
Was it helpful?

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.

OTHER TIPS

So you want to catch a leading edge only? There's nothing particularly smart to say beyond that you should compare the current value to a previous value.

How you should best do that probably depends on your wider data format.

If you're storing your objects as position and velocity, and just adding velocity every frame (so, Euler integration, essentially) then you can trigger a collision whenever there's no overlap per the position but there is per position + velocity. That's a potential answer even if you're not explicitly storing velocity but can provide it by some other means.

Similarly if you're storing current position and last position (eg, for Verlet integration) then you can do the two tests like that.

If there's a convenient structure for just storing the result from the previous frame and comparing to the current then that's also an option — a std::map indexed by a std::pair might be overkill if you're on a tight memory or cache budget but if there's any special game-specific knowledge you can take advantage of then you might not need to write the option off.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top