LibGDX:矩形是否正确碰撞/重叠?

时间:2014-06-21 19:03:08

标签: java libgdx collision rectangles

玩家等级

public class Player extends Sprite implements InputProcessor {
    public Vector2 velocity = new Vector2();
    private float speed = 500;
    public Rectangle rectangle;
    public Player(Sprite sprite){
        super(sprite);
        this.rectangle = sprite.getBoundingRectangle();
    }
    public void draw(SpriteBatch spriteBatch){
        update(Gdx.graphics.getDeltaTime());
        super.draw(spriteBatch);
    }
    public void update(float delta) {
        rectangle = new Rectangle(getX() + velocity.x * delta,0,rectangle.getWidth(),rectangle.getWidth());
        setX(getX() + velocity.x * delta);
    }
}

PlayScreen类

public class PlayScreen implements Screen {
    private Player player;
    private OrthographicCamera camera;
    private OrthogonalTiledMapRenderer renderer;
    private TiledMap map;
    private Rectangle rightRectangle, leftRectangle, playerRectangle;
    //private ShapeRenderer shapeRenderer;
    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        renderer.render();
        renderer.getSpriteBatch().begin();
        player.draw(renderer.getSpriteBatch());
        renderer.getSpriteBatch().end();

        //shapeRenderer.begin(ShapeType.Filled);
        //shapeRenderer.setColor(0, 1, 0, 1);
        //shapeRenderer.rect(
        //  player.getX() + player.velocity.x * delta, 0, 
        //  player.rectangle.getWidth(), player.rectangle.getHeight());
        //shapeRenderer.end();
    }
    @Override
    public void resize(int width, int height) {
        camera.viewportWidth = width;
        camera.viewportHeight = height;
        camera.update();
    }
    @Override
    public void show() {
        camera = new OrthographicCamera();
        map = new TiledMap();
        renderer = new OrthogonalTiledMapRenderer(map);
        //shapeRenderer = new ShapeRenderer();

        player = new Player(new Sprite(new Texture("img/player.png")));
        rightRectangle = new Rectangle(1280,0,0,720);
        leftRectangle = new Rectangle(0,0,0,720);

        boolean wallLeft = leftRectangle.overlaps(player.rectangle);
        boolean wallRight = rightRectangle.overlaps(player.rectangle);

        if(wallLeft){
            System.out.println("wallLeft Overlap");
            player.velocity.x = 0;
        }
        else if(wallRight){
            System.out.println("wallRight Overlap");
            player.velocity.x = 0;
        }
        player.setPosition(
            Gdx.graphics.getWidth()/2f - player.getWidth()/2f,
            Gdx.graphics.getHeight()/2f - player.getHeight()/2f 
                - Gdx.graphics.getHeight()/5f);
    }
}

似乎没有正确碰撞。 rightRectangleleftRectangle是我的屏幕边界。当我使用shapeRenderer时,它会生成ShapeRendered矩形,它将跟随我的玩家。但是,我相信我的player.rectangle由于某种原因根本不动,导致它不与我的边界相撞。任何帮助将不胜感激!

*如果leftRectangle和rightRectangle的宽度不是0(即1或0.0001),打开应用程序后,它会自动打印出“wallLeft Overlap”一次,玩家仍然可以移动自如。当它为0时,没有任何打印输出,玩家仍然可以自由移动。所以我认为leftRectangle和rightRectangle的宽度不是问题。

0 个答案:

没有答案