class Ball { float x, y; float diameter; float vx = 0; float vy = 0; int id; Ball[] others; Ball(float xin, float yin, float din, int idin, Ball[] oin) { x = xin; y = yin; diameter = din; id = idin; if(id==0) diameter+=10; 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; } } } void move() { x += vx; y += vy; if (x + diameter/2 > width) { x = width - diameter/2; vx *= friction; } else if (x - diameter/2 < 0) { x = diameter/2; vx *= friction; } if (y + diameter/2 > height) { y = height - diameter/2; vy *= friction; } else if (y - diameter/2 < 0) { y = diameter/2; vy *= friction; } if(vx > 0) { vx -= 0.01; } if(vx < 0) { vx += 0.01; } if(vy > 0) { vy -= 0.01; } if(vy < 0) { vy += 0.01; } if(vx == 0) vy = 0; if(vy == 0) vx = 0; } void display() { if(id!=0) fill(120,34,119); else fill(255,34,34); ellipse(x, y, diameter, diameter); } }