Major python script problems.

Alright guys, I’ve been trying to get an accurate gravity simulation to work using the Law of Universal Gravitation (if you’re not familiar or just google it ;)). I would very much appreciate some assistance from more experienced pythoners (erwin?).
Since the intention is to be able to have BGE to accurately represent particle attraction in 3D space the “world gravity” is turned to 0. I hope to one day have these particles varying in mass from specs of dust to planets etc; but for now I’m sticking with some cubes.
I’ve tried this in many forms, my two most recent can be downloaded here: (edit: fixed it, something with this forums link code is messed up and keeps trying to look under where my site is completly independant and I never put that anywhere in the url)(note: when in doubt copy and paste)
Once I’ve fixed any syntax errors etc. in each new incarnation all I get is a script that crashes blender completely.:eek: Which is as you can imagine is quite frustrating.
These versions are using setForce but I have in the past tried applyImpulse as well. The’re two in the zip file on where each running of the script has one aplication of force, the other has a seperate aplication of force for each object. I’ve also tried removing the camera and lamp to make sure it wasn’t trying to use them in the gravity calculations. I’m baffled.
I’d be happy if I could get something that wouldn’t crash blender and had a printable force/velocity vector.
At least blender crashes gracefully… :o
Thanks in advance!

  • J

The link to your zip file is not working.

Why does it keep changing my urls :confused:
Well it should work now.

Crashes on my PC as well.

I have no idea why though. Your script does not seem to have any syntax errors.

Try using a different loop structure (other than “for”), maybe a “while” loop?

There were times when one type of loop statement would crash my game, but another would work just fine.

Crashes here too.

Social, thanks for taking a look at it. Switching the loop types was a good idea but after switching to an incremental while statment it still crashed.

Anybody have any other ideas?

well, if it doesn’t bother you to put a property on each object that will be calculated you could just put everything in side the for statement inside a hasattr check
for example:

for i in range[0,obs,1]:
    if hasattr(oblist[i],'hasmass'):
        bla bla bla....

just have each a prop called ‘hasmass’ on each object you want to be part of the sim

hmmm… i took a closer look at your script and i noticed somthing. when you square your distances you make them positive and you need ‘negative’ distances for this to work(otherwise your objects will only move in the positive directions on any axis). my suggestion would be to have a check to se if ‘xdiff’ is negative, and then apply the force as negative once you calculate it

hehe, this is actually more complicated than it looks.

you’ll need to compleatly change the way you’re calculating the forces of gravity. right now your script is being told that there are three objects for each single object, one on each of the influenced object’s respective axies. that will not give you a correct sim. you need to have the force of gravity dispersed between the 3 axies, not have a seperate gravity force for each axis alone.

HA! I think I got it!
Thanks for your input Demobobin I wanted to impliment the object filtering but wasn’t sure how to do it.
I still have no idea why it crashed blender all those times…

Ok here is an example blend with my script in action (tested on 2.42 preview 42) it’s an early version but it works :
When the game is running check the console it should output the number of objects in the scene and then the force being applied to each cube.
I went ahead an anotated the code some, I’ll be making additions/revisions and I’d love to see if a script like this can be used to create 3d orbital simulations :smiley:

hello. i’m not trying to be cirtical or offensive so please don’t take it that way ;), but your script need to be modified to simulate gravity correctly.

because of the value you are using for the gravitational constant, the formula assumes that mass is in kg, distance is in meters and time is in seconds. there is nothing wrong with this except that if you want to see movement happen at a reasonable speed you’ll have to increase the objects masses greatly, or decrease the objects size and distance from one another. if you do this you’ll see that the objects are going off in very wierd directions, not at all the way the should. as i said before your script is assuming that there are three objects for every single influencing object. this pic shows what i mean.

you need the distance to be calculated by pathagorean’s formula, not by the manhattan method. then you simply distribute the force of gravity to each axis proportionaly.

also, just another little thing, you should exclude the object running the script from calculating the forces of gravity on itself. it just wastes computer power.

the hotlink won’t work, you’ll have to copy and paste it into the adress bar

i was bored so i changed it for you. :slight_smile:

it works for attraction, orbit calculation/simulation and anything else you can think of.

Notes: right now everything is made so that every onbect influences everyother object by the exact rules of gravity, it is exactly realistic. it is how ever pretty hard on the cpu this way. with just 10 objects you need to calculate and apply 90 forces. it wouls be easy to set a threshold so that only objects that make a big difference in another objects path of travel would be calculated, if you need to increase performance this is the best sacrifice to make.

Notes II:
----the units are as follows----

1 blender unit of length = 1 meter
1 blender unit of mass = 1,000,000 kilograms
1 second in blender = 1 second
1 blender unit of force = 1/4 Newton

Thanks for the time you’ve put in! I’m sorry to report several of your criticisms may be invalid.

First; my calculations being 3 “objects” for each object is incorrect. You must think in terms of 3d vector addition, if your not familiar with it I suggest you look into it. The resultant vectors magnitude is equivilent that of the force calculated with your meathod. The reason I used my method over the type you used was that it was a much more direct route given that the setForce requires the value to be given in components thus making it unesisary to even combine the vectors. Upon looking at it again I very well may find that some changes need to be made.

Second; you multiply the BGE mass by 1,000,000 once it is captured into python, but do not take that into acount when you export the force back to the BGE. If you had an object of BGE mass of 1kg then it is caluclated as 1,000,000kg and then exported with a large force to the BGE where it still has a mass of 1 kg. This essentialy multiplies that objects acceleration by 1,000,000 making your simulation seem to run WAY faster. You could argue it makes it funner to watch, but that was not the intention and nowhere was it noted the scale difference from reality. I’m planning to have a time scaling option and a more versitile meathod for handling a greater range of masses in future versions.

Third; I did take into acount not calculating for itself as yours did. Mine, however, also acomidated for objects that currently lie in the same coordinates, whereas yours would give you an error on your console.

That is far more than enough for now. If you still feel like arguing/discussing I suggest you either post your own topic somewhere (because I doubt this thread with generate any more attention), or find some physiscs/math profesor to converse with (they should know about vecotrs). I for one don’t have the time to invest in that right now. We’re all always learning. :cool:

I had forgotten the BGE force was 1/4 of a Newton for each unit, that’s important to catch and I will acomidate. I will extensivly review your file on my own and no doubt find ways to refine my simulation in the process; which is sometihng I would prefer to do on my own for now.
Thanks and good luck to you.

Please consider this topic closed because the major python issues have been solved.

i’m sorry if i offended you :o
while i still disagree, i really didn’t want to seem like i was pushing my ideas on you. there are just a few ways to go about this and i was trying to show you the way i would have done it.

once again, it was never my intention to make anyone angry


I understand, I over reacted. I truly do appreciate yours and everyones help. Demobobin; I PMed you. Oh and yours still works much better than mine :wink: