A slightly different iteration of the animation with audio can be seen here.
The springing code snippet (simplified)
private function spring(ballA:Ball, ballB:Ball):void{ var dx:Number = ballB.x - ballA.x; var dy:Number = ballB.y - ballA.y; var dist:Number = Math.sqrt(dx * dx + dy * dy); var ax:Number = dx * springAmount; var ay:Number = dy * springAmount; ballA.vx += ax / ballA.mass; ballA.vy += ay / ballA.mass; ballB.vx -= ax / ballB.mass; ballB.vy -= ay / ballB.mass; } }Return to the experiments gallery