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 回答