Java 2D 中的碰撞检测无法正常工作
Collision Detection in Java 2D Not Working Properly
所以基本上在过去一年半的时间里,我一直在努力让这个盒子人(玩家)在与另一个盒子碰撞时停止向与盒子碰撞的方向移动。它是有点成功,但在看似随机的时刻,如果我向上移动并击中玩家左侧的箱子,它可能会飞溅到与之相撞的箱子中并飞下。各个方向都一样
public int checkCollision(ID id){
for(int i = 0; i < handler.obj.size(); i++){ //Cycles through all the objects
GameObject go = handler.obj.get(i); //Stores a temp GameObject
if(go.getID() == id){ //Checks if the id matches and if so do the collision stuff.
if(getBoundsT().intersects(go.getBounds())){
System.out.println("collided top");
y = go.getY() + go.getHeight();
velY = 0;
return 0; //Top side
}
if(getBoundsB().intersects(go.getBounds())){
System.out.println("collided bottom");
y = go.getY() - height;
velY = 0;
return 1; //Bottom side
}
if(getBoundsL().intersects(go.getBounds())){
x = go.getX() + width;
velX = 0;
System.out.println("collided left");
return 2; //Left Side
}
if(getBoundsR().intersects(go.getBounds())){
System.out.println("collided right");
x = go.getX() - width;
velX = 0;
return 3; //Right Side
}
if(getBounds().intersects(go.getBounds())){
System.out.println("collided on all sides");
return 4; //All the sides
}
}
}
return 5; //No Collision
}
checkCollision 方法每秒调用 60 次。 getBounds(l/r/u/d) 函数只是 returns 与字母对应的左侧、右侧、顶部(上)或底部(下)侧的矩形。 Id 正是玩家与之发生碰撞的对象。在这种情况下,它是墙,所以无论它说什么,它都只是墙。 (我确保矩形不会超出它们应该超出的区域。)
我已尽我所能,如有任何帮助,我们将不胜感激! (这是我的第一个问题,如果写得不好请见谅)
编辑:相交代码 (getBounds) 这是在所有对象继承的 GameObject class 中。
public Rectangle getBoundsB() {
return new Rectangle((int)(x + (width/2) - (width/4)), (int)(y + height/2), (int)(width / 2), (int)(height / 2));
}
public Rectangle getBoundsT() {
return new Rectangle((int)(x + (width/2) - (int)(width/4)), (int) y, (int)width / 2, (int)(height / 2));
}
public Rectangle getBoundsR() {
return new Rectangle((int)(x + width - 5), (int)(y + 5), 5, (int)(height - 10));
}
public Rectangle getBoundsL() {
return new Rectangle((int)x, (int)(y + 5), 5, (int)(height - 10));
}
public Rectangle getBounds() {
return new Rectangle((int)x, (int)y, (int)width, (int)height);
}
在 tick 方法中(每 60 秒调用一次):
checkCollision(ID.Wall);
下图显示了靠墙的矩形的 3 个边。如果我将矩形按在墙上,蓝色和绿色线的左角会接触墙壁,而红线会接触墙壁。所以发生的事情是因为你首先检查顶部,它看到在那个单一的点有一个交叉点并且 returns true 对于顶部交叉点而不是左侧。
我认为您可以通过修改获得 top/bottom 边界的方式来解决此问题。也许通过从宽度中减去 1,你 return 你的边界。
所以基本上在过去一年半的时间里,我一直在努力让这个盒子人(玩家)在与另一个盒子碰撞时停止向与盒子碰撞的方向移动。它是有点成功,但在看似随机的时刻,如果我向上移动并击中玩家左侧的箱子,它可能会飞溅到与之相撞的箱子中并飞下。各个方向都一样
public int checkCollision(ID id){
for(int i = 0; i < handler.obj.size(); i++){ //Cycles through all the objects
GameObject go = handler.obj.get(i); //Stores a temp GameObject
if(go.getID() == id){ //Checks if the id matches and if so do the collision stuff.
if(getBoundsT().intersects(go.getBounds())){
System.out.println("collided top");
y = go.getY() + go.getHeight();
velY = 0;
return 0; //Top side
}
if(getBoundsB().intersects(go.getBounds())){
System.out.println("collided bottom");
y = go.getY() - height;
velY = 0;
return 1; //Bottom side
}
if(getBoundsL().intersects(go.getBounds())){
x = go.getX() + width;
velX = 0;
System.out.println("collided left");
return 2; //Left Side
}
if(getBoundsR().intersects(go.getBounds())){
System.out.println("collided right");
x = go.getX() - width;
velX = 0;
return 3; //Right Side
}
if(getBounds().intersects(go.getBounds())){
System.out.println("collided on all sides");
return 4; //All the sides
}
}
}
return 5; //No Collision
}
checkCollision 方法每秒调用 60 次。 getBounds(l/r/u/d) 函数只是 returns 与字母对应的左侧、右侧、顶部(上)或底部(下)侧的矩形。 Id 正是玩家与之发生碰撞的对象。在这种情况下,它是墙,所以无论它说什么,它都只是墙。 (我确保矩形不会超出它们应该超出的区域。)
我已尽我所能,如有任何帮助,我们将不胜感激! (这是我的第一个问题,如果写得不好请见谅)
编辑:相交代码 (getBounds) 这是在所有对象继承的 GameObject class 中。
public Rectangle getBoundsB() {
return new Rectangle((int)(x + (width/2) - (width/4)), (int)(y + height/2), (int)(width / 2), (int)(height / 2));
}
public Rectangle getBoundsT() {
return new Rectangle((int)(x + (width/2) - (int)(width/4)), (int) y, (int)width / 2, (int)(height / 2));
}
public Rectangle getBoundsR() {
return new Rectangle((int)(x + width - 5), (int)(y + 5), 5, (int)(height - 10));
}
public Rectangle getBoundsL() {
return new Rectangle((int)x, (int)(y + 5), 5, (int)(height - 10));
}
public Rectangle getBounds() {
return new Rectangle((int)x, (int)y, (int)width, (int)height);
}
在 tick 方法中(每 60 秒调用一次):
checkCollision(ID.Wall);
下图显示了靠墙的矩形的 3 个边。如果我将矩形按在墙上,蓝色和绿色线的左角会接触墙壁,而红线会接触墙壁。所以发生的事情是因为你首先检查顶部,它看到在那个单一的点有一个交叉点并且 returns true 对于顶部交叉点而不是左侧。
我认为您可以通过修改获得 top/bottom 边界的方式来解决此问题。也许通过从宽度中减去 1,你 return 你的边界。