使用 TILED 地图进行 Java 碰撞检测

发布于 2024-12-03 15:17:26 字数 157 浏览 2 评论 0原文

我正在制作一款游戏,遇到了一个问题。 我进行了碰撞检测,当多边形接触瓷砖时,结果为真。 虽然这非常适合让玩家不要走墙(真的吗?)。 当我施加重力时,它应该用相同的方法停止,但这造成了问题。 它一直下落,直到撞到地板,但随后你也无法再行走,所以我需要另一个碰撞检测。 我不知道从哪里开始? :( 谢谢。

I was making a game and came across a problem.
I made a collision detection that says true when the poly touches a tile.
While that was perfect for making the player not to walk (true?) a wall.
When i applied gravity it should stop with the same method BUT that made a problem.
it kept falling till it hitter the floor but then you also can't walk anymore so i need another collision detection.
and i have no clue where to start? :(
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮湿 2024-12-10 15:17:26

将所有图块加载到 ArrayList 中。然后,您可以使用 for-each 循环对玩家的所有图块应用碰撞检测。请注意,以下示例可能不会声明工作所需的所有内容,但它应该可以帮助您了解这种碰撞检测方法的工作原理,并允许您将其实施到游戏中。

Tile.java

    import java.awt.Rectangle;
    private int tileWidth = 32;
    private int tileHeight = 32;
    private int x;
    private int y;
    public class Tile() {
        public Tile(int tx, int ty) {
            x = tx * tileWidth;
            y = ty * tileHeight;
        }
        public Rectangle getBounds() {
            return new Rectangle(x, y, tileWidth, tileHeight);
        }
    }

CheckCollision.java

    import java.awt.Rectangle;
    public class CheckCollision {
        public boolean isColliding(Player player, Tile tile) {
            Rectangle pRect = player.getBounds();
            Rectangle tRect = tile.getBounds();
            if(pRect.intersects(tRect)) {
                return true;
            } else {
                return false;
            }
        }
    }

Player.java

    import java.awt.Rectangle;
    import java.util.ArrayList;
    public class Player {
        public void move(ArrayList<Tile> tiles) {
            y -= directionY; //falling
            for(Tile t: tiles) { // for all of the Tiles in tiles, do the following
                Tile next = t;
                if(CheckCollision.isColliding(this, next) {
                    y += directionY; //stop falling
                }
            }
            x -= dx; // move your x
            for(Tile t: tiles) { // for all of the Tiles in tiles, do the following
                Tile next = t;
                if(CheckCollision.isColliding(this, next) {
                    x += directionY; //move back if collides                  }
            }

        }

        public Rectangle getBounds() {
            return new Rectangle(playerX, playerY, playerWidth, playerHeight);
        }
    }

Graphics.java(绘制图块和播放器的类)

    import java.awt.ActionListener;
    import java.util.ArrayList;
    public class Graphics extends JPanel implements ActionListener {
        public ArrayList<Tile> tiles = new ArrayList();
        Player player = new Player();
        public JPanel() {
            tiles.add(new Tile(0, 0)); //adds a Tile at X:0 Y:0 to ArrayList tiles
        }

        @Override
        public void ActionPerformed(ActionEvent e) {
            player.move(tiles);
        }
    }

Load all of your tiles into an ArrayList. Then you can use a for-each loop to apply collision detection to all of the tiles with your player. Please note that the following example may not declare everything necessary to work, but it should help you understand how this method of collision detection works and allow you to implement it into your game.

Tile.java

    import java.awt.Rectangle;
    private int tileWidth = 32;
    private int tileHeight = 32;
    private int x;
    private int y;
    public class Tile() {
        public Tile(int tx, int ty) {
            x = tx * tileWidth;
            y = ty * tileHeight;
        }
        public Rectangle getBounds() {
            return new Rectangle(x, y, tileWidth, tileHeight);
        }
    }

CheckCollision.java

    import java.awt.Rectangle;
    public class CheckCollision {
        public boolean isColliding(Player player, Tile tile) {
            Rectangle pRect = player.getBounds();
            Rectangle tRect = tile.getBounds();
            if(pRect.intersects(tRect)) {
                return true;
            } else {
                return false;
            }
        }
    }

Player.java

    import java.awt.Rectangle;
    import java.util.ArrayList;
    public class Player {
        public void move(ArrayList<Tile> tiles) {
            y -= directionY; //falling
            for(Tile t: tiles) { // for all of the Tiles in tiles, do the following
                Tile next = t;
                if(CheckCollision.isColliding(this, next) {
                    y += directionY; //stop falling
                }
            }
            x -= dx; // move your x
            for(Tile t: tiles) { // for all of the Tiles in tiles, do the following
                Tile next = t;
                if(CheckCollision.isColliding(this, next) {
                    x += directionY; //move back if collides                  }
            }

        }

        public Rectangle getBounds() {
            return new Rectangle(playerX, playerY, playerWidth, playerHeight);
        }
    }

Graphics.java (Your class that draws the tiles and player)

    import java.awt.ActionListener;
    import java.util.ArrayList;
    public class Graphics extends JPanel implements ActionListener {
        public ArrayList<Tile> tiles = new ArrayList();
        Player player = new Player();
        public JPanel() {
            tiles.add(new Tile(0, 0)); //adds a Tile at X:0 Y:0 to ArrayList tiles
        }

        @Override
        public void ActionPerformed(ActionEvent e) {
            player.move(tiles);
        }
    }
时光磨忆 2024-12-10 15:17:26

我的《我的世界》克隆版也遇到了类似的问题。问题是我的代码每帧都会检测到与地板的碰撞,我通过取消所有 3 个维度的运动来响应这一碰撞。我改变了它,所以我只取消了该运动的垂直分量,并且效果很好。

I had a similar problem with my Minecraft clone. The problem was that my code kept detecting a collision with the floor every frame, which I was responding to by cancelling motion in all 3 dimensions. I changed it so I only cancelled the vertical component of that motion, and it worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文