0

This is developed in JavaFX.

There are 2 balls - a dynamic ball and a stationary one. The first ball(dynamic) bounces off the walls and anything that comes in its way.

The second ball's purpose is to be an obstacle for the first ball. So, whenever the first ball touches the second ball, the first ball should instantly bounce away. Currently, the bouncing has very bad accuracy, and I don't know exactly how to fix it. Sometimes the first ball will bounce properly, but usually it will go INSIDE the second ball, get stuck in there for a moment, and then bounce away. The picture below goes over this exact issue. enter image description here

Here's my code for detecting collision and responding to it:

   //Pythagorean Theorem, to detect collision, by estimating the distance between the two circles
                double dx = circle.getLayoutX() - circle2.getLayoutX();
                double dy = circle.getLayoutY() - circle2.getLayoutY();
                double radii = circle.getRadius() + circle2.getRadius();

                double distance = (dx * dx) + (dy * dy);
                double minDistance = radii * radii;


// I believe something is missing in the lines below, which is causing the problem.

                if (distance < minDistance) { //If circle1(dynamic) collides with circle2(stationary)

                    c1SpeedX = c1SpeedX * -1; //Inverts direction.

                }

I spent hours on google, but I was not able to find an answer. I hope somebody can provide a solution and explain the issue. Thanks a lot in advance everybody!

Gregg1989
  • 675
  • 6
  • 14
  • 24
  • Logically wouldn't you want `distant <= minDistance`? I've not looked at all the details yet but this struck me as odd. Ironically a similar question has been asked: http://stackoverflow.com/questions/4613345/python-pygame-ball-collision-with-interior-of-circle?rq=1 – xQbert Nov 21 '13 at 18:08
  • I've tried both < and <= and it makes no difference at all. – Gregg1989 Nov 21 '13 at 18:11

2 Answers2

1

It's probably because the ball don't always have time to exit the bigger ball when its direction is reversed again and again.

If distance == minDistance:

Just do as you do now.

If distance < minDistance:

The ball is inside the larger one. Then it should already have bounced off and be a bit away. The ball should already have moved sqrt(distance-minDistance) away from the larger ball.

Viktor
  • 558
  • 5
  • 12
0

Mmm...

I think you have some formulas errors.

By example, distance and minDistance should be

double distance = Math.sqrt((dx * dx) + (dy * dy));
double minDistance = radii;
nashuald
  • 805
  • 3
  • 14
  • 31
  • 1
    The sqrt operation is more expensive than multiplying, that's why I have radii * radii, instead of the sqrt. – Gregg1989 Nov 21 '13 at 18:11
  • I think, you should ensure your code works ok first and after that, you could optimize code. – nashuald Nov 21 '13 at 18:13
  • could it be the `c1SpeedX` should be `c1SpeedX = c1SpeedX*-1` instead of `c1SpeedX = c1SpeedX-1`? because the comment say: 'Inverts direction'. – nashuald Nov 21 '13 at 18:33
  • Damn, that's exactly how I have it in my code, I copied an old version. I'll fix it in the main post. That's not the issue, sadly. – Gregg1989 Nov 21 '13 at 18:38
  • So bad. More info is needed to find the bug. – nashuald Nov 21 '13 at 18:39
  • The problem I think is missing information about calculating the angle and making the ball bounce away. I have no idea how to do that and all the guides that I've found haven't helped :\ – Gregg1989 Nov 21 '13 at 22:38