我正在开发的 Cocos2D 游戏之一具有圆形爆炸效果。这些爆炸效果需要对爆炸半径内的所有游戏角色(由矩形边界框表示,因为所讨论的对象是坦克)造成其设定的最大伤害的百分比。所以这归结为圆到矩形的碰撞以及圆的半径离最近的矩形边缘有多远。昨晚我试图解决这个问题,但我相信可能有更好的方法。特别是,我不知道根据计算出的距离确定应用伤害百分比的最佳方法。
注意:所有坦克对象都有一个锚点(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)这个圆校正碰撞算法可以做得更好吗?我的支票太多了吗?
2) 如何计算基于百分比的伤害?我目前的方法偶尔会产生负数,我不明白为什么(也许我需要更多的睡眠!)。但是,在我的 if 语句中,我询问距离是否 < 爆炸半径。当控制通过时,距离/半径必须<1对吗?所以 1 - 中间计算不应该是负数。
感谢任何帮助/建议
编辑
我偶尔的否定结果是由于括号放错了。
damageToApply = (1 - (distanceFromExplosionCenter/explosionRadius)) * explosionMaxDamage;
仍在寻找有关如何计算爆炸半径伤害的输入。