Gravity Physics Toy

by Paul Sullivan

Introduction

A physics simulation that accurately implements Newtonian gravity and applies planet textures to the objects.

Description

The most interesting part of this project was adding textures to my objects. After doing some unsuccessful experimenting in Blender, I found a sphere .obj mesh online that had proper texture mapping. The provided tinyobj code was able to successfully read this information from the file and I used it to map a variety of planet textures. The textures displayed on the planets depends on their mass: the smallest planets will have the moon's texture while the largest will have a neutron star texture. This was a good way to show of multiple textures and made more sense than randomly assigning textures.

Each planet leaves a trail of Phong shaded spheres. This is to make their paths easier to see. The trail is kept in a fixed-size array and old points are replaced with new ones as time goes on.

The camera is always looking at the average position of the planets. Since the planets are constantly moving, this is recalculated every frame. To rotate the planets I actually move the camera in a circle around them. The radius of this circle is determined by the W/S keys while the position along the circle is determined by an angle controlled by the A/D keys.

Other notes: The gravity constant used in this simulation is much higher than in real life. This is intentional to make the toy more interesting. Also, don't hold space down for too long. If a planet goes zooming off into the distance the average center point becomes the middle of nowhere because everything is outside render distance.

Controls

Pictures

A texture of the surface of Jupiter. This is correctly mapped to a sphere using the normal maps on the sphere .obj mesh.

A very busy simulation!

Smaller planets orbiting a neutron star (the light purple object).

Sources