Best way to apply force towards a point.

Title says it - what’s the ‘best’ way to apply (global) force towards a point, as opposed to in some direction?
As in, planetary gravity.
I can think of a few ways, but they’re all pretty ‘hacky’.

from bge import logic
from mathutils import Vector


def main():
	scene = logic.getCurrentScene()
	cont = logic.getCurrentController()
	own = cont.owner
	for obj in scene.objects:
		if "gravity" in obj:
			# Get vector to planet center
			down_vec = Vector(obj.getVectTo(own)[1])
			# Apply force in that direction to simulate gravity
			down_vec.magnitude = GRAVITY
	player = logic.getCurrentScene().objects["Player"]
	up_vec = own.getVectTo(player)[1]
	player.alignAxisToVect(up_vec, 2, 1)

This example doesn’t work with static objects for some extremely odd reason , so , how do we fake gravity/forces/acceleration/collisions if we want to turn off the buggy physics engine ?


GravityVector 003.blend (98.7 KB)

What makes you think Bullet is buggy? I doubt that I could write a more accurate physics engine even ignoring collisions (just forces and stuff like that).
My advice: Use bullet

Also, that script needs to be run by every object you want to be attracted to the source. To make an object attract the ones running the script, give the attractor a property ‘gravity’

The reason your blend looks so weird is because you have it parented to something. There’s some funny stuff going on in there.

If you want a slightly more accurate simulation than CaptainAndrews, remember that F=MA, and gravity is an acceleration, so you’ll have to multiply the gravitational acceleration by the objects mass (obj.mass) before you can use applyForce. It also ignores gravitational falloff due to distance.
My own attempt at gravity was to do with establishing stable orbits, and was fairly sucessful. Have a look at the blend here:
gravity.blend (641 KB)
Bear in mind that this is also not perfectly physically accurate, as otherwise these orbits would take a couple of years. I wanted things to happen fast. If you want ‘realism’ set the Gravitation Constant in the script to 6.67 * 10 ** -11, and then remember that gravity is a very very weak force unless some of your objects are very very large (yes, in the blend gravity was multiplied several million times)

1 Like

Brilliant! Thanks, you just saved me a lot of coding to simulate gravity.

upbge now supports gravity as a vector per object

you can assign it 1 time if you want or update using near etc

gravity3d.blend (808.5 KB)

@stryker24 @sdfgeoff