Collision Detection?
in
Programming Questions
•
5 months ago
Hello!
I'm quite new to Processing and i'm just trying to get my head around a few things
Fairly simple question i'm hoping, this is some collision code made by Keith Peters and it's included in the examples under 'bouncing bubbles'. Can anybody simply explain to me in Layman's terms how the collision method works?
class Ball { // creates ball class
float x, y;
float diameter;
float vx = 0; // sets starting velocity of x position of moving ball
float vy = 0; // sets starting velocity of y position of moving ball
int id;
Ball[] others; // stores balls in an array
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2 (dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
Any help would be amazing thank you!
1