2013-07-02 149 views
0

我正在研究的Cocos2D遊戲之一有圓形爆炸效果。這些爆炸效果需要對爆炸半徑範圍內的所有遊戲角色(由矩形邊界框表示,因爲所討論的對象爲坦克)所設置的最大傷害量的百分比進行處理。所以這可以歸結爲矩形碰撞以及距離最近的矩形邊緣多遠。我昨晚刺殺了這件事,但我相信可能有更好的辦法。特別是,我不知道根據計算的距離確定應用損害百分比的最佳方法。確定爆炸半徑的傷害 - Circle to Rectangle 2D

注意:所有坦克對象都有一個錨點(0,0),所以位置是根據邊界框的左下角。爆炸點是圓形爆炸的中心點。

 TankObject * tank = (TankObject*) gameSprite; 

     float distanceFromExplosionCenter; 
     // IMPORTANT :: All GameCharacter have an assumed (0,0) anchor 
     if (explosionPoint.x < tank.position.x) { 
      // Explosion to WEST of tank 
      if (explosionPoint.y <= tank.position.y) { 
       //Explosion SOUTHWEST 
       distanceFromExplosionCenter = ccpDistance(explosionPoint, tank.position); 

      } else if (explosionPoint.y >= (tank.position.y + tank.contentSize.height)) { 
       // Explosion NORTHWEST 
       distanceFromExplosionCenter = ccpDistance(explosionPoint, 
                  ccp(tank.position.x, tank.position.y + tank.contentSize.height)); 

      } else { 
       // Exp center's y is between bottom and top corner of rect 
       distanceFromExplosionCenter = tank.position.x - explosionPoint.x; 

      } // end if 
     } else if (explosionPoint.x > (tank.position.x + tank.contentSize.width)) { 
      // Explosion to EAST of tank 
      if (explosionPoint.y <= tank.position.y) { 
       //Explosion SOUTHEAST 
       distanceFromExplosionCenter = ccpDistance(explosionPoint, 
                  ccp(tank.position.x + tank.contentSize.width, 
                       tank.position.y)); 

      } else if (explosionPoint.y >= (tank.position.y + tank.contentSize.height)) { 
       // Explosion NORTHEAST 
       distanceFromExplosionCenter = ccpDistance(explosionPoint, 
                  ccp(tank.position.x + tank.contentSize.width, 
                   tank.position.y + tank.contentSize.height)); 

      } else { 
       // Exp center's y is between bottom and top corner of rect 
       distanceFromExplosionCenter = explosionPoint.x - (tank.position.x + tank.contentSize.width); 

      } // end if 
     } else { 
      // Tank is either north or south and is inbetween left and right corner of rect 
      if (explosionPoint.y < tank.position.y) { 
       // Explosion is South 
       distanceFromExplosionCenter = tank.position.y - explosionPoint.y; 

      } else { 
       // Explosion is North 
       distanceFromExplosionCenter = explosionPoint.y - (tank.position.y + tank.contentSize.height); 

      } // end if 
     } // end outer if 
     if (distanceFromExplosionCenter < explosionRadius) { 
      /* 
      Collision :: Smaller distance larger the damage 
      */ 
      int damageToApply; 
      if (self.directHit) { 
       damageToApply = self.explosionMaxDamage + self.directHitBonusDamage; 
       [tank takeDamageAndAdjustHealthBar:damageToApply]; 
       CCLOG(@"Explsoion-> DIRECT HIT with total damage %d", damageToApply); 
      } else { 
       // TODO adjust this... turning out negative for some reason... 
       damageToApply = (1 - (distanceFromExplosionCenter/explosionRadius) * explosionMaxDamage); 
       [tank takeDamageAndAdjustHealthBar:damageToApply]; 
       CCLOG(@"Explosion-> Non direct hit collision with tank"); 
       CCLOG(@"Damage to apply is %d", damageToApply); 
      } // end if 

     } else { 
      CCLOG(@"Explosion-> Explosion distance is larger than explosion radius"); 
     } // end if 
    } // end if 

的問題:

1)這個圈子能以RECT碰撞算法做的更好?我有太多支票嗎?

2)如何計算損失百分比?我目前的方法偶爾會產生負數,我不明白爲什麼(也許我需要更多的睡眠!)。但是,在我的if語句中,我會問如果距離<的爆炸半徑。當控制通過時,距離/半徑必須是< 1對不對?所以1 - 中間計算不應該是負數。

感謝所有幫助/諮詢

編輯

我偶爾陰性結果的原因是放錯地方的括號。

damageToApply = (1 - (distanceFromExplosionCenter/explosionRadius)) * explosionMaxDamage; 

仍在尋找關於如何計算爆炸半徑損傷的輸入。

+0

仍在尋找這個 –

回答

0

您可以檢查使用ccpDistance的對象的距離:

float distance = ccpDistance(sprite1.position, sprite2.position); 

我認爲ccpDistance將始終返回正值。但如果沒有,那麼得到的絕對值:fabsf(distance)

+0

的一些輸入是的,我在我的發佈代碼中使用ccpDistance。我還假定它只返回正值。這可能是值得檢查的,並可能是我看到計算損害的負面結果的原因。我不認爲我可以做一個簡單的ccpDistance調用,因爲我需要知道矩形的哪個角/邊的距離。我的精靈都有(0,0)的錨。因此,當爆炸位於精靈右側時,我不想將距離與左側角落進行比較。 –