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.