1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/*
* main.cpp
*
* Created on: Jul 28, 2012
* Author: michaela
* Fnet = (G * m1 * m2) / r^2
*
* Optional parameters ./gravity_simulator [--random] [num bodies]
*
* As of now, only the main body affects the other bodies, they do not affect each other
* Click on the main body and move the mouse to move it, click again to release
*/
#include "SDL_collide.h"
#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>
#include <list>
#include <math.h>
#include <iostream>
const int SCREEN_HEIGHT = 600;
const int SCREEN_WIDTH = 800;
const int SCREEN_BPP = 32;
const double G = 6.67e-1; //Scaled down by e-10
const double DELTA_TIME = 0.1; //Arbitrary
const double RADIUS = 3.0;
//Hardcoded number of bodies, used if no argument is specified
int bodies = 100;
struct celestial_body
{
int count;
double mass;
double radius;
double x, y;
double vX, vY;
double aX, aY;
SDL_Color color;
celestial_body(int newCount = 0, double newRadius = 0.5, double newMass = 0, int newX = 0, int newY = 0)
{
count = newCount;
if(newMass == 0)
mass = (rand() % 1000) + 1;
else
mass = newMass;
radius = newRadius;
if(newX == 0)
x = fmod((double)rand(), (double)SCREEN_WIDTH);
else
x = newX;
if(newY == 0)
y = fmod((double)rand(), (double)SCREEN_HEIGHT);
else
y = newY;
vX = vY = 0;
aX = aY = 0;
//Random color and avoid very dark colors
color.r = (int)mass % (rand() % 10000) % 255;
color.g = (int)mass % (rand() % 10000) % 255;
color.b = (int)mass % (rand() % 10000) % 255;
if(color.r <= 20 && color.g <= 20 && color.b <= 20)
{
color.r += 25;
color.g += 25;
color.b += 25;
}
}
void draw(SDL_Surface* screen)
{
filledCircleRGBA(screen, x, y, radius, color.r, color.g, color.b, 255);
}
void update()
{
vX += aX * DELTA_TIME;
vY += aY * DELTA_TIME;
x += vX * DELTA_TIME;
y += vY * DELTA_TIME;
}
void print()
{
std::cout << "Count: " << count << "\nMass: " << mass << "\nX: " << x << "\nY: " << y <<
"\nColor: " << (int)color.r << " " << (int)color.g << " " << (int)color.b << "\n\n";
}
};
void spawn_body(std::list<celestial_body>& cb, int newX = 0, int newY = 0)
{
static int count = 1;
celestial_body new_body(count++, RADIUS, 0, newX, newY);
cb.push_back(new_body);
new_body.print();
}
void cleanup(std::list<celestial_body>& cb)
{
cb.clear();
SDL_Quit();
}
int main(int argc, char** argv)
{
SDL_Surface* screen;
SDL_Event event;
std::list<celestial_body> cb;
std::list<celestial_body>::iterator it;
std::list<celestial_body>::iterator ij;
double distance, diffX, diffY;
double Fnet; //Net Force on body
double theta; //Angle between two points in 2-D space
double accel; //Net acceleration of body
bool quit = false;
bool random = false;
bool selected = false;
//Process arguments
if(argc == 3)
{
if(strcmp(argv[1], "--random") == 0)
random = true;
int tempBodies = atoi(argv[2]);
if(tempBodies > 0)
bodies = tempBodies;
}
if(SDL_Init(SDL_INIT_VIDEO) == -1)
quit = true;
SDL_WM_SetCaption("Gravity Simulator", NULL);
SDL_ShowCursor(1);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF);
if(screen == NULL)
quit = true;
//Initialize main static body
celestial_body main(0, 8.0, 1000, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
main.print();
if(random)
for(int i = 1; i <= bodies; ++i)
spawn_body(cb);
while(!quit)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
quit = true;
if(event.type == SDL_KEYDOWN)
if(event.key.keysym.sym == SDLK_ESCAPE)
quit = true;
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(SDL_CollideBoundingCircle(main.x, main.y, main.radius, event.button.x, event.button.y, 1, 0))
selected = !selected;
else
spawn_body(cb, event.button.x, event.button.y);
}
}
if(selected)
{
main.x = event.motion.x;
main.y = event.motion.y;
}
SDL_FillRect(screen, &screen->clip_rect, 0x000000);
main.draw(screen);
for(it = cb.begin(); it != cb.end(); ++it)
(*it).draw(screen);
SDL_Flip(screen);
for(it = cb.begin(); it != cb.end(); ++it)
{
diffX = main.x - (*it).x;
diffY = main.y - (*it).y;
//Determine the distance between two bodies
distance = sqrt((diffX * diffX) + (diffY * diffY));
//Determine the net gravitational force between the two
Fnet = ((G * (*it).mass * main.mass)) / (distance * distance);
//Determine the angle from a to b in 2-Dimensional space
theta = atan2(diffY, diffX);
//Determine the total acceleration
accel = Fnet / (*it).mass;
//Find acceleration from vector components
(*it).aX = accel * cos(theta);
(*it).aY = accel * sin(theta);
//Check for collision between new_body and main
//main gains the mass of colliding body
if(SDL_CollideBoundingCircle(main.x, main.y, main.radius, (*it).x, (*it).y, (*it).radius, 1))
{
main.mass += (*it).mass;
it = cb.erase(it);
}
}
for(it = cb.begin(); it != cb.end(); ++it)
(*it).update();
SDL_Delay(10);
}
cleanup(cb);
return 0;
}
|