planetary gravity demo

I played around with some planetary gravity. One of my game ideas I’ve had sitting around is a space based Galaxy that is online. I decided to play around and do some work with gravity. I have a small planet orbiting a sun. There are no textures, but  the orbit is nice. I start by initializing some different variables.

float r;
float dx;
float dy;
float sunx=0;
float sunz=0;
float F;
float M1;
float M2;
float ax;
float ay;
float G=-5;
GLfloat z=-500.0f;   // Depth Into The Screen
GLfloat xpos = -20.0f;
GLfloat zpos = -20.0f;
float vx=.35;
float vy=-.22;

Now I set my masses.

   M1=1; //planet
   M2=332946; //sun

Then I do some math calculations and draw.

dx = xpos-sunx; 
dy = zpos-sunz;
r = sqrt(dx*dx + dy*dy);
F = G*M1*M2/(r*r);
ax = G*M1*dx/(r*r*r);
ay = G*M1*dy/(r*r*r);
dx is the distance on the x axis, and dy is the distance on the y axis (2D). The other equations are kepler’s law. This is the law of planetary motion created by Johanas? Kepler in the 15 or 1600’s. Then, I move the planet by the velocity and draw. The files I’m linking too are the EXE, the devC++ project file, and the C++ source. This is made in opengl. Just extract these files with Winrar, Winzip, etc and either run the exe or open the project and look at the source and play with it. If you use it in a game, I won’t care, but I’d be honored for you to tell me my source was used.

NOTE: my source may not be very optimized, but it works. I can’t be held responsible for any problems it may cause. Don’t use it as a guide to learn a programming style! Mine might not be that great.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: