I'm trying to set up a scene using a script that places a bunch of cubes in random locations throughout the screen and sets their physics type to rigid body. Ideally, I'd like to have one cube be an agent whose actions can be controlled via a different script. How do I set objects' physics type en masse using python in the game engine? I've tried

ActiveObj.game.physics_type = 'RIGID_BODY'
ActiveObj.game.use_collision_bounds = 1

as well as

for obj in bpy.data.objects:

but, obviously, neither works in the game engine. How do I get around this?

  • $\begingroup$ As I was told, bpy won't work when you run the game as standalone so don't use it unless you are about to take it out in the end.That was just a notice. $\endgroup$ – Lev Jul 17 '15 at 21:51
  • $\begingroup$ So how can I add objects via script in the game engine and set their physics type? Every method I've found uses bpy :/ $\endgroup$ – Archana Ram Jul 17 '15 at 21:59

It's easy to turn a rigid body static and back again. Start with a rigid body, and then you can suspend/restore the dynamics:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

A slightly more hacky way to turn a rigid body static is to set it's mass to infinite (ie no force will have an effect).

import bge

cont = bge.logic.getCurrentController()
own = cont.owner
own.mass = float('inf')

To make it rigid body again, set it's mass back to a proper number

To make an object dynamic is hard as BGE doesn't have access to the inertia tensor/radius of gyration. Even setting the angular velocity cap to 0.0 doesn't work. I guess you'd have to, every frame, set it's angular velocity to zero and restore it's orientation.

(untested code):

import bge

own = bge.logic.getCurrentController().owner
if 'ori' not in own:
    own['ori'] = own.worldOrientation.copy()
own.worldOrientation = own['ori']
own.worldAngularVelocity = [0.0, 0.0, 0.0]

Create a cube in a different layer and tick rigid in his physics property.

Then go in your active layer and place an empty where you want your cubes to spawn.

Add an "Edit Object" actuator to the empty.

Choose when to activate it and set the Cube in the different layer as the object to be added.

Then the newly added object will have rigid physics.

  • $\begingroup$ Any way to do that without the gui? The project I'm working on relies on me being able to not use the gui whenever possible $\endgroup$ – Archana Ram Jul 17 '15 at 21:58
  • $\begingroup$ I don't think so, but I haven't tried this to know. I just searched the API and did not find a command that would change rigid to static and so on. You can decide when to add the objects via python but you must have the actuator connected. Wait to read some other solutions too. I could have missed something. $\endgroup$ – Lev Jul 17 '15 at 22:15
  • $\begingroup$ It's possible, though I don't recommend it this way... Spawn a headless blender, create the new object, save to a temp file, libload. Horrible, I know. $\endgroup$ – pqftgs Aug 17 '15 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.