Using python-ogre's OgreOde

rvweb

30-03-2007 08:34:29

Hi everybody,

I'm quite new to Ogre, and I'm using the Python Wrapper to create a small engine of my own. At the moment I am trying to set up a working program with physics. I am using the demo application that comes with the distribution to help me.
My program launches properly and I can see the object, but it's not moving and I don't understand why. It seems that the physics listeners are inactive.
Could anyone check what's wrong with this piece of code? I think the problem might not be that very hard to find for those with some experience, but it would help me a lot.

Basically, I have a main class that is for the program, and two classes that take care of the events (one for the pysics, the other for ogre).

Thanks a lot then! Here is a light version of my code:
import sys, time, random
from math import *
import Ogre as ogre
import OIS
import SampleFramework as sf
import OgreOde


class OgreOdeListener(OgreOde.CollisionListener, OgreOde.StepListener):
def __init__(self):
OgreOde.CollisionListener.__init__(self)
OgreOde.StepListener.__init__(self)
def collision(self, contact) :
g1 = contact.getFirstGeometry()
g2 = contact.getSecondGeometry()
if (g1 and g2):
b1 = g1.getBody()
b2 = g2.getBody()
if (b1 and b2 and OgreOde.Joint.areConnected(b1, b2)):
return False
contact.setCoulombFriction(9999999999)
contact.setBouncyness(0.1)
return True

class OgreListener(sf.FrameListener):
def __init__( self, program, renderWindow, camera ):
sf.FrameListener.__init__(self, renderWindow, camera)
self._program = program
def frameStarted(self, evt):
bOK = sf.FrameListener.frameStarted(self, evt)
self._program.frameStarted(evt, self.Keyboard, self.Mouse)
return bOK
def frameEnded(self, evt):
self._program.frameEnded(evt, self.Keyboard, self.Mouse)
return sf.FrameListener.frameEnded(self, evt)

class Program(sf.Application):
def __init__(self):
sf.Application.__init__(self)
def _createScene(self):
sceneManager = self.sceneManager
camera = self.camera
# Shadows
# Light
# Physics
self._world = OgreOde.World(sceneManager)
self._world.setGravity( (0,0,-9.80665) )
self._world.setCFM(1E-5)
self._world.setERP(0.8)
self._world.setAutoSleep(True)
self._world.setAutoSleepAverageSamplesCount(10)
self._world.setContactCorrectionVelocity(1.0)
#ogre.MovableObject.setDefaultQueryFlags(1<<0)
stepModeType = OgreOde.StepHandler.QuickStep
STEP_RATE = 0.01
frame_rate = 1.0 / 60.0
max_frame_time = 1.0 / 4.0
time_scale = 1.7
self._stepper = OgreOde.ForwardFixedInterpolatedStepHandler (self._world, stepModeType, STEP_RATE, frame_rate, max_frame_time, time_scale)
# Skybox
# Objects
self._plane = OgreOde.InfinitePlaneGeometry(ogre.Plane(ogre.Vector3(0,0,1),0),self._world, self._world.getDefaultSpace())
typeName = "ogrehead"
name = "ogrehead"
entity = sceneManager.createEntity(name, typeName + ".mesh")
node = sceneManager.getRootSceneNode().createChildSceneNode(name)
node.attachObject(entity)
entity.setNormaliseNormals(True)
entity.setCastShadows(True)
body = OgreOde.Body(self._world)
node.attachObject(body)
size = ogre.Vector3((OgreOde.Utility.randomReal() * 0.5 + 0.1) * 2.0,
(OgreOde.Utility.randomReal() * 0.5 + 0.1) * 2.0,
(OgreOde.Utility.randomReal() * 0.5 + 0.1) * 2.0)
mass= OgreOde.SphereMass(1.0,size.x)
mass.setDensity(5.0,size.x)
geom = OgreOde.SphereGeometry(size.x,self._world,self._world.getDefaultSpace())
node.setScale(size.x * 0.2,size.x * 0.2,size.x * 0.2)
body.setMass(mass)
geom.setBody(body)
body.setPosition(ogre.Vector3(0,0,10))
def _createFrameListener(self):
self.ogreListener = OgreListener(self, self.renderWindow, self.camera)
self.root.addFrameListener(self.ogreListener)
self.ogreodeListener = OgreOdeListener()
self._world.setCollisionListener(self.ogreodeListener)
def frameStarted(self, evt, Keyboard, Mouse):
self.sceneManager.setShadowFarDistance((abs(self.camera.getPosition().y) + 1.0) * 3.0)
time = evt.timeSinceLastFrame
if (self._stepper.step(time)):
self._world.synchronise()
def frameEnded(self, evt, Keyboard, Mouse):
pass

if __name__ == "__main__":
program = Program()
program.go()

rewb0rn

30-03-2007 11:20:16

i dont know python so i cant really help with that code, but you set gravity to -9.8, what is ok, but be aware that if you have large models you will hardly notice the gravitation, so try out higher values (I use something like -900 in my current application)

rvweb

30-03-2007 15:01:20

Well, I set the gravity to -900, but it didn't change anything.
I think my body is symply not taken into account by the physics engine. I may have forgotten something related to OgreOde, but I don't know what. I don't think my problem comes from Python.
Thanks for your help anyway!

brunomfs

24-09-2007 17:20:07

Hello everyone,
doing a search on the forums, i saw there are lots of people with this problem, even more if we are speaking of Python-Ogre users, including me.

Well, the reason i am bumping this thread is that i think i found a solution for the "correctly configured but static world problem": your application (in the above code an instance of the Program class) has to mantain as an attribute pointers to all the bodies that have to be updated in the simulation, so when the application is notified by the stepper, it can update the bodies positions/orientations. I am not 100% sure about this and also i am still doing tests so, PLEASE, correct me if am wrong! :)

Build our own OgreOde application based only on the demos that came with the source is really painful and demands a large amount of free time to mess around and test codes. So please, let's share knowledge and even better, make good tutorials 8)

rewb0rn

24-09-2007 22:05:35

word!! :D