문제

I'm trying to make a reflection of the ball from the walls and rectangles. With walls this code works fine, but with rectangles I have problems. It just reflect to the left and then to the right every 1 pixel until collision ends. Example:

Picture Example

what am I doing wrong?

Ball:

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

Rectangle:

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;
    } 
}
도움이 되었습니까?

해결책

Replace

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

with

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

Also,

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

should be at the end of update method. This should solve the issue.

Hope this helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top