Search code examples
java2dcollision-detection

2d balls not colliding properly


I'm just trying to code a nice looking physics game.

The ball collision looks nice but if the balls are colliding too slow, they "stick" in each other. I have no clue why they do.

Here's my collision function:

private void checkForCollision(ArrayList<Ball> balls) {
    for (int i = 0; i < balls.size(); i++) {
        Ball ball = balls.get(i);
        if (ball != this && ball.intersects(this)) {
            this.collide(ball, false);
        }
    }
}

public boolean intersects(Ball b) {
    double dx = Math.abs(b.posX - posX);
    double dy = Math.abs(b.posY - posY);
    double d = Math.sqrt(dx * dx + dy * dy);
    return d <= (radius + b.radius);
}

private void collide(Ball ball, boolean b) {
    double m1 = this.radius;
    double m2 = ball.radius;
    double v1 = this.motionX;
    double v2 = ball.motionX;

    double vx = (m1 - m2) * v1 / (m1 + m2) + 2 * m2 * v2 / (m1 + m2);

    v1 = this.motionY;
    v2 = ball.motionY;
    double vy = (m1 - m2) * v1 / (m1 + m2) + 2 * m2 * v2 / (m1 + m2);

    if (!b)
        ball.collide(this, true);
    System.out.println(vx + " " + vy);
    motionX = vx * BOUNCEOBJECT;
    motionY = vy * BOUNCEOBJECT;
}

But this is what happens when they collide with a low speed: enter image description here

So do you have an idea?

EDIT:

The update of Alnitak works very nice... but one problem is still there... if i add gravity like this:

public void physic() {
    motionY += GRAVITY;                  // <= this part (GRAVITY is set to 0.3D)
    checkForCollision(screen.balls);
    keyMove();
    bounceWalls();

    posX += motionX;
    posY += motionY;
}

They still move into each other. I think this is the wrong way to add gravity, or isn't it?

And I think I did something wrong with the collision formula, because they don't fall right:

!falling balls

and then they slowly sink into the ground.

EDIT: found an AMAZING tutorial: http://www.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html


Solution

  • (m1 - m2) * v1 / (m1 + m2) + 2 * m2 * v2 / (m1 + m2);
    

    This has an integer value 2. Please make it 2.0f or 2.0d then check it out. It must be the problem for small speeds. Becuse integer constant autocasts multiplied doubles.

    If this does not work, then Alnitak 's answer would be helpful.

    If you need real nice physics, you should use the force then convert it to velocity then convert it to displacement . Look at integrator techniques like Runge Kutta and Euler Integration

    Force-->acceleration-->velocity-->displacement
    

    if collision occurs, just update the force then the rest will be flowing.

    ----> http://codeflow.org/entries/2010/aug/28/integration-by-example-euler-vs-verlet-vs-runge-kutta/ <-----

    http://www.forums.evilmana.com/game-programming-theory/euler-vs-verlet-vs-rk4-physics/

    http://www.newagepublishers.com/samplechapter/001579.pdf

    http://cwx.prenhall.com/bookbind/pubbooks/walker2/

    Verlet integration is a point between Runge-Kutta-4 and Euler Integration preferably for molecular dynamics (a good example for bouncing balls if you ommit the electrical fields and bonds)