0

プレートが上から下に落ちるゲームに取り組んでいます。一部のプレートは地面で「跳ね返り」、その後再び上向きに動き始めます。これにより、落下するプレートが「上昇するプレート」と衝突する状況が発生します。

私の問題?この衝突を検出する方法がわかりません。

if(CGRectIntersectsRect([self boundingBox], [self boudingBox])) すべてのプレートが同じクラスからのものであるため、このステートメントは常に真であるため、私は書く ことができません。

forループでプレートを作成します。

for(i=0; i<9; i++){

 Plate *plate = [Plate initPlate];

}

そして、ゲーム全体でこれらのプレートを再利用します。

2つのプレート間の衝突を検出する方法に関するアイデアや回避策はありますか?アドバイスをいただければ幸いです。

よろしく。

4

2 に答える 2

1

プレートのセットを管理する(たとえば、NSMutableArrayを使用する)クラスが必要です。Plateクラスでの衝突をチェックする代わりに、この新しいクラスでそれを行います。

アレイが次のようになっていると仮定します。

NSMuttableArray *plateSet

あなたはこれを行うことができます:

for (Plate *bouncingPlate in plateSet)
{
    if ([bouncingPlate bouncing])
    {
        for (Plate *fallingPlate in plateSet)
        {
            if (![fallingPlate bouncing])
            {
                /* Check for collision here between fallingPlate and bouncingPlate */
            }
        }
    }
}

または、よりエレガントに:

for (Plate *bouncingPlate in plateSet)
{
    if (![bouncingPlate bouncing])
    {
        continue;
    }

    for (Plate *fallingPlate in plateSet)
    {
        if ([fallingPlate bouncing])
        {
            continue;
        }
        /* Check for collision here between fallingPlate and bouncingPlate */
    }
}
于 2012-06-28T00:00:56.850 に答える
0

はい...それらをに追加してから、衝突をチェックするためにNSMutableArray使用する必要がありますccpDistance

このようなもの:

for (int i=0;i<8,i++){
   for (int j=i+1,j<9,j++){
if(ccpDistance([[plates objectAtIndex:i]position],[[plates objectAtIndex:j]position])<plateRadius*2) {
//collision detected
}}}

もちろん、これはプレートが円の場合に機能します

正方形の使用CGRectIntersectsRect

 for (int i=0;i<8,i++){
   for (int j=i+1,j<9,j++){
if([plates objectAtIndex:i].visible && [plates objectAtIndex:j].visible &&(CGRectIntersectsRect([[plates objectAtIndex:i]boundingBox],[[plates objectAtIndex:j]boundingBox])) {
//collision detected
}}}
于 2012-06-28T00:59:31.167 に答える