【问题标题】:AndEngine: Handling collisions with TMX ObjectsAndEngine:处理与 TMX 对象的冲突
【发布时间】:2013-02-18 16:03:09
【问题描述】:

我现在设法加载了一个 tmx 地图我想创建精灵不能移动的障碍物,我这样恢复了障碍物:

try {
        final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, new ITMXTilePropertiesListener() {
            @Override
            public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {
                /* We are going to count the tiles that have the property "cactus=true" set. */
                if(pTMXTileProperties.containsTMXProperty("obstacle", "true")) {
                    //TMXTiledMapExample.this.mCactusCount++;
                    //coffins[coffinPtr++] = pTMXTile.getTileRow() * 15 + pTMXTile.getTileColumn();

                }
            }
        });

我如何处理与障碍物的碰撞,以防止玩家穿过障碍物(例如墙壁)?

【问题讨论】:

  • 你会使用 Astar 路径吗?

标签: android collision-detection andengine


【解决方案1】:

我相信您要问的是如何实现碰撞处理。明确一点:碰撞检测是您确定某物与其他物发生碰撞(重叠)的步骤。碰撞处理就是你,比如说,移动其中一个东西,使它不再重叠。在这种情况下,我假设我们已经通过了碰撞检测并开始进行碰撞处理,因为您在一个名为“onTMXTileWithPropertiesCreated”的方法中,我猜这意味着玩家在这样的瓷砖上。所以这就是这个想法,简单地说:

当由于玩家(或其他精灵)的移动,您检测到该精灵正在与您希望无法通过的精灵发生碰撞——用您的术语来说是“真实的”,您会想要将精灵向后移动一段距离,以防止它重叠。

使用矩形执行此操作非常简单。用其他形状做这件事会有点复杂。因为您正在使用 TMX 平铺地图,所以矩形现在可能可以使用。这是一个带有矩形的基本示例。

public boolean adjustForObstacle(Rect obstacle) {
    if (!obstacle.intersect(this.getCollisionRect())) return false;

    // There's an intersection. We need to adjust now.
    // Due to the way intersect() works, obstacle now represents the
    // intersection rectangle.

    if (obstacle.width() < obstacle.height()) {
        // The intersection is smaller left/right so we'll push accordingly.
        if (this.getCollisionRect().left < obstacle.left) {
            // push left until clear.
            this.setX(this.getX() - obstacle.width());
        } else {
            // push right until clear.
            this.setX(this.getX() + obstacle.width());
        }
    } else {
        if (this.getCollisionRect().top < obstacle.top) {
            // push up until clear.
            this.setY(this.getY() - obstacle.height());
        } else {
            // push down until clear.
            this.setY(this.getY() + obstacle.height());
        }
    }
    return true;
}

这样做的目的是计算重叠矩形并将精灵沿重叠的最小维度移动,使其不再重叠。由于您使用的是 AndEngine,因此您可以使用 IShape 中的 collidesWith() 方法,它比上述方法更优雅地检测碰撞。

【讨论】:

    【解决方案2】:

    因为我用这个

    if(pTMXTileProperties.containsTMXProperty("obstacle", "true")) {
                        //TMXTiledMapExample.this.mCactusCount++;
                        //coffins[coffinPtr++] = pTMXTile.getTileRow() * 15 + pTMXTile.getTileColumn();
                        //initRacetrackBorders2();
                         // This is our "wall" layer. Create the boxes from it
                            final Rectangle rect = new Rectangle(pTMXTile.getTileX()+10, pTMXTile.getTileY(),14, 14);
                            final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
                            PhysicsFactory.createBoxBody(mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
                            rect.setVisible(false);
                            mScene.attachChild(rect);
                    }
    

    玩得开心!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2015-03-30
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      相关资源
      最近更新 更多