0

ピクセルごとの衝突検出 (ボールとバーの間) が必要なゲームを作成しようとしています。ここに私のロジックがあり、logcat はボールの調整された x 軸を表示します。ボールの位置登録は連続したピクセル単位ではありません。

シーン 1 - 右側の打球バー

シーン 2 - 上部のボール ヒッティング バー

シーン 3 - 下部のボール ヒッティング バー

シーン 4 - 左の打球バー

@Override

public void onUpdate(final float pSecondsElapsed) {

int collisionDetection(final Ball ball, final Sprite bar){

//scene 1
if((ball.getX() == bar.getX()) && ((ball.getY()+(ball.getHeight()/2)) >= bar.getY()) && ((ball.getY()+(ball.getHeight()/2)) >= (bar.getY()+bar.getHeight()))){

        Ball.mPhysicsHandler.setVelocityX(-(Ball.mPhysicsHandler.getVelocityX()));
        return 1;
    }
    //scene 4
    else if(((ball.getX()+ball.getWidth()) == bar.getX()-1)){// && ((ball.getY()+(ball.getHeight()/2)) >= bar.getY()) && ((ball.getY()+(ball.getHeight()/2)) >= (bar.getY()+bar.getHeight()))){
        changeToBlue();
        Ball.mPhysicsHandler.setVelocityX(-(Ball.mPhysicsHandler.getVelocityX()));
        return 1;
    }
    //scene 2
    else if(((ball.getY()+ball.getHeight()) == bar.getY()) && (ball.getX()+(ball.getWidth()/2) >= bar.getX()) && (ball.getX()+(ball.getWidth()/2) <= (bar.getX()+bar.getWidth()))){

        Ball.mPhysicsHandler.setVelocityY(-(Ball.mPhysicsHandler.getVelocityY()));
        return 2;
    }
    //scene 3
    else if((ball.getY() == (bar.getY()+bar.getHeight())) && (ball.getX()+(ball.getWidth()/2) >= bar.getX()) && (ball.getX()+(ball.getWidth()/2) <= (bar.getX()+bar.getWidth()))){

        Ball.mPhysicsHandler.setVelocityY(-(Ball.mPhysicsHandler.getVelocityY()));
        return 2;
    }
    else{
        return 0;
    }
}

}

ログキャット:

08-12 18:19:32.710  32531-32560/? A/GameCore: 860

08-12 18:19:32.720  32531-32560/? A/GameCore: 867

08-12 18:19:32.740  32531-32560/? A/GameCore: 874

08-12 18:19:32.760  32531-32560/? A/GameCore: 880

08-12 18:19:32.770  32531-32560/? A/GameCore: 887

08-12 18:19:32.790  32531-32560/? A/GameCore: 894

08-12 18:19:32.810  32531-32560/? A/GameCore: 900

08-12 18:19:32.820  32531-32560/? A/GameCore: 907

08-12 18:19:32.840  32531-32560/? A/GameCore: 914

08-12 18:19:32.860  32531-32560/? A/GameCore: 921

08-12 18:19:32.870  32531-32560/? A/GameCore: 927

08-12 18:19:32.890  32531-32560/? A/GameCore: 934

08-12 18:19:32.910  32531-32560/? A/GameCore: 941

08-12 18:19:32.930  32531-32560/? A/GameCore: 947

08-12 18:19:32.940  32531-32560/? A/GameCore: 955

08-12 18:19:32.960  32531-32560/? A/GameCore: 961

08-12 18:19:32.970  32531-32560/? A/GameCore: 968

08-12 18:19:32.990  32531-32560/? A/GameCore: 974

08-12 18:19:33.010  32531-32560/? A/GameCore: 982

08-12 18:19:33.020  32531-32560/? A/GameCore: 987

08-12 18:19:33.040  32531-32560/? A/GameCore: 994

08-12 18:19:33.060  32531-32560/? A/GameCore: 1000

08-12 18:19:33.070  32531-32560/? A/GameCore: 1007

08-12 18:19:33.090  32531-32560/? A/GameCore: 1014

08-12 18:19:33.110  32531-32560/? A/GameCore: 1020

08-12 18:19:33.120  32531-32560/? A/GameCore: 1027

08-12 18:19:33.140  32531-32560/? A/GameCore: 1034

08-12 18:19:33.160  32531-32560/? A/GameCore: 1042

08-12 18:19:33.170  32531-32560/? A/GameCore: 1047

08-12 18:19:33.190  32531-32560/? A/GameCore: 1054

08-12 18:19:33.210  32531-32560/? A/GameCore: 1061

08-12 18:19:33.220  32531-32560/? A/GameCore: 1067

08-12 18:19:33.241  32531-32560/? A/GameCore: 1074

08-12 18:19:33.261  32531-32560/? A/GameCore: 1080

08-12 18:19:33.271  32531-32560/? A/GameCore: 1087

08-12 18:19:33.291  32531-32560/? A/GameCore: 1094

08-12 18:19:33.311  32531-32560/? A/GameCore: 1100

08-12 18:19:33.321  32531-32560/? A/GameCore: 1107

08-12 18:19:33.341  32531-32560/? A/GameCore: 1114

08-12 18:19:33.361  32531-32560/? A/GameCore: 1120

08-12 18:19:33.371  32531-32560/? A/GameCore: 1127

08-12 18:19:33.391  32531-32560/? A/GameCore: 1134

08-12 18:19:33.411  32531-32560/? A/GameCore: 1140

08-12 18:19:33.421  32531-32560/? A/GameCore: 1147

08-12 18:19:33.441  32531-32560/? A/GameCore: 1154

08-12 18:19:33.461  32531-32560/? A/GameCore: 1160

08-12 18:19:33.471  32531-32560/? A/GameCore: 1167
4

1 に答える 1

0

「ピクセルパーフェクト」衝突ではなく、円/線衝突を実行しているようです。

円の線の衝突を見つける最も簡単な方法は、線上で円に最も近い点を見つけ、その点から円までの距離を確認することです。ここに良いチュートリアルがあります:http://plasticsturgeon.com/2011/03/actionscript-collision-detection-2-circle-line-collision-detection/ actionscript 3で書かれていますが、Javaに十分近いフォローしても問題ありません。そして、数学は互いに同じです。

特に継続的な衝突検出が必要な場合の別のアプローチは、Box2D を使用することです。これには、andengine に Box2DExtension を使用します。https://github.com/nicolasgramlich/AndEnginePhysicsBox2DExtension

于 2013-08-13T17:39:49.003 に答える