Java 在二维环境中用Slick检测碰撞
我在游戏中检测碰撞时遇到了一些问题。碰撞检测似乎只在部分墙壁上起作用。我正在使用slick2d作为我的图书馆。如果你需要更多的代码或信息,我可以给你。我现在必须去某个地方,所以我只是很快地把它打出来,但如果你还需要什么,尽管问 代码:Java 在二维环境中用Slick检测碰撞,java,2d,collision,slick2d,Java,2d,Collision,Slick2d,我在游戏中检测碰撞时遇到了一些问题。碰撞检测似乎只在部分墙壁上起作用。我正在使用slick2d作为我的图书馆。如果你需要更多的代码或信息,我可以给你。我现在必须去某个地方,所以我只是很快地把它打出来,但如果你还需要什么,尽管问 代码: @覆盖 公共void managewall冲突(场景currScene){ 对于(int i=0;i
@覆盖
公共void managewall冲突(场景currScene){
对于(int i=0;icurrTile.getY2()){
}
//顶
else if(this.x>currTile.getX()){
}
打破
}
}
}
}
感谢您的帮助。谢谢
新代码:
for(int i = 0; i < currScene.getMapManager().getWalls().size(); i++){
MapTile currTile = currScene.getMapManager().getWalls().get(i);
if(currTile.isCollidable()){
if(this.getBounds().intersects(currTile.getBounds())){
//down
if(this.y2 < currTile.getY() && this.y < currTile.getY2() && this.y2 < currTile.getY2()){
this.setY(currTile.getY() - this.getHEIGHT());
}
//up
if(this.y < currTile.getY2() && this.y2 > currTile.getY() && this.y2 > currTile.getY2()){
this.setY(currTile.getY2());
}
//right
if(this.x2 < currTile.getX() && this.x < currTile.getX2() && this.x2 < currTile.getX2()){
this.setX(currTile.getX() - this.getWIDTH());
}
//left
if(this.x < currTile.getX2() && this.x2 > currTile.getX() && this.x2 > currTile.getX2()){
this.setX(currTile.getX2());
}
}
}
}
for(int i=0;icurrTile.getY()&&this.y2>currTile.getY2()){
this.setY(currTile.getY2());
}
//对
if(this.x2currTile.getX()&&this.x2>currTile.getX2()){
this.setX(currTile.getX2());
}
}
}
}
我仍然有问题。它可以很好地检测碰撞,但无论碰撞到什么地方,它都会到达同一个点。想象一个2d房间。因此,如果它与左边的墙发生碰撞,那么它会自动转到该墙上的某个位置。如果你试着沿着墙滑动,它会变得很滑,你会不断地向前然后向后移动。我对正在发生的事情感到困惑
for(int i = 0; i < currScene.getMapManager().getWalls().size(); i++){
MapTile currTile = currScene.getMapManager().getWalls().get(i);
if(currTile.isCollidable()){
if(this.getBounds().intersects(currTile.getBounds())){
//down
if(this.y2 < currTile.getY() && this.y < currTile.getY2() && this.y2 < currTile.getY2()){
this.setY(currTile.getY() - this.getHEIGHT());
}
//up
if(this.y < currTile.getY2() && this.y2 > currTile.getY() && this.y2 > currTile.getY2()){
this.setY(currTile.getY2());
}
//right
if(this.x2 < currTile.getX() && this.x < currTile.getX2() && this.x2 < currTile.getX2()){
this.setX(currTile.getX() - this.getWIDTH());
}
//left
if(this.x < currTile.getX2() && this.x2 > currTile.getX() && this.x2 > currTile.getX2()){
this.setX(currTile.getX2());
}
}
}
}