Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 642
TeNNoX Avatar asked Aug 29 '12 18:08

TeNNoX


2 Answers

This is a common problem that happens because sometimes the delta-v of the bouncing ball is insufficient to take it back out of the collision zone.

So the collision routine reverses the direction again, taking it back inside the other ball, ad-infinitum.

You should add a sufficient offset (in the direction of the collision force) to the position of the ball to ensure that the newly calculated positions are no longer colliding.

Alternatively, check whether the balls would collide once you add the new motion values:

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

but you should also change ball.intersects(this) to intersects(ball).

They may appear to collide slightly too early, but on a fast moving ball it probably won't be visible.

like image 154
Alnitak Avatar answered Oct 13 '22 11:10

Alnitak


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

like image 22
huseyin tugrul buyukisik Avatar answered Oct 13 '22 12:10

huseyin tugrul buyukisik