I'm building a simple Android Game. And I'm stuck on detecting the direction of a collision of two balls. I have a moving ball A, and a fixed ball B. Ball A is much smaller than B.
I do not care about the mass of two balls. After the collision of two balls, the ball B is disappear, and the ball A changes the direction!
I want something like this..
Here is my current code:
private void collision(Ball a, Ball b){
if(b.isVisible){
double d = Math.sqrt((a.cx - b.cx)*(a.cx - b.cx) + (a.cy - b.cy)*(a.cy - b.cy));
if(d <= a.radius + b.radius) {
b.isVisible = false;
if(a.dx * b.dx < 0 && a.dy * b.dy < 0){
a.dx = - a.dx;
a.dy = - a.dy;
b.dx = - b.dx;
b.dy = - b.dy;
} else if(a.dx * b.dx < 0){
a.dx =- b.dx;
a.dx = - b.dx;
} else if(a.dy * b.dy < 0){
a.dy = - a.dy;
b.dy = - b.dy;
} else{
a.dx = - a.dx;
a.dy = - a.dy;
b.dx = - b.dx;
b.dy = - b.dy;
}
}
}
}
But it just reverses the direction of the ball B
So, I'm finding the better solution. Anyone help? Thanks a lot!