bool MidPointCollisionStrategy::execute(
      const MultiframeSprite& obj1, const MultiframeSprite& obj2) const {
  int COLLISION_DISTANCE = 
    obj1.getCurrentFrameref()->getWidth()/2 + obj2.getCurrentFrameref()->getWidth()/2;
  float xobj1 = obj1.X()+obj1.getCurrentFrameref()->getWidth()/2;
  float yobj1 = obj1.Y()+obj1.getCurrentFrameref()->getHeight()/2;
  float xobj2 = obj2.X() + obj2.getCurrentFrameref()->getWidth()/2;
  float yobj2 = obj2.Y() + obj2.getCurrentFrameref()->getHeight()/2;
  return distance(xobj1, yobj1, xobj2, yobj2) < COLLISION_DISTANCE;
}
Beispiel #2
0
bool RectangularCollisionStrategy::execute(
      const MultiframeSprite& obj1, const MultiframeSprite& obj2) const {
  float left1 = obj1.X();
  float left2 = obj2.X();
  float right1 = left1+obj1.getFrame()->getWidth();
  float right2 = left2+obj2.getFrame()->getWidth();
  float top1 = obj1.Y();
  float top2 = obj2.Y();
  float bottom1 = top1+obj1.getFrame()->getHeight();
  float bottom2 = top2+obj2.getFrame()->getHeight();
  if ( right1 < left2 ) return false;
  if ( left1 > right2 ) return false;
  if ( bottom1 < top2 ) return false;
  if ( bottom2 < top1 ) return false;
  return true;
}