Libgdx 多边形的交集

4
我正在通过为Mario Zechner的开源跳跃游戏添加一些功能来学习libgdx。我试图创建一些带有角度的平台,并遇到了旋转矩形的碰撞检测问题。
我按照这个解决方案,使用了多边形和我的矩形边界。
出于测试目的,我还没有设置角度。我只是想验证Bob是否能正确地从平台上跳下来。但由于某种原因,这并不起作用。边界要么太靠左,要么在平台上方,要么根本不存在。我没有正确设置多边形吗? 使用Box2d会更容易吗?我没有任何经验,我想知道是否过于复杂了。
public PlatformClient(int platformType, float x, float y) {
    this.platformType = platformType;
    float x1 = x - Platform.PLATFORM_WIDTH/2;
    float y1 = y + Platform.PLATFORM_HEIGHT/2;

    this.polyBounds = new Polygon(new float[]{x1, y1, x1+Platform.PLATFORM_WIDTH, y1, x1+Platform.PLATFORM_WIDTH, y1-Platform.PLATFORM_HEIGHT, x1, y1-Platform.PLATFORM_HEIGHT}); 
    polyBounds.setPosition(x-Platform.PLATFORM_WIDTH/2, y-Platform.PLATFORM_HEIGHT/2);
}

class Platform {
   public static final float PLATFORM_WIDTH = 2f;
   public static final float PLATFORM_HEIGHT = 0.35f;

}

在Bob类中,当他移动时更新多边形边界:
public void update(float deltaTime) {   
    ...
    position.add(velocity.x * deltaTime, velocity.y * deltaTime);
    bounds.x = position.x - BOB_WIDTH / 2;
    bounds.y = position.y - BOB_HEIGHT / 2;
    float newX = position.x - BOB_WIDTH / 2;
    float newY = position.y - BOB_HEIGHT / 2;
    polyBounds.setVertices(new float[]{ 
            newX, newY, 
            newX+BOB_WIDTH, newY, 
            newX+BOB_WIDTH, newY-BOB_HEIGHT,
            newX, newY-BOB_HEIGHT});
}

在世界级:

 private void checkPlatformCollisions () {
   int len = platforms.size();
   for (int i = 0; i < len; i++) 
   {
      PlatformClient platform = platforms.get(i);
      if (bob.position.y >= platform.position.y) 
      {
         if(Intersector.overlapConvexPolygons(bob.polyBounds, platform.polyBounds))
         {
               System.out.println("it overlaps");
               // jump off platform
         }
      }
   }
 }

编辑

由于形状渲染器的帮助,我能够正确地设置多边形。我在上面的代码中修复了一些+,-问题。但是下面的代码:Intersector.overlapConvexPolygons()仍然无法正常工作(参见图像)。它在多边形接触之前就跳动或根本不跳动。 还有其他想法吗?

enter image description here

这是我绘制Bob和平台的多边形,它们明显重叠。

 public void render() {
    shapeRenderer.setProjectionMatrix(cam.combined);
    shapeRenderer.begin(ShapeType.Line);
    for(int i=0; i<world.platforms.size(); i++) {

        shapeRenderer.setColor(1, 0, 0, 1);

        shapeRenderer.polygon(world.platforms.get(i).polyBounds.getVertices());
        shapeRenderer.polygon(world.bob.polyBounds.getVertices());
    }
    shapeRenderer.end();
   }

你尝试过使用ShapeRenderer打印形状以进行调试吗? - Robert P
我正在使用SpriteBatch而不是ShapeRenderer。根据这个,我不应该混合使用这两个。 - user2246120
1
我知道你不应该混合使用它们。但是为了调试目的,最好使用ShapeRenderer来绘制精确的形状。使用spritebatch绘制的图像可能比您的形状更大、更小或甚至位于另一个位置。因此,只需调用spritebatch.end(),然后shaperenderer.begin(Shapetype.LINE),再绘制多边形,最后调用shaperenderer.end()和spritebatch.start()即可。我曾经尝试过多边形碰撞检测,但由于我的多边形非凸,所以它没有起作用。所以最好你自己试一下! - Robert P
谢谢,我已经成功设置了多边形。但是它仍然无法正常工作(请参见我在原帖中的编辑)。 - user2246120
1
因为如果你使用spritebatch渲染,你需要手动设置位置、旋转等参数。而且形状总是依赖于图像。我曾经遇到过将多边形的点按错误顺序设置导致形状出现了这样的问题:__/ __,这样就无法进行碰撞检测了。请回复并标记问题已解决。谢谢。 - Robert P
显示剩余3条评论
1个回答

3

好的,我通过移除

polyBounds.setPosition(x-Platform.PLATFORM_WIDTH/2, y-Platform.PLATFORM_HEIGHT/2);

构造函数中的代码解决了问题。现在碰撞检测正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接