【问题标题】:Detect collision between Actors检测 Actor 之间的碰撞
【发布时间】:2015-10-07 18:25:35
【问题描述】:

我正在尝试在我的Player(演员)和我的Obstacle(演员)之间实现碰撞检测器,我想知道在它们之间执行collision detection 的最佳方法是什么。我看到一些用户说他们在课堂上创建了一个Rectangle object 并每帧更新它的边界,但我不知道这是否是执行此操作的最佳方法(我也在尝试这样做,但我的碰撞检测器方法在我的玩家接触障碍物之前触发)。

这是我要检查的内容:

public boolean touchedWall() {
    // Loop for the obstacles
    for (Obstacle obstacle : this.world.getObstacles()) {
        // Check if player collided with the wall
        if (this.bounds.overlaps(obstacle.getBounds())) {
            Gdx.app.log(Configuration.TAG, "collided with " + obstacle.getName());              
            return true;
        }
    }   
    return false;
}

这是方法触发的地方(它应该在玩家边界撞墙时触发):

【问题讨论】:

  • “this.bounds.overlaps(obstacle.getBounds())”中引用的“边界”类型是什么?
  • 在这两个演员中我都这样做了:private Rectangle bounds。这是你想知道的吗?我不知道为什么会这样,因为我做了 Rectangle 的 X 位置和演员边界的 X 位置之间的差异,结果是 0。

标签: libgdx collision actor


【解决方案1】:

我想通了!我没有使用this.bounds.set(x, y, width, height),而是像这样使用this.bounds.set(x, y, height, width)

this.bounds.set(this.getX(), this.getY(), this.getHeight(), this.getWidth());

对不起,我的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2010-12-27
    相关资源
    最近更新 更多