我试图制作一个游戏,其中每个像素需要碰撞检测(在球和杆之间)。 这是我的逻辑和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;
}
}
}
Logcat:
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
答案 0 :(得分:0)
看起来你没有进行“像素完美”碰撞,而是进行圆/线碰撞。
找到圆线碰撞的最简单方法是找到圆上最近的点,然后检查从该点到圆的距离。 这里有一个很好的教程:http://plasticsturgeon.com/2011/03/actionscript-collision-detection-2-circle-line-collision-detection/ 它写在actionscript 3中,但它足够接近java,你应该没有问题跟随。数学从一个到另一个是相同的。
另一种方法,特别是如果你需要连续碰撞检测,是使用Box2D。为此,请使用Box2DExtension作为andengine。 https://github.com/nicolasgramlich/AndEnginePhysicsBox2DExtension