# In BGE is there a way to apply gravity to the inside of a sphere

I'm building a game that has a level where a character walks on the inside of a sphere. Like a bubble, but all the objects in the sphere are pulled towards the outer edge. Gravity is strongest at the walls of the sphere, but are 0 at the center of the sphere. So the player will float as in space if they jumped too high.

So, the question is how can I set the gravity inside a sphere so that the maximum gravity is exerted on the outer wlls and null at it center?

To figure out a basic implementation of a "spherical gravity," let's look at how gravity works in normal, Newtonian physics. Here is the equation that you probably learned in high school physics:

$$F_g=\frac{G\cdot m_{\text{earth}}\cdot m_{\text{object}}}{r^2}$$

where $$F_g$$ is the force on the object due to gravity, $$G$$ is the gravitational constant, $$m_{\text{earth}}$$ is the mass of the Earth, $$m_{\text{object}}$$ is the mass of the object, and $$r$$ is the distance between the Earth and the object. Since $$G$$ and $$m_{\text{earth}}$$ are always the same on earth, we often simplify the equation as follows:

$$F_g=9.81\cdot\frac{m_{\text{object}}}{r^2}$$

The only issue with our formula is that we don't have any direction for our gravity. In normal physics we just assume that it is straight down (if we're getting technical, $$9.81$$ is a vector quantity that gives us our direction). We can't make this assumption inside of our sphere. To find the direction, we can multiply our gravity by the normalized location of our object (a vector with a length of $$1$$ pointing directly at our object):

$$F_g=9.81\cdot\frac{m_{\text{object}}}{r^2}\cdot\hat{\mathbf s}$$

where $$\hat{\mathbf s}$$ is the normalized location vector.

Now that we have a formula to calculate the gravity, we can write a Python script. This script must be run every logic tic on every object that will be affected by gravity, like this:

Script:

import bge

g = 9.81

obj = bge.logic.getCurrentController().owner
loc = obj.worldPosition
len = loc.length

try:
gravity = loc.normalized() * obj.mass * g / (radius - len) ** 2
except ZeroDivisionError:
gravity = [0,0,0]

obj.applyForce(gravity)


This will find how far the object is from the origin, then push it away from the origin, using a linear interpolation to vary the force from 0 at the center, and 9.8 at the edge of the sphere (defined as 20).

Here is an basic example .blend file. You can move the sphere with A, S, D, W, Q, and E. It will start motionless in the middle, then become pulled faster and faster as it gets closer to the enclosing sphere. Tapping Spacebar will make the sphere jump towards the origin.

• It looks like you are still using $mg/r^2$ which goes to infinity at the center. I think you want to just use $mgr/R$ (where $R$ is the radius of the sphere) which is linear in r and really does go to zero at the center of the sphere. – uhoh Feb 27 '18 at 20:17
• @uhoh I'm actually using $mg/(R-||r||)^2$, so gravity increases as the ball approaches the inside of the sphere. – Scott Milner Feb 27 '18 at 20:21
• Ah, indeed, and therefore becomes infinite at the surface of the sphere, and very larger even for finite sized objects which "bounce". A linear dependence on $r$ still might look more "natural" in an unnatural sort of way. – uhoh Feb 28 '18 at 5:58
• @uhoh Well, seeing as how normal gravity uses an inverse square falloff, I"m trying to replicate that. You're welcome to modify the code to fit your own purposes, of course. :-) – Scott Milner Feb 28 '18 at 14:37
• @uhoh and Scott: Thank you both for you expert help in this. I can't believe I didn't see that. I've been racking my brain and poof you two got these old cogs working again. A very hearty Thank you and Numerous KUDOS to you both. When my game comes out you two will be in the Special Thanks. Thanks So much. BTW The game's name is OOPS (abbreviated of course, but keep an eye out) it will be a full Blender game and free to the world. – Iam KraZasHell Apr 27 '18 at 0:39