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:
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()