开发者

Java Collision Detection with TILED maps

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开发者_JAVA百科 have no clue where to start? :( Thanks.


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);
        }
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜