gpt4 book ai didi

java - 使用 Java、Slick2D 和平铺 map 编辑器进行碰撞检测

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:42 24 4
gpt4 key购买 nike

很长一段时间以来,我一直专注于碰撞检测以及如何处理它。我需要帮助了解如何使用 Tiled map 编辑器进行碰撞检测。我用播放器、相机和键盘移动解析并显示了 TMX 文件。我不确定如何处理碰撞。我的 map 有 2 层,一层用于您可以在其上行走的草地和瓷砖,另一层是 objectLayer。我见过有人说我应该遍历对象层中的所有图 block 并为它们分配矩形。我不知道该怎么做,我正在寻找一些见解。

我的主要问题是:如何遍历图层并获取所有图 block 并为其分配矩形。

游戏类:

    public class Game extends BasicGameState {

Player player;
Camera cam;
Map map = new Map();

public void init(GameContainer container, StateBasedGame sbg) throws SlickException {
map.init();
cam = new Camera(0, 0);
player = new Player(new Image("res/textures/player.png"), container.getWidth() / 2, container.getHeight() / 2, 32, 32);
}

public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
cam.tick(player);
player.update(container, delta, map);
}

public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException {
g.translate(-cam.getX(), -cam.getY());
map.render();
player.render(g);
g.translate(cam.getX(), cam.getY());
}

public int getID() {
return 1;
}

玩家类别:

    public class Player {

float x, y;

int width, height;
double velX = 0.4;
double velY = 0.4;

Image img;

public Player(Image img, float x, float y, int width, int height) {
this.img = img;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

public void init() throws SlickException {

}

public void update(GameContainer container, int delta, Map map) {
Input input = container.getInput();

if (input.isKeyDown(Input.KEY_D)) x += velX;
if (input.isKeyDown(Input.KEY_A)) x -= velX;
if (input.isKeyDown(Input.KEY_W)) y -= velY;
if (input.isKeyDown(Input.KEY_S)) y += velY;

}

public void render(Graphics g) {
//g.scale(-2, -2);
g.drawImage(img, x, y);
}

public float getX() {
return x;
}

public float getY() {
return y;
}

map 类:

    public class Map {

TiledMap map;

public void init() throws SlickException {
map = new TiledMap("res/map/zenith.tmx");
}

public void render() throws SlickException {
map.render(0, 0);
}

最佳答案

我不太熟悉获取磁贴的具体方法,但它肯定在 Javadoc 中。 http://slick.ninjacave.com/javadoc/

要检查与玩家的碰撞,您需要更改移动以检查玩家的新位置是否会发生碰撞,否则您会卡在他们身上。

public void update(GameContainer container, int delta, Map map) {
Input input = container.getInput();
float dx = 0, dy = 0;
// add to dx and dy instead of moving the player straight away.
if (input.isKeyDown(Input.KEY_D)) dx += velX;
if (input.isKeyDown(Input.KEY_A)) dx -= velX;
if (input.isKeyDown(Input.KEY_W)) dy -= velY;
if (input.isKeyDown(Input.KEY_S)) dy += velY;
// here we check the collisions, you can fiddle with this if statement and it'll behave differently.
if(dx != 0 || dy != 0){
if(map.checkCollision(x+dx,y+y){
x+=dx;
y+=dy;
}
}
}

至于检查碰撞本身,您需要在 map 类中使用一个新方法来为玩家创建一个矩形,并检查 map 类中保存碰撞的二维数组。您可以在 map 初始化时填充此数组,方法是在您需要的图层中循环遍历 Tiled map ,并检查该图 block 是否与 getTile(x,y) 发生冲突。如果图 block 具有正确的碰撞属性,您可以在二维数组中的该位置创建一个矩形,如下所示:

collisionArray[x][y] = new Rectangle(x,y,TILEWIDTH,TILEHEIGHT);

您现在可以检查内部碰撞

map.checkCollision(x,y)

并使用

为玩家创建了一个矩形
Rectangle player = new Rectangle(x,y,PLAYERWIDTH,PLAYERHEIGHT);

将它们放在一起,然后您可以使用 .intersect(r) 检查与碰撞数组中任何图 block 的交集,如前所述,并且 map.collision 可以返回 true 给应该停止玩家移动的玩家。

我希望这不是太复杂,javadoc 非常有帮助,Kevin Glass 写了一篇关于仅使用 2D 数组检查碰撞的文章,具有不同的碰撞框大小。 http://www.cokeandcode.com/main/tutorials/tile-maps/

关于java - 使用 Java、Slick2D 和平铺 map 编辑器进行碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com