getGravity()?

in blender version 2.49, I know how to SET the gravity for the game engine via GameLogic.setGravity(). However, I wish to access the current gravity data. I checked the documentation, specificly GameLogic but no find. I assume that the function merly sets the gravity for each individual dynamic object in the environment. Basically, has anyone been able to locate the gravitational data?

silly idea, but just have something drop x distance to and count the time it takes?

Interestingly, there isn’t a getGravity() function, even in the current 2.65. It really should be added in…

You can set it with logic.setGravity(), so unless you need to grab the world’s gravity (and unless it deviates from the usual 9.8), you can just set it and record what you set. Something like:



import GameLogic

GameLogic.grav = [0, 0, 20]

GameLogic.setGravity(GameLogic.grav)


I’m not familiar with 2.49’s API, but that should work.

What about releasing a “ball” and seeing how far it falls per frame for 3 frames, and get the acceleration?
invisible ghost ball?

With KX_Scene.gravity you can read and write the gravity. I found it by searching ‘gravity’ in the API of Blender 2.65.

from bge import logic
logic.getCurrentScene().gravity = [0.0, 0.0, -5.0]
gravity = logic.getCurrentScene().gravity
print(gravity)

That must have been exposed recently. Glad to see it there. I believe that’s fairly recent, though, so I don’t think 2.49 has such a property…?

So, I can turn scene gravity up down? what about individual objects?

I was thinking about a weapon that is a temp anti-gravity beam,

I figured out a way that already works with my current system. It can tell
wether gravity is either ON or OFF via the int variable that is assigned to
the object that houses my gravity manipulation function. But still, I don’t
get why it isnt accessible in 2.49. Like, it DOES exist, therefore it should
be accessible. But yea, the later versions of Blender are nicer. On another
note…im gona hate porting my game to the latest version.

It was reccently added, alongside the KX_GameObject.scene attribute. Thanks Moerdn!

It is not in 2.49 and will never be except you build your own custom version. I recognized this same years ago but never really needed.
What you can da ( similar to your above idea) store the values somewhere when you set gravity. The only gravity you cant get is the initial one. As described by previous posters it is possible to measure that.

And thanks to moerdn we have now access to group object. Thank you very much. This changes came in very silent.

hey guys, I’m a noob at scripting, so I’m sorry that this question is stupid :stuck_out_tongue:
I have an always and a keyboard sensors connected to a python controller, and the keyboard sensor is called “col”
here is the script for the python controller

from bge import logic

logic.grav = [0, 0, -9.8]
logic.setGravity(logic.grav)
cont = logic.getCurrentController()
own = cont.owner
col = own.sensors['col']
if col.positive:
    logic.grav = [-9.8, 0, 0]

whenever I hit the spacebar (the button assigned to the keyboard sensor) nothing happens :c
why is that?

OH! I also have the always sensor at true level triggering

the always sensor is the problem. either get rid of it, or include it in youre script. just define it, then add it to your if statement just like you did for “col”

I got rid of the always sensor but then the script didn’t work when I started the game so I included it in the script but it’s still not working :c

gravity.blend (668 KB)

This is the reason:

if col.positive and alw.positive:
    logic.grav = [-9.8, 0, 0]

You changed the value of a variable called ‘logic.grav’. If you want to set the gravity you do it like so:

logic.setGravity([-9.8, 0, 0])

Or like this if you are using the most recent version of Blender:

cont.owner.scene.gravity = [-9.8, 0, 0]

Also it’s advisable to use a module instead of a script. This will come in handy if you have a lot of code and it’s easier to handle to my opinion. Add an extension to the name of the script, for example: ‘Text.py’ and on the controller change Python Script Type to Module. Then write: ‘Text.myModule’, or in this case: ‘Text.setGravity’.

def setGravity(cont):
    scene = cont.owner.scene
    if cont.sensors[0].positive == True:
        scene.gravity = [9.8, 0, 0]
    else:
        scene.gravity = [0, 0, -9.8]

Also better to put the logic on the button itself. And don’t use pulse mode. You only want to set the gravity once, each time you are on the button, and each time you are off the button. And you don’t need an always sensor for this. The collision sensor already generates the pulses you need.