利用TiledMap进行Libgdx碰撞检测 [英] Libgdx Collision Detection with TiledMap

查看:633
本文介绍了利用TiledMap进行Libgdx碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力通过平铺地图实现碰撞检测系统。我有一个2D的口袋妖怪风格游戏,有一个平铺的地图呈现。具体来说,我的平铺地图.tmx文件中有一个碰撞图层,我希望与播放器和其他实体进行交互。我的问题是如何将播放器精灵(扩展Sprite类)连接到平铺贴图的碰撞图层并导致两者之间发生碰撞。任何建议都表示赞赏。

I'm struggling with implementing a collision detection system through the tiledmap. I have a 2d "pokemon style" game that has a tiled map rendered. Specifically, I have a 'collision' layer in my tiled map .tmx file that I want to interact with the player and other entities. My question is how do I connect the player sprite (extends Sprite class) to the 'collision' layer of the tiledmap and cause collision between the two. Any advice is appreciated.

推荐答案

首先你的播放器应该是不是扩展Sprite ,因为你的播放器通常远远超过 Sprite 。它可能包含几个精灵甚至动画。保持精灵作为玩家的财产。

First of all your Player should probably not extend Sprite, because your player is usually much more than a Sprite. It probably consists of several sprites or even Animations. Keep a sprite as a property of the player.

这个问题本身已经被多次解决。您通常需要执行以下步骤:

The question itself has already been adressed several times. You usually need the following steps:


  1. 在地图中查找碰撞图层

  2. 提取所有对象来自这一层

  3. 检查每个对象是否发生碰撞

在代码中这可能看起来像有点像这样:

In code this might look a bit like this:

int objectLayerId = 5;
TiledMapTileLayer collisionObjectLayer = (TiledMapTileLayer)map.getLayers().get(objectLayerId);
MapObjects objects = collisionObjectLayer.getObjects();

// there are several other types, Rectangle is probably the most common one
for (RectangleMapObject rectangleObject : objects.getByType(RectangleMapObject.class)) {

    Rectangle rectangle = rectangleObject.getRectangle();
    if (Intersector.overlaps(rectangle, player.getRectangle()) {
        // collision happened
    }
}

您可能感兴趣的更多链接:

Some more links which you might be interested in:

  • Java Tiled Map Game (LibGDX) | Episode 4 - collision detection
  • Java Tiled Map Game (LibGDX) | Episode 4 update - better collision detection implementation
  • Android Game Development with libgdx – Collision Detection, Part 4
  • SuperKoalio example game with TiledMaps and collision

这篇关于利用TiledMap进行Libgdx碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆