256

2D Gravity Toy

I wrote an interactive gravity simulator in C using SDL2.

Here's a screenshot:

There are 4 tools that can be selected using the tool-bar at the top left. From top to bottom, they are:
Pan: Click and drag to move the view pane around.
Push/Pull: Hold the LMB down to pull objects towards the cursor; can be toggled to push them instead, and stength is adjustable.
Add: Add new objects. Hold LMB and drag to set their initial velocity. Mass is adjustable, and you can also give objects negative mass; such objects are attracted to others as normal, but others are repelled from them.
Explode: Destroy objects, fragmenting them into smaller pieces that fly outwards. Number of fragments is adjustable, and setting it to 0 allows you to simply delete objects.

The mousewheel can be used to zoom in and out.

I don't currently provide any executable files, so you'll have to compile it yourself. The only requirement is SDL2. Here's the download for the source code.

One oddity I've noticed is that the orbits in my programme have hugely processing apsides. The old Java applet (now reimplemented in JavaScript) I was loosely inspired by has the same quirk. Does anyone know why this is? If so, please email me. I'm pretty sure I've implemented gravity correctly, with attraction determined by the inverse square of distance. I'd expect the orbits to stay the same shape but they don't. It doesn't seem to be related to the framerate/speed of simulation.

About | Contact Me | Index
Page last modified on 2026-02-23.