How to make spherical / planetary gravity in the BGE

In this video, I go over how to create gravity such as that on planets - gravity that does not pull straight down but rather to a point.

1 Like

Would this spherical/planetary with dynamic terrain loading?It would nice if we could make a endless runner videogame with this type of gravity.

It probably would work

It would be kind of complex because the earth goes around the sun.

Very cool man, good to see you posted this!

It would be kind of complex because the earth goes around the sun.

and the Moon
– the 3 body problem

Maybe he will make a tutorial about it.But maybe not.It might be too hard.

Can someone get this logic bricks version of spherical gravity (sort of) to work. i can do it with python but am trying to show my students how to do something cool with just the basics. - Thanks, Ian.

full-spherical-gravity-lbricks.blend (547.7 KB)

I believe that in this case, Logic Bricks are really not adapted for this.

Just a touch of Python:

# filename:

# > 6.674×10−11 m3⋅kg−1⋅s−2
G = 6.674e-11

def computeGravity(distance, massA, massB, factor=1):
    `factor` allows you to downscale everything as needed, the BGE doesn't handle cosmic scales...
    return G * massA * massB / (factor * distance) ** 2

def gravity(controller):
    owner = controller.owner
    scene = owner.scene
    # the planet object must be named `planet`
    # the planet object must have a gameproperty named `mass`
    planet = scene.objects['planet']
    planet_mass = float(planet['mass'])

    distance, ownerToPlanet, _ = owner.getVectTo(planet) # vector
    force = computeGravity(distance, owner.mass, planet_mass, 1e-1)
    owner.applyForce(force * ownerToPlanet.normalized())
    owner.alignAxisToVect(-ownerToPlanet, 2, 1)

This should be minimal enough, you can play with the values just like I did, funny to be either on Jupiter or on the Moon :wink:

full-spherical-gravity-python.blend (527.7 KB)

(I don’t like the noodle soup logic bricks end up creating)

Thanks for going to that trouble for me. It may be beyond the students (and me) to understand without a line by line explanation. But the fact that it works so well will definitely give them a thrill and I can offer extra marks to anyone who can explain it.


Just read the code, there is very few lines, and everything is rather verbose.

The script just fetches the planet object, gets a gameproperty from it (to get its mass), and processes the gravity force following Newton’s laws (I mean, I just googled it very quick):’s_law_of_universal_gravitation

The only thing that requires a bit of explanation would be these two lines:

owner.applyForce(force * ownerToPlanet.normalized())
owner.alignAxisToVect(-ownerToPlanet, 2, 1)

The first line just applies the force as a vector following the ownerToPlanet unit vector (force is a scalar, but we need to use a vector, so I use ownerToPlanet.normalized() so that I have a direction).

The second line aligns the object to always have its bottom pointing towards the center of the planet (just like a canadian and an australian both stand on their feet, one is not on its head and not the other). The parameters of the function might look obscure, but you can look in the Python Bible for the BGE, here is a link:

Do not think that code is hard because it is code !

I started writing my firsts scripts for the BGE when I was 13 at school too, it was shitty at first, but I only wish I had access to the scripts I am now writing, especially when they are as verbose as this :slight_smile:

Python is rather friendly if you write it in a friendly way, so don’t be scared and just try to read line by line, word by word, and try to understand the general idea.

edit: By the way, how would you mark my work ? :stuck_out_tongue:

10 out of 10!
We should be able to create some images (i.e. arrows to describe the forces and vectors).
Well done. The 13 year old you would be very proud the man.


1 Like

Dude, I’m gonna try this in my 2D game. :slight_smile: I’m planning on a level in space falling back down to earth and it would be so cool to include mini planets on the way down! :slight_smile:

go for it. Take a look at my Infinity Life

Are there any changes I should make to the code for 2D? I tried the code from this video and my character (Which only moves on local x and z axis) was rotating weird and ended up floating behind my planet out of view from the camera.
P.S. Your game looks incredible!

you’ll need to adjust the axises in the code. I think the code has a line to alignAxisToVect() an you might need to change the parameter for the axis on that otherwise i think you should be good

Oh, turns out the code was fine. I had the origin of my planet off center. This will be so cool!