[英]How to do collision detection with many walls (maze)?
在我的游戏中,玩家导航迷宫。 我无法弄清楚如何与墙壁进行适当的碰撞检测。 对于停留在某个区域很容易进行碰撞检测:
if (x > rightWallX - playerWidth) x = rightWallX - playerWidth;
if (x < leftWallX) x = leftWallX;
//...
但是我如何对许多墙进行碰撞检测呢?
我可以做没有校正的普通碰撞检测(如if (intersecting) return true;
),但我无法正确纠正。 如果我只是存储旧的x
和y
并重置它们,那么
如何在迷宫中进行碰撞检测?
最简单的方法,一旦你已经解决了碰撞检测 , 解决冲突是演员移到最接近的有效位置到演员会是如果不是它与碰撞的对象。 这假定没有惯性,但它足以用于迷宫般的游戏或自上而下的地图爬行游戏。
如果您想进一步简化计算,可以限制自己检测更改actor的x
或y
坐标是否更好。 如果你的演员有一个轴对齐的矩形命中框,并且所有障碍物也是轴对齐的矩形(最简单的情况),这个假设确实是正确的。 然而,在其他一些情况下,结果可能并不令人满意(潜在的神器:滑翔对角墙的速度提升 - 在大多数迷宫游戏中并非如此)。
请记住,多次碰撞可能同时发生(推动两面墙)。 如果两个墙之间没有锐角,则演员可以相交(例如,如果所有障碍物都是轴对齐并且间距足够大),则依次固定每个碰撞就足够了 - 只是在第一次碰撞后不要停止。
您可以使用Rectangle.intersects()方法:
public Rectangle Player(){
return new Rectangle(PlayerX,PlayerY,PlayerWidth,PlayerHeight);
//we do this for getting players x and y values every tick
}
if(Player().intersects(new Rectangle(0,0,100,50)))//if(player touching wall)
new Rectangle(0,0,100,50)只是一个例子,您可以更改它。
好的,我现在正在制作一个2D自上而下的视图游戏,我不确定你是如何创建你的迷宫的。 但是,在我的游戏中,我的等级是从Tile [] [] tiles = new Tile [levelWidth] [levelHeight]创建的; 阵列。 我处理碰撞检测的方法是检查周围的瓷砖,看它们是否是实心的。
这是我的getTile方法。
public Tile[][] getTile(int x, int y) {
if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) {
return new VoidTile();
} else {
return tiles[x][y];
}
}
在我的Tile.java类中,我有一个isSolid()方法,它返回tile是否为实体。 我的所有tile都扩展了我的Tile.java,因此它们继承了这个方法,并在构造函数中覆盖它。 正如我之前所说,我不确定你是否使用与我相同的级别实现方式。 但是,这样做是很好的做法:)
就个人而言,我不喜欢使用.intersects()和.contains()方法进行Sprite碰撞检测。 我主要将它们用于按钮等。
好吧,在我的player.java类中,我有一个checkBlockedDirection(int x,int y)方法,它看起来像这样。
public void checkBlockedDirection(int x, int y) {
boolean u = map.getTile(x, y - 1).isSolid();
boolean d = map.getTile(x, y + 1).isSolid();
boolean l = map.getTile(x - 1, y).isSolid();
boolean r = map.getTile(x + 1, y).isSolid();
if (u) {
uBlocked = true;
System.out.println("up tile blocked");
} else {
uBlocked = false;
}
if (d) {
dBlocked = true;
System.out.println("down tile blocked");
} else {
dBlocked = false;
}
if (l) {
lBlocked = true;
System.out.println("left tile blocked");
} else {
lBlocked = false;
}
if (r) {
rBlocked = true;
System.out.println("right tile blocked");
} else {
rBlocked = false;
}
}
然后在我的播放器更新方法中我有这个
public void tick() {
float dx = 0;
float dy = 0;
if (input.up.isPressed()) {
direction = 0;
} else if (input.down.isPressed()) {
direction = 2;
} else if (input.left.isPressed()) {
direction = 3;
} else if (input.right.isPressed()) {
direction = 1;
} else {
direction = 4; // standing
}
checkBlockedDirection((int)x, (int)y);
if (input.up.isPressed() && y > 0 && !uBlocked) {
dy += -speed;
} else if (input.down.isPressed() && y < map.getHeight() - 1 && !dBlocked) {
dy += speed;
} else if (input.left.isPressed() && x > 0 && !lBlocked) {
dx += -speed;
} else if (input.right.isPressed() && x < map.getWidth() - 1 && !rBlocked) {
dx += speed;
}
x += dx;
y += dy;
}
基本上它只是检查上,下,左或右块是否稳固。 如果它们是固体,那么它就不会移动,如果它们不固定,那么你可以向所需的方向移动。
不确定这是否有帮助,但这只是我对这种网格碰撞检测的看法:)
希望这可以帮助 :)
请享用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.