解决二维游戏碰撞(多边形)
Solving 2d game collision (polygons)
我看了很多教程如何检测碰撞,但没有看到如何解决碰撞。我在做一个俯视游戏,玩家有圆形碰撞形状,而墙壁是各种多边形。
我正在使用 slick2d。我应该做的是,如果玩家与角落发生碰撞,我会按法线移动玩家,直到他不与角落发生碰撞。在我检查角落之后,我以类似的方式检查边缘。然而,我的播放器经常摇晃,突然转弯,偶尔会穿墙。
代码如下:
碰撞检测代码(播放器内部class):
public void checkCollision (ArrayList<Wall> walls) {
//pos is position vector
Shape player_c = getShape();
for (Wall w : walls){
if (player_c.intersects(w.getShape())){
for (int i = 0; i < w.getShape().getPointCount(); i++){
float point_x, point_y;
point_x = w.getShape().getPoint(i)[0];
point_y = w.getShape().getPoint(i)[1];
if (player_c.contains(point_x, point_y)){
while (player_c.intersects(w.getShape())){
float normal_x, normal_y;
normal_x = w.getShape().getNormal(i)[0];
normal_y = w.getShape().getNormal(i)[1];
pos.x += 0.001 * normal_x;
pos.y += 0.001 * normal_y;
player_c = getShape();
}
break;
} else {
if (player_c.intersects(PolygonExt.getLine((Polygon)w.getShape(), i))){
while (player_c.intersects(w.getShape())){
float[] normal;
normal = PolygonExt.getLineNormal((Polygon)w.getShape(), i );
pos.x += 0.001 * normal[0];
pos.y += 0.001 * normal[1];
player_c = getShape();
}
break;
}
}
}
}
}
}
墙 class:
public class Wall {
Polygon shape;
private Color color;
public Wall(Vector2f... points) {
shape = new Polygon();
for (int i = 0; i < points.length; i++){
shape.addPoint(points[i].x, points[i].y);
}
color = new Color((float)Math.random()*0.5f + 0.5f, (float)Math.random()*0.5f + 0.5f, (float)Math.random()*0.5f + 0.5f);
}
public static ArrayList<Wall> createMap(char[][] map, int wall_length) {
ArrayList<Wall> w = new ArrayList<>();
int width = map[0].length;
int height = map.length;
System.out.println(width + " " + height);
for (int i = 0; i < width; i++){
for (int j = 0; j < height; j++){
if (map[j][i] == 'x'){
w.add(new Wall (new Vector2f(i*wall_length, j*wall_length), new Vector2f((i+1)*wall_length, j*wall_length)
,new Vector2f((i+1)*wall_length, (j+1)*wall_length), new Vector2f(i*wall_length, (j+1)*wall_length)));
}
}
}
return w;
}
public void update (float d){
}
public void render (Graphics g){
g.setColor (color);
g.fill(shape);
}
public Shape getShape () {
return shape;
}
}
多边形扩展 class:
public class PolygonExt extends Polygon {
public static float[] getLineNormal (Polygon p, int index){
float[] result = new float[2];
float x1, x2, y1, y2;
int next_index = (index + 1) % p.getPointCount();
x1 = p.getPoint(index)[0];
y1 = p.getPoint(index)[1];
x2 = p.getPoint(next_index)[0];
y2 = p.getPoint(next_index)[1];
double angle = Math.atan2(y2-y1, x2-x1)+Math.PI/2d;
result[0] = (float) Math.cos(angle);
result[1] = (float) Math.sin(angle);
if (p.contains(x1+(x2-x1)/2 + result[0]*0.01f, y1+(y2-y1)/2 + result[1]*0.01f)){
result[0] *= -1;
result[1] *= -1;
}
return result;
}
public static Line getLine (Polygon p, int index){
int next_index = (index + 1) % p.getPointCount();
float x1, x2, y1, y2;
x1 = p.getPoint(index)[0];
y1 = p.getPoint(index)[1];
x2 = p.getPoint(next_index)[0];
y2 = p.getPoint(next_index)[1];
Line l = new Line (x1, y1, x2, y2);
return l;
}
}
在您的播放器 class 中,您首先使用这条线测试与墙壁的交点:
if (player_c.intersects(w.getShape())){
然后在 if
部分中,您似乎正在更新玩家的位置,直到它停止与此处的墙壁相交:
while (player_c.intersects(w.getShape())){
normal_x, normal_y;
w.getShape().getNormal(i)[0];
w.getShape().getNormal(i)[1];
x += 0.001 * normal_x;
pos.y += 0.001 * normal_y;
player_c = getShape();
}
这表明,在 Slick2D 执行其下一个 render() 循环之前,所有与墙壁交互的玩家移动都将在此循环内发生。这意味着任何玩家与墙的交互似乎都是立即发生的,这大概就是为什么它看起来像玩家跳来跳去的原因。
为了在屏幕上显示增量移动,您需要在 Slick2D 中每个 update() 循环增量更新玩家位置(即删除上面的 while 循环作为起点),以便每个增量变化都可以在接下来的 render() 循环中呈现到屏幕上。您还应该建立时间增量的使用,以便无论游戏速度有多快,动作都显得流畅 运行.
就玩家跳墙的原因而言,这表明您的碰撞检测逻辑有问题。你有没有想过使用物理引擎来为你做这件事?这将节省您编写一组已经存在于其他地方并且旨在处理碰撞检测的所有方面的库。我开始尝试编写自己的碰撞检测例程,但一旦我开始阅读有关如何正确执行此操作的信息(here is one of many websites outlining the basics - this has a section on circle and axis aligned bounding box [AABB] interactions, which I think covers your scenario) I quickly realised that it was a huge undertaking and it would be better to use a library. JBox2D is one example, which seems to be quite popular. I use dyn4j,尽管用户社区很小,但效果很好。
希望对您有所帮助。
我看了很多教程如何检测碰撞,但没有看到如何解决碰撞。我在做一个俯视游戏,玩家有圆形碰撞形状,而墙壁是各种多边形。
我正在使用 slick2d。我应该做的是,如果玩家与角落发生碰撞,我会按法线移动玩家,直到他不与角落发生碰撞。在我检查角落之后,我以类似的方式检查边缘。然而,我的播放器经常摇晃,突然转弯,偶尔会穿墙。
代码如下:
碰撞检测代码(播放器内部class):
public void checkCollision (ArrayList<Wall> walls) {
//pos is position vector
Shape player_c = getShape();
for (Wall w : walls){
if (player_c.intersects(w.getShape())){
for (int i = 0; i < w.getShape().getPointCount(); i++){
float point_x, point_y;
point_x = w.getShape().getPoint(i)[0];
point_y = w.getShape().getPoint(i)[1];
if (player_c.contains(point_x, point_y)){
while (player_c.intersects(w.getShape())){
float normal_x, normal_y;
normal_x = w.getShape().getNormal(i)[0];
normal_y = w.getShape().getNormal(i)[1];
pos.x += 0.001 * normal_x;
pos.y += 0.001 * normal_y;
player_c = getShape();
}
break;
} else {
if (player_c.intersects(PolygonExt.getLine((Polygon)w.getShape(), i))){
while (player_c.intersects(w.getShape())){
float[] normal;
normal = PolygonExt.getLineNormal((Polygon)w.getShape(), i );
pos.x += 0.001 * normal[0];
pos.y += 0.001 * normal[1];
player_c = getShape();
}
break;
}
}
}
}
}
}
墙 class:
public class Wall {
Polygon shape;
private Color color;
public Wall(Vector2f... points) {
shape = new Polygon();
for (int i = 0; i < points.length; i++){
shape.addPoint(points[i].x, points[i].y);
}
color = new Color((float)Math.random()*0.5f + 0.5f, (float)Math.random()*0.5f + 0.5f, (float)Math.random()*0.5f + 0.5f);
}
public static ArrayList<Wall> createMap(char[][] map, int wall_length) {
ArrayList<Wall> w = new ArrayList<>();
int width = map[0].length;
int height = map.length;
System.out.println(width + " " + height);
for (int i = 0; i < width; i++){
for (int j = 0; j < height; j++){
if (map[j][i] == 'x'){
w.add(new Wall (new Vector2f(i*wall_length, j*wall_length), new Vector2f((i+1)*wall_length, j*wall_length)
,new Vector2f((i+1)*wall_length, (j+1)*wall_length), new Vector2f(i*wall_length, (j+1)*wall_length)));
}
}
}
return w;
}
public void update (float d){
}
public void render (Graphics g){
g.setColor (color);
g.fill(shape);
}
public Shape getShape () {
return shape;
}
}
多边形扩展 class:
public class PolygonExt extends Polygon {
public static float[] getLineNormal (Polygon p, int index){
float[] result = new float[2];
float x1, x2, y1, y2;
int next_index = (index + 1) % p.getPointCount();
x1 = p.getPoint(index)[0];
y1 = p.getPoint(index)[1];
x2 = p.getPoint(next_index)[0];
y2 = p.getPoint(next_index)[1];
double angle = Math.atan2(y2-y1, x2-x1)+Math.PI/2d;
result[0] = (float) Math.cos(angle);
result[1] = (float) Math.sin(angle);
if (p.contains(x1+(x2-x1)/2 + result[0]*0.01f, y1+(y2-y1)/2 + result[1]*0.01f)){
result[0] *= -1;
result[1] *= -1;
}
return result;
}
public static Line getLine (Polygon p, int index){
int next_index = (index + 1) % p.getPointCount();
float x1, x2, y1, y2;
x1 = p.getPoint(index)[0];
y1 = p.getPoint(index)[1];
x2 = p.getPoint(next_index)[0];
y2 = p.getPoint(next_index)[1];
Line l = new Line (x1, y1, x2, y2);
return l;
}
}
在您的播放器 class 中,您首先使用这条线测试与墙壁的交点:
if (player_c.intersects(w.getShape())){
然后在 if
部分中,您似乎正在更新玩家的位置,直到它停止与此处的墙壁相交:
while (player_c.intersects(w.getShape())){
normal_x, normal_y;
w.getShape().getNormal(i)[0];
w.getShape().getNormal(i)[1];
x += 0.001 * normal_x;
pos.y += 0.001 * normal_y;
player_c = getShape();
}
这表明,在 Slick2D 执行其下一个 render() 循环之前,所有与墙壁交互的玩家移动都将在此循环内发生。这意味着任何玩家与墙的交互似乎都是立即发生的,这大概就是为什么它看起来像玩家跳来跳去的原因。
为了在屏幕上显示增量移动,您需要在 Slick2D 中每个 update() 循环增量更新玩家位置(即删除上面的 while 循环作为起点),以便每个增量变化都可以在接下来的 render() 循环中呈现到屏幕上。您还应该建立时间增量的使用,以便无论游戏速度有多快,动作都显得流畅 运行.
就玩家跳墙的原因而言,这表明您的碰撞检测逻辑有问题。你有没有想过使用物理引擎来为你做这件事?这将节省您编写一组已经存在于其他地方并且旨在处理碰撞检测的所有方面的库。我开始尝试编写自己的碰撞检测例程,但一旦我开始阅读有关如何正确执行此操作的信息(here is one of many websites outlining the basics - this has a section on circle and axis aligned bounding box [AABB] interactions, which I think covers your scenario) I quickly realised that it was a huge undertaking and it would be better to use a library. JBox2D is one example, which seems to be quite popular. I use dyn4j,尽管用户社区很小,但效果很好。
希望对您有所帮助。