class Ball { float x, y; // position float trjX, trjY; // trajectoire float speed; // vitesse float x2, y2; // prochaine position int w = 4; // taille Ball(float x, float y) { this.x = x; this.y = y; this.trjX = 0; this.trjY = 0; } void move(Circle[] circle) { // calcul de la trajectoire trjY += GRAVITY; // calcul de la prochaine position x2 = x + trjX; y2 = y + trjY; // calcul des intersections boolean intersected = false; for (int i = 0; i < circle.length; i++) { Circle c = circle[i]; for (int j = 0; j < c.s.length; j++) { Segment s = c.s[j]; if (java.awt.geom.Line2D.linesIntersect((double)x, (double)y, (double)x2, (double)y2, (double)s.x1, (double)s.y1, (double)s.x2, (double)s.y2)) { // point d'intersection float uaN = (s.x2 - s.x1)*(y - s.y1) - (s.y2 - s.y1)*(x - s.x1); float ubN = (x2 - x)*(y - s.y1) - (y2 - y)*(x - s.x1); float uD = (s.y2 - s.y1)*(x2 - x) - (s.x2 - s.x1)*(y2 - y); if (uD == 0) { // break intersected = true; break; } float ua = uaN/uD; float ub = ubN/uD; float intX = x + ua*(x2 - x); float intY = y + ua*(y2 - y); // calcul de la nouvelle direction // V' = V - 2 * V . N * N float sc = scal(trjX, trjY, s.normX, s.normY); trjX -= 0.87*s.normX*2*sc; trjY -= 0.87*s.normY*2*sc; x2 = intX + trjX; // +trjX et +trjY n'est pas correct mais sinon ça bloque y2 = intY + trjY; // joue un son c.playSound(s); // ajoute une vaguelette ripples.add(new Ripple(intX, intY, 20, -1-(int)random(16777216), 0.5)); intersected = true; break; } } if (intersected) break; } // déplacement x = x2; y = y2; } float scal(float x1, float y1, float x2, float y2) { return (x1*x2 + y1*y2); } void display() { stroke(0); fill(250, 200, 100); ellipse(x, y, w, w); } }