Springing Oddissey Gravitational attraction

This animation is caused by the gravitational attraction between two objects. The closer the objects are, the stronger their acceleration towards each other.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Dowload source code

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