Gravity Simulator [BGE]

Due to my general physics and astronomy interest, i have always wanted to make a gravity simulator somehow.
After a bit of searching on yt, i found a few projects that indeed tried to simulate gravitional force but none of them actually gives the ability for interactions on multiple planetary systems.

So i decided to do that using blender’s game engine.

At first i was planning to impose the gravity force on planetary system with 2 big mass objects, and then by the press of a button add smaller objects on a specified spawn location.

The point of that was how to implement every possible interaction that can occure between every single pair of objects on the scene. So in every frame, every frame calculates its total force caused by every other object on the scene. Of course the force calculated between each pair is the Newton’s gravity force: F=G \cdot \dfrac{M \cdot m}{r^2}

Here is a video presentation of the simulation. FPS keeps steady at 60 , for almost 20-30 objects spawned on the scene, I think that is pretty decent for bge and considering number of calculations being made per frame.

video coming soon…

After that i wanted to simulate a gravity field.


I figured out two ways to do implement that.

The first one was to define all the points of the grid and draw using bge.render.drawLine to connect them. The result was pretty good but all this procedure just kills performance. The maximum number of grid points comes to 28, if i should consider one body’s deformation and to 20 if i consider a second one. That’s pretty low so i needed to find another way of doing that.

Here is how it looks like:

And here comes the second way. Vertex Shader.  I had never been involved into shaders, did not know much about them, and to be honest i still have difficulties to get their IMMENSE importance on 3D Modeling and Gaming.
What a Vertex Shader does is to process the vertices and calculate their projected position on the screen. Well thats the default task 😛

So by altering a Vertex Shader’s code we can force our way into deforming the object whatever way we like.
This is the reason why a Vertex Shader seems the ideal choice for creating the needed deformation of the grid.

Another great thing about the VS is that it has the minimum impact you can possibly get on the framerate. This is because even if its not customly defined, the default shader will run either way. The only difference is that it just contains 5,6 lines of code, most of them scalar vector multiplications and vector addition or substraction, very simple stuff.

This is the demonstration of the VS for the gravitational grid. Two objects, almost infinite grid possibilities, still 60 fps 😀


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