Sign in to follow this  
Followers 0
mklynge

Ball vs Ball Collision Problem

7 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Hi, thanks for your reply.

 

The new position is done for every object like this:

 

public void updatePhysics(long frameTime) {

 

oldvel = vel;

vel = vel.add(acc.mul(frameTime).add(friction));

oldpos = pos;

pos = pos.add(oldvel.add(vel).mul(0.5f * frameTime));

 

}

 

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0