0

私は境界ボックスを使用して衝突をチェックします。タッチには問題ありませんが、2 つのスプライトの衝突にはまったく問題ありません (それらはすべて円形ですが、バウンディング ボックスは長方形です)。

より良い方法はありますか?(以下のコードを変更していただければ幸いです:

-(bool) isFishCollidingWithRect:(CGRect) rect
{
     FishEntity* fish;
     CCARRAY_FOREACH(fishes, fish)
     {
        if (fish.visible && CGRectIntersectsRect([fish boundingBox], rect)) {
             [fish gotHit];
             return YES;
         }
      }
      return NO;
}

元の四角形と頂点が円にちょうど接する四角形の中間にある、より小さな四角形を取得する良い方法があると思います。サイズは簡単に取得できますが、四角形が回転する可能性があるため、座標は非常に難しく、私の数学は最悪です

-(bool) isBirdCollidingWithSprite:(CCSprite*) sprite
 {
     BirdEntity* bird;
     CCARRAY_FOREACH(birdsAlone, bird)
     {
         float distance = sqrt((bird.anchorPoint.x - sprite.anchorPoint.x) * (bird.anchorPoint.x - sprite.anchorPoint.x) + (bird.anchorPoint.y - sprite.anchorPoint.y) * (bird.anchorPoint.y - sprite.anchorPoint.y)); 
         if (bird.visible && (bird.contentSize.width / 2 + sprite.contentSize.width / 2) >= distance) {
             if ([bird inBubble] == NO) {
                 [bird bubbled];
             }
             return YES;
         }
     }
     return NO;
 }

まあ、魚は常にヒットします。 contentSize はバッチノードのテクスチャ全体であると思います。何を使えばいいですか?

4

1 に答える 1

0

それらはすべて円なので、非常に簡単です。2 つの円の中心間の距離を確認するだけです。円の半径の合計よりも大きい場合、スプライトは接触していません。少ない場合は重複しています。等しい場合は、触れているだけです。

于 2011-03-18T16:24:19.910 に答える