2D Rigid Body dynamics

This program draws balls bouncing around a scene by representing the balls as rigid circles and simulating their dynamics:

The program has the following properties:

  • Under 400 lines of code.
  • OpenGL rendering.
  • Real-time rigid body simulation.
  • Written entirely in OCaml.
  • Simple Euler time-integrator.
  • Recursive bisection to handle collisions accurately.

The OCaml programming language allows this task to be completed with the performance of C++ and the brevity of functional programming. Moreover, future languages like F# will allow such computations to be performed concurrently, using multiple cores or CPUs, to further improve both performance and GUI responsiveness.


The source code can be compiled with:

ocamlopt -I +lablGL lablgl.cmxa lablglut.cmxa unix.cmxa balls.ml -o balls

The program can be run by specifying the number of balls n:

./balls n