Fernando A.W.
Fernando A.W.

Reputation: 273

Java BounceBall Ball Move

I have a question. I'm not getting make the balls escape from the mouse pointer. All balls go to the left corner when the mouse pointer enters the screen. What am I doing wrong? Any tips??

My full code: Java BounceBall mouse event

Or

http://ideone.com/vTGzb7

Method with problem:

public void move(Ball ball, Point mouse) {

    try {
        Point p = ball.getLocation();
        Point speed = ball.getSpeed();
        Dimension size = ball.getSize();

        int vx = speed.x;
        int vy = speed.y;

        int x = p.x;
        int y = p.y;

        // ----------------------
        if (mouse != null) {

            int xDistance = Math.abs(x + size.width - mouse.x);
            int yDistance = Math.abs(y + size.height - mouse.y);

            if (xDistance < yDistance) {
                if (x + size.width < mouse.x) {
                    if (vx > 0) {
                        vx *= -1;
                    }
                } else {
                    if (vx > 0) {
                        vx *= -1;
                    }
                }
            } else {
                if (y + size.height < mouse.y) {
                    if (vy > 0) {
                        vy *= -1;
                    }
                } else {
                    if (vy > 0) {
                        vy *= -1;
                    }
                }
            }

        }
        // ----------------------

        if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
            vx *= -1;
        }
        if (y + vy < 0
                || y + size.height + vy > getParent().getHeight()) {
            vy *= -1;
        }
        x += vx;
        y += vy;

        ball.setSpeed(new Point(vx, vy));
        ball.setLocation(new Point(x, y));

    } catch (Exception e) {
        e.printStackTrace();
    }

}

For some balls it works fine. They hit in the mouse pointer and change your direction. But the majority goes to the corner of the screen.

Thank You.

Upvotes: 1

Views: 270

Answers (1)

Fernando A.W.
Fernando A.W.

Reputation: 273

Problem Solved...

Problem: The bubbles were locked in the upper corner of the screen. And do not hit the mouse pointer.

Solution: I calculated the distance from the X and Y position relative to the bubble diameter and the mouse pointer. For collision.

int xDistance = Math.abs((x + (diameter / 2)) - mouse.x);
int yDistance = Math.abs((y + (diameter / 2)) - mouse.y);

Then calculated the X and Y radius of the bubbles.

int radiusX = (size.width / 2);
int radiusY = (size.height / 2);

Finally, I changed the IF to check the relationship between the distance of the bubble radius. Changing your direction.

if (xDistance <= radiusX && yDistance <= radiusY) {
    if (xDistance < yDistance) {
        vx *= -1;
    } else {
        vy *= -1;
    }

    System.out.println("Hit!");
}

New Move Method:

public void move(Ball ball, Point mouse) {

    try {
        Point p = ball.getLocation();
        Point speed = ball.getSpeed();
        Dimension size = ball.getSize();

        int diameter = ball.dimeter;

        int vx = speed.x;
        int vy = speed.y;

        int x = p.x;
        int y = p.y;

        int radiusX = (size.width / 2);
        int radiusY = (size.height / 2);

        // ----------------------

        if (mouse != null) {
            int xDistance = Math.abs((x + (diameter / 2)) - mouse.x);
            int yDistance = Math.abs((y + (diameter / 2)) - mouse.y);

            System.out.printf("b(%d, %d) m(%d, %d) dx(%d, %d)\n", x, y,
                    mouse.x, mouse.y, (x + vx) - mouse.x, (y + vy)
                            - mouse.y);

            if (xDistance <= radiusX && yDistance <= radiusY) {

                if (xDistance < yDistance) {
                    vx *= -1;
                } else {
                    vy *= -1;
                }

                System.out.println("Hit");
            }
        }

        if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
            vx *= -1;
        }
        if (y + vy < 0
                || y + size.height + vy > getParent().getHeight()) {
            vy *= -1;
        }

        x += vx;
        y += vy;

        ball.setSpeed(new Point(vx, vy));
        ball.setLocation(new Point(x, y));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions