I am trying to create something similar to this effect (only example of a game that implements this I could think of): http://youtu.be/sqxv1pWrgSs?t=13m39s
It’s basically just planetary gravity, however there are multiple gravitational targets, therefore it orbits between them.
This is what I have managed to do so far:
from bge import logic
scene = logic.getCurrentScene()
cont = logic.getCurrentController()
own = cont.owner
star = scene.objects["Sun"]
r = own.position - star.position
G = 6.67 #if this is set to the actual gravitational constant, it will literally take years to orbit.
#Planetary Gravitation equation
F = G*star.mass*own.mass/r.magnitude**2*(r/r.magnitude)
#apply Newton's law:
own.applyForce(-F, False)
star.applyForce(F, False)
if not "init" in own:
v = (G*star.mass/r.magnitude)**(1/2)
own.setLinearVelocity([v, 0, 0], False)
star.setLinearVelocity([-v*own.mass/star.mass, 0, 0], False)
own["init"] = True
Now, the question is, how to go about applying this to every object in the scene… Simply setting every object to attract to just one central object is the simple way, but that isn’t realistic since all objects attract with equal force, though it effects the one with lower mass more, what is the best way of accomplishing this?
To be honest I have no idea what is going on there :P… Not English + Blender 2.49 + Numpy == A confused MrPutuLips
I managed this:
from bge import logic
scene = logic.getCurrentScene()
cont = logic.getCurrentController()
own = cont.owner
masses = [] #create a list of the "pairs" of objects in a list
for m2 in range(len(scene.objects)-1):
masses.append(m2) #create a list of the scene.objects - 1 (m2)
for m1 in masses: #apply to each object in the list
masses.remove(m1) #remove so it will not stack
for m2 in masses: #for each pair of masses
r = (scene.objects[m1].position-scene.objects[m2].position) #distance between the two objects
F = -r/r.magnitude**3*scene.objects[m1].mass*scene.objects[m2].mass #Planetary (Radial) Gravtitation. You can **3 since it ** by r a 2nd time
scene.objects[m1].applyForce(F, False) #apply the calculated Force
masses.insert(m1, m1) #re-insert the value in the same list location
Problem with this is that objects added don’t apply to the equation (because it’s scene.objects - 1), so another object has to be added for the previous one to be affected. Also, it uses hella lot of logic calculation (basically calculating gravity between every object^2)…
i made an example for you,it uses force and collision sensor.
[ATTACH]290881[/ATTACH]
[/quote]
That works like a beast. Guess I was trying to make something far too complex for something so simple.
Anyway I changed it a bit to be more “scientifically correct” (though it obviously isn’t). I’ll upload the .blend when my internet stops being dial-up
Thanks
EDIT: .blend attached
Changes I made:
Inherits mass from parent of gravitational field (i.e the planet/asteroid’s mass).
If there is no parent, it uses a property “mass” to determine the strength of the gravitation
r exists (the distance between the centre of the gravitation and the objects determines how much gravity effects it)
Newton’s law is applied to both objects (equal & opposite force).
Only problem still: Objects don’t effect each other in the gravitation (which they should). I’ll see if I can get something to work.
Might need some tweaking, but for now I’m going to mark it solved. Thanks for the help.