java游戏 - 光滑 - 碰撞处理

时间:2012-11-21 21:25:23

标签: java collision-detection slick2d tiles-game

如何在光滑中管理碰撞?

我创建了地图,墙上的每个瓷砖都有选项block = true;

如何实现碰撞,如果我撞墙,我可以移动它? 此外,如果我到达地图的边缘,阻止移动。

package javagame;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class Play extends BasicGameState {

    Animation movingUp, movingDown, movingLeft, movingRight;
    Animation player;
    int[] duration = {200, 200, 200};
    float playerX = 0;
    float playerY = 0;
    float cameraX;
    float cameraY;
    float screenWidth;
    float screenHeight;
    private TiledMap map = null;
    private static final float SPEED = 0.1f;

    private boolean[][] blocked;

    public Play(int state, int x, int y) {
        playerX = (float) x;
        playerY = (float) y;
    }

    private boolean blocked(float x, float y) {
        return blocked[(int) x][(int) y];
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

        map = new TiledMap("map/map.tmx");

        // build a collision map based on tile properties in the TileD map 
        blocked = new boolean[map.getWidth()][map.getHeight()];
        for (int x = 0; x < map.getWidth(); x++) {
            for (int y = 0; y < map.getHeight(); y++) {
                int tileID = map.getTileId(x, y, 0);
                String value = map.getTileProperty(tileID, "blocked", "false");
                if ("true".equals(value)) {
                    blocked[x][y] = true;
                }
            }
        }

        Image[] walkUp = {
            new Image("graphics/player/up0.png"),
            new Image("graphics/player/up1.png"),
            new Image("graphics/player/up2.png")
        };

        Image[] walkDown = {
            new Image("graphics/player/down0.png"),
            new Image("graphics/player/down1.png"),
            new Image("graphics/player/down2.png")
        };
        Image[] walkLeft = {
            new Image("graphics/player/left0.png"),
            new Image("graphics/player/left1.png"),
            new Image("graphics/player/left2.png")
        };
        Image[] walkRight = {
            new Image("graphics/player/right0.png"),
            new Image("graphics/player/right1.png"),
            new Image("graphics/player/right2.png")
        };

        movingUp = new Animation(walkUp, duration, false);
        movingDown = new Animation(walkDown, duration, false);
        movingLeft = new Animation(walkLeft, duration, false);
        movingRight = new Animation(walkRight, duration, false);

        player = movingDown;

    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

        screenWidth = gc.getWidth();
        screenHeight = gc.getHeight();

        cameraX = (screenWidth / 2) - (playerX / 2);
        cameraY = (screenHeight / 2) - (playerY / 2);

        map.render((int) playerX, (int) playerY);

        player.draw(cameraX, cameraY);

        g.drawString("X: " + playerX + "\nY: " + playerY, 520, 20);
        g.resetTransform();

    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
            player = movingUp;
            playerY += delta * SPEED;
            player.update(delta);
        } else if (input.isKeyDown(Input.KEY_DOWN) || input.isKeyDown(Input.KEY_S)) {
            player = movingDown;
            playerY -= delta * SPEED;
            player.update(delta);
        } else if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_A)) {
            player = movingLeft;
            playerX += delta * SPEED;
            player.update(delta);
        } else if (input.isKeyDown(Input.KEY_RIGHT) || input.isKeyDown(Input.KEY_D)) {
            player = movingRight;
            playerX -= delta * SPEED;
            player.update(delta);
        }

    }

}

以下是示例源代码(NetBeans项目):

https://www.box.com/s/9nicp0n067de632kcpj3

2 个答案:

答案 0 :(得分:2)

在更新时,您只需检查阵列即可。如果是墙不更新。如果是边框,请不要更新。

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    Input input = gc.getInput();
    float yChange = 0, xChange=0;
    boolean capturedInput = false;
    if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
        player = movingUp;
        yChange += delta * SPEED;
        capturedInput = true;
    } else if (input.isKeyDown(Input.KEY_DOWN) || input.isKeyDown(Input.KEY_S)) {
        player = movingDown;
        yChange -= delta * SPEED;
        capturedInput = true;
    } else if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_A)) {
        player = movingLeft;
        xChange += delta * SPEED;
        capturedInput = true;
    } else if (input.isKeyDown(Input.KEY_RIGHT) || input.isKeyDown(Input.KEY_D)) {
        player = movingRight;
        xChange -= delta * SPEED;
        capturedInput = true;
    }
    if(capturedInput==true && !blocked(playerX+xChange,playerY+yChange)){
        playerX += xChange;
        playerY += yChange;
        player.update(delta);
    }
}

private boolean blocked(float x, float y) {
    return blocked[(int) x][(int) y];
}

差不多,在你先检查它之前不要更改值,然后在一切正常的情况下更改播放器值。

编辑:我添加了一个变量captureInput,这会让我知道我们实际上有一个keyDown事件。关于此,有两条评论。我认为这是一个“黑客”,使您的代码工作,而不是最美丽。我个人会尝试向窗口添加一个事件监听器来监听按键点击,当点击一个键时我会改变位置信息,一直到帧刷新并且会更新,或者只是在现在有更改时刷新通过调用repaint()。

有很多教程可以用来听onKeyDown。你的代码应该现在可以更好地工作,并且不需要手动捕获边缘就像你做的那样,你应该能够在布尔块数组的边缘绘制一个墙,如果没有你可以继续使用你的方法。

答案 1 :(得分:0)

尝试从XY位置乘以图块尺寸创建块的矩形,然后使用intersects()方法验证它是否与您的实体发生碰撞,如{ {3}}

我希望这对你有用。