Столкновение мяча и прямоугольников, LibGDX

Пытаюсь сделать отражение мяча от стен и прямоугольников. Со стенами этот код работает нормально, а вот с прямоугольниками у меня проблемы. Он просто отражается влево, а затем вправо через каждый 1 пиксель, пока не закончится столкновение. Пример:

Пример изображения

Что я делаю не так?

Мяч:

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

person Paul    schedule 18.03.2014    source источник


Ответы (1)


Заменять

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

должен быть в конце метода обновления. Это должно решить проблему.

Надеюсь это поможет.

person Tanmay Patil    schedule 18.03.2014
comment
То же самое. единственное изменение - теперь расстояние между шаром и прямоугольником больше. Все равно спасибо =) - person Paul; 18.03.2014
comment
Спасибо! =D Я только что заменил bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight()); круг.набор(позиция.х, позиция.у, круг.радиус); Теперь это работает - person Paul; 18.03.2014
comment
Рад узнать, что это помогло .. Я полагаю, вы могли бы принять ответ сейчас :) - person Tanmay Patil; 19.03.2014