• Create Account

# Ball vs Ball Collision Problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1mklynge  Members   -  Reputation: 112

Like
0Likes
Like

Posted 16 June 2013 - 05:47 AM

Hi all,

The last week I've been re-working the physics system in my little android (Java) ball game and I have bumped into a little problem. I am clearly not good enough to figure out the vector math behind it! I have searched around a bit, but haven't found any solutions to my problem.

The system for collision is pretty simple, I check all possible collisions once pr. ball set (so with 2 balls its 1 collision, 3 balls its 3 collisions etc) and call a method in my physics class (a physics class is linked to each Actor class - so each ball is represented by a PhysicsObject).

What I have now is when I detect a collision and call the method applyObject(PhysicsObject t) on one of the balls (only call it once pr. collision):

public void applyObject(PhysicsObject t) {

Vector3 n = new Vector3(this.pos.x - t.pos.x, this.pos.y - t.pos.y, 0);

double d = n.length();

n.x = n.x / d;

n.y = n.y / d;

double aci = vel.dot(n);

double bci = t.vel.dot(n);

double acf = bci;

double bcf = aci;

this.vel.x += (acf - aci) * n.x;

this.vel.y += (acf - aci) * n.y;

t.vel.x += (bcf - bci) * n.x;

t.vel.y += (bcf - bci) * n.y;

}

Here comes the tricky part, when two balls are colliding the x-velocity is calculated correctly (one ball from left to right, other from right to left with the same y position). But the y-velocity is acting all up and making the balls go way too fast (especially if they have the same x, but different y). Is there something obvious I am missing here? From what I can tell it might be something about missing a 2nd dimension check, but I simply can't find a solution for it.

Hopefully someone can help me out, thanks.

Edited by mklynge, 16 June 2013 - 05:49 AM.

### #2mawigator  Members   -  Reputation: 467

Like
0Likes
Like

Posted 16 June 2013 - 09:08 AM

Your code looks perfectly fine here. How do you calculate new positions?

The only change, I would calculate Vab=t.vel-this.vel (vector subtraction) and then Vab.dot(n).

It would be faster rather than calculating t.vel.dot(n) and this.vel.dot(n)

and then subtracting them (but, both solutions are equal).

### #3mklynge  Members   -  Reputation: 112

Like
0Likes
Like

Posted 16 June 2013 - 09:42 AM

The new position is done for every object like this:

public void updatePhysics(long frameTime) {

oldvel = vel;

oldpos = pos;

}

The acceleration and friction is both a vector with values: (0, 0).

### #4mklynge  Members   -  Reputation: 112

Like
0Likes
Like

Posted 16 June 2013 - 03:09 PM

To simplify the update method I rewrote it into:

public void updatePhysics(long frameTime) {

pos.x = pos.x + (vel.x * 0.5 * frameTime);

pos.y = pos.y + (vel.y * 0.5 * frameTime);

}

The problem is still there. Ball A and B going from left/right and right/left, meeting in the middle is perfectly bouncing off each other. Ball C and D going from top/bottom to bottom/top is actually not bouncing off each other but only adding speed to each other (made it so each ball only collide once to test the direction of all balls).

### #5mklynge  Members   -  Reputation: 112

Like
0Likes
Like

Posted 17 June 2013 - 10:09 AM

I found a error that made the oldpos of a object not work correctly, but still the problem is there. I have edited my code so when a collision appears the objects gets set back to their old position. Even with tons of balls on the screen they always stop (in the applyObject() I just set velocity to (0, 0) for both objects). But with the function named in my first post the y-factor seems screwed somehow. The balls still add speed instead of "swap" speed and direction.

Hope someone can help me here.

### #6mklynge  Members   -  Reputation: 112

Like
0Likes
Like

Posted 17 June 2013 - 12:33 PM

Ok, found something strange. Using this code:

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

The balls going up and down is now responding as expected (like the balls going left/right). But if a ball going down is colliding with a ball going left, all hell breaks lose. They add an insane amount of speed to each other for some reason.

I also simplifed the method that handles the collision:

public void Collision(PhysicsObject a, PhysicsObject b) {

Vector3 n = new Vector3(a.pos.x - b.pos.x, a.pos.y - b.pos.y, 0);

double d = n.length();

n.x = n.x / d;

n.y = n.y / d;

double aci = a.vel.dot(n);

double bci = b.vel.dot(n);

double acf = bci;

double bcf = aci;

a.vel.x += (acf - aci) * n.x;

a.vel.y += (acf - aci) * -n.y;

b.vel.x += (bcf - bci) * n.x;

b.vel.y += (bcf - bci) * -n.y;

}

Edit: And the update loop pr. PhysicsObject: (in case anyone was wondering if something is being added)

public void updatePhysics(long frameTime) {

oldpos.x = pos.x;

oldpos.y = pos.y;

pos.x = pos.x + (vel.x * 0.5 * frameTime);

pos.y = pos.y + (vel.y * 0.5 * frameTime);

}

Edited by mklynge, 17 June 2013 - 12:37 PM.

### #7h4tt3n  Members   -  Reputation: 1893

Like
0Likes
Like

Posted 19 June 2013 - 12:51 PM

I can't tell you where the error is, but I recommend you to go through all your vector math code. It seems as if somewhere there's an x where there should have been an y or the other way around.

### #8mklynge  Members   -  Reputation: 112

Like
0Likes
Like

Posted 03 July 2013 - 05:43 PM

Would it have anything to do with the way the coordination system is made? A normal coordinate system would have x positive to the right and y up positive. In Android (like XNA) its x positive to the right and y down for the positive values.

There is no x where it should be y and the other way around (double checked the Vector3 class I use/made). Only one method is working with these values (the update) except when something needs to get a new position (at collision or key event etc).

EDIT: Had to check the Vector3 class again and found the bug! Thanks for the help!

Edited by mklynge, 03 July 2013 - 05:48 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS