Andrew Buck and Justin Reynolds
CS 4610/7610 - Computer Graphics I
University of Missouri
Spring 2013
The gravity-sim application is written in C++ using OpenGL and FLTK. The source is available on GitHub here.
The simulation can handle many hundreds of objects.
Levels in which the actor must collect all objects can be created and played.
A force grid shows the gravitational potential caused by the objects.
This application provides a sandbox environment for experimenting with the effects of gravity. It offers the following features:
The objects in the simulation obey Newton's law of universal gravitation. The force between each pair of objects is computed as an inverse-square relation. The forces are aggregated and applied to each object to advance the simulation. The current implementation has O(n^2) complexity.
Each object in the simulation is defined as a Body class.
double Xpos, Ypos, Zpos; //Current translation
double Xrot, Yrot, Zrot; //Current rotation
double dXpos, dYpos, dZpos; //Change in translation
double dXrot, dYrot, dZrot; //Change in rotation
double mass, radius;
bool isOrigin, isStatic, isTarget, isActor, collidable;
std::vector<Point> trail; //History trail
computeForce(Body &b, double timestep); //Applies effect of other body
draw();
drawHistory();
A universe class is defined that manages the simulation.
std::list<Body> objList; //Contains all of the objects
addObject();
removeObject();
addTime(double timestep);
computeDistance(Body &a, Body &b);
computeForce(double x, double y, double z);
draw();
save();
load();
The GlWindow class is a child of the Fl_Gl_Window class and handles user input and drawing commands.
/* sensitivity variables */
/* key press variables */
/* mouse position variables */
/* other state variables */
Camera cam1, cam2;
Universe theUniverse;
displayMe(Camera); //Draws the scene with the given camera
handle(); //Handles events
animate(); //Called repeatedly with an interval timer to add time
clear();
showHelp();
Each keypress or mouse movement generates an event, and the appropriate action is taken depending on the state of the program. This allows additional functionality such as drawing the force grid, and adding static members, actors, and targets.