球和矩形之间的碰撞,LibGDX

时间:2014-03-18 15:57:04

标签: java libgdx collision

我试图从墙壁和矩形反射球。有了墙,这个代码工作正常,但有矩形我有问题。它只是向左反射,然后向右反射每1个像素,直到碰撞结束。例如:

  

Picture Example http://i58.tinypic.com/eprep0.png

我做错了什么?

球:

public class Ball {

    private int DEFAULT_SPEED = 2;
    private double angle;
    private static final int PI = 180;
    private int mAngle;
    private Rectangle bounds;
    private Circle circle;
    private Vector2 position;
    Player player;
    Block block;

    public Ball(Vector2 position, Block block) {
        this.position = position;
        this.block = block;
        mAngle = getRandomAngle();
        bounds = new Rectangle(position.x, position.y, Gdx.graphics.getWidth() / 20, Gdx.graphics.getHeight() / 15);
        circle = new Circle(position.x, position.y, Gdx.graphics.getWidth() / 26);

    }

    // update moves
    public void update() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight());
        circle.set(position.x, position.y, circle.radius);

        double angle = Math.toRadians(mAngle);


        position.x += 2*(int)Math.round(DEFAULT_SPEED * Math.cos(angle));
        position.y += 2*(int)Math.round(DEFAULT_SPEED * Math.sin(angle));

        if(position.x >=  Gdx.graphics.getWidth() - circle.radius){
            reflectVertical();
        } else if(position.x <=  0){
            reflectVertical();
        }
        if(position.y >=  Gdx.graphics.getHeight()- circle.radius){
            reflectHorizontal();
        } else if(position.y <=  0){
            reflectHorizontal();
        } else if(bounds.overlaps(block.getBounds())){
            reflectHorizontal();
            System.out.println("BLOCK");
        }

    }
    // update |
    public void reflectVertical(){
        if(mAngle > 0 && mAngle < PI){
            mAngle = PI - mAngle;
        } else {
            mAngle = 3 * PI - mAngle;
        }
    }

    // update -
    public void reflectHorizontal(){
        mAngle = 2 * PI - mAngle;
    }

    private int getRandomAngle() {
        Random rnd = new Random(System.currentTimeMillis());

        return rnd.nextInt(1) * PI + PI / 2 + rnd.nextInt(15) + 285;
    }

    public double getAngle() {
        return angle;
    }

    public void setAngle(double angle) {
        this.angle = angle;
    }

    public int getDEFAULT_SPEED() {
        return DEFAULT_SPEED;
    }

    public void setDEFAULT_SPEED(int dEFAULT_SPEED) {
        DEFAULT_SPEED = dEFAULT_SPEED;
    }

    public Circle getCircle() {
        return circle;
    }

    public void setCircle(Circle circle) {
        this.circle = circle;
    }

    public Rectangle getBounds() {
        return bounds;
    }

    public void setBounds(Rectangle bounds) {
        this.bounds = bounds;
    }

    public Vector2 getPosition() {
        return position;
    }
}

矩形:

public class Block {

    private Rectangle bounds;
    private Vector2 position;

    public Block(Vector2 position) {
        this.position = position;

        bounds = new Rectangle(position.x, position.y, Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 12);
    }

    public void update() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight());
    }

    public Rectangle getBounds() {
        return bounds;
    }

    public void setBounds(Rectangle bounds) {
        this.bounds = bounds;
    }

    public Vector2 getPosition() {
        return position;
    } 
}

1 个答案:

答案 0 :(得分:0)

替换

bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight());

bounds.set(position.x - bounds.getWidth() / 2,
                        position.y - bounds.getHeight() / 2,
                        bounds.getWidth(), bounds.getHeight());

此外,

bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight());
circle.set(position.x, position.y, circle.radius);

应该在更新方法的末尾。这应该可以解决问题。

希望这有帮助。