Hi,
I am at my wits end here. I was trying to follow the collision examples and info from the manual and I managed to set up my collisions where they detect each other…(i am pretty sure they do as I can see the collisions happening with .show(), and I can see the bounding sphere on the egg.
However I want a function to run when the collision happens ( I am using the egg N bowl example so when the egg hits the floor the function runs). Here is my code that adds the colliders:
def startScene(self):
if self.ode_SPACE.getNumGeoms()>1:
for o in self.simObjects:
o.destroy()
self.simObjects = []
for j in self.simJoints:
j = None
######### initialize Traverser
###set handlers
self.floor = loader.loadModelCopy("maze")
self.floor.reparentTo(render)
self.floor.setPos(0,0,0.57)
#self.floor.setScale(200,200,.5)
self.floorGround = self.floor.find("**/ground_collide")
self.floorGround.node().setIntoCollideMask(BitMask32.bit(0))
self.floorGround.show()
self.table=loader.loadModelCopy('misc/rgbCube')
self.table.reparentTo(render)
self.table.setScale(200,200,.5)
self.table.setZ(.5*self.table.getSz())
self.table.setColor(.2,.5,.7,1)
self.table.setLightOff()
self.simObjects.append( ODEbox( world=self.ode_WORLD, space=self.ode_SPACE,
realObj=self.table,
density=0, friction=0 ) )
###########################Load the peeps#################################################
#load peeps
numPeeps=20
for p in range(numPeeps):
peeps=loader.loadModelCopy("people" + str(randint(1,2)))
peeps.reparentTo(render)
peeps.setPos(random.gauss(0,40),random.gauss(0,40),.1)
self.SIM_peeps = ODEbox( world=self.ode_WORLD, space=self.ode_SPACE,
realObj=peeps,
density=0, friction=0 )
self.simObjects.append(self.SIM_peeps)
###################create the somJoints list here Once#############################
self.simJoints = []
levitation=1.4
self.hangingBody = ode.Body(self.ode_WORLD)
self.hangingBody.setPosition((0,0,levitation))
#Create a pedestal to hold the bowl up########################################
self.pedestal=loader.loadModelCopy('misc/rgbCube')
self.pedestal.reparentTo(render)
self.pedestal.setScale(1,1,0.8)
self.pedestal.setZ(.6)
self.pedestal.setColor(.3,.3,.3,0)
self.pedestal.setLightOff()
self.SIM_pedestal = ODEtrimesh( world=self.ode_WORLD, space=self.ode_SPACE,
realObj=self.pedestal, collObj=self.pedestal,
density=10, friction=2.5, )
self.simObjects.append(self.SIM_pedestal)
#attach the pedestal to a PlaneJoint for 2D movement#############################
eJ=ode.Plane2DJoint(self.ode_WORLD)
eJ.attach(self.SIM_pedestal.body, ode.environment)
# make sure to "save" the joint
self.simJoints.append(eJ)
###################then load and attach the bowl with a hingeJoint########################
# load the bowl
self.bowl=loader.loadModelCopy('spoon4')
self.bowl.reparentTo(render)
#This section deals with adding a specular highlight to the ball to make
#it look shiny
m = Material()
m.setSpecular(Vec4(1,1,1,1))
m.setShininess(96)
self.bowl.setMaterial(m, 1)
self.bowl.setScale(0.78,0.78,0.78)
self.bowl.setZ(.5)
self.SIM_bowl = ODEtrimesh( world=self.ode_WORLD, space=self.ode_SPACE,
realObj=self.bowl, collObj=self.bowl,
density=10, friction=1, )
self.simObjects.append(self.SIM_bowl)
self.SIM_bowl.body.setPosition(self.hangingBody.getPosition())
bJ=ode.HingeJoint(self.ode_WORLD)
bJ.attach(self.SIM_bowl.body, self.SIM_pedestal.body
)
bJ.setAnchor(self.SIM_bowl.body.getPosition())
#used to be 0.1
bJ.setAxis((0.01,0.5,0))
#bJ.setFixed()
# make sure to "save" the joint
self.simJoints.append(bJ)
# make sure to "save" the joint
# list to keep ODE joints (to connect the collision spheres of the egg),
# to keep their effect, or else Python considers the joints as garbage !!
#self.simJoints = []
# load some numEggs egg(s)
numEggs=1
for e in range(numEggs):
egg=loader.loadModelCopy('egg')
egg.reparentTo(render)
egg.setP(-90)
egg.setPos(0,0,1.65)
egg.setScale(4.26,4.26,4.26)
realObjectID=id(egg)
egg.setTag('egg','')
cNode = CollisionNode('egg')
cNode.addSolid(CollisionSphere(0,0,0,.25))
cNode.setFromCollideMask(BitMask32.bit(0))
cNode.setIntoCollideMask(BitMask32.allOff())
eggC = egg.attachNewNode(cNode)
eggC.show()
eggSkin=egg.find('**/eggSkin')
eggColl=egg.find('**/coll')
if self.eggCollVis:
eggSkin.hide()
else:
eggColl.hide()
# -------------------------
eggMainCollSphere=egg.find('**/mainSphere')
self.simObjects.append(
ODEsphere( world=self.ode_WORLD, space=self.ode_SPACE,
realObj=egg, collObj=eggMainCollSphere,
density=5, friction=2, noColl_ID=realObjectID ) )
# -------------------------
for s in egg.findAllMatches('**/sphere?').asList():
self.simObjects.append(
ODEsphere( world=self.ode_WORLD, space=self.ode_SPACE,
realObj=None, collObj=s,
density=5, friction=2, noColl_ID=realObjectID ) )
length=len(self.simObjects)
# create fixed joint to lock sphere's position relative to the main sphere,
# or simply relative to the last defined sphere will work just the same
fJ=ode.FixedJoint(self.ode_WORLD)
fJ.attach(self.simObjects[length-1].body, self.simObjects[length-2].body)
fJ.setFixed()
# make sure to "save" the joint
self.simJoints.append(fJ)
for o in self.simObjects:
if not self.trimeshVis and hasattr(o,'visualizer'):
o.visualizer.hide()
#########
#add collision node to the traverser and the pusher
self.cTrav = CollisionTraverser()
self.cHandler = CollisionHandlerQueue()
self.cTrav.addCollider(eggC, self.cHandler)
self.cTrav.showCollisions(render)
#########
base.cTrav = self.cTrav
for i in range(self.cHandler.getNumEntries()):
entry = self.cHandler.getEntry(i)
name = entry.getIntoNode().getName()
if name == "ground_collide": self.loseGame(entry)
World()
run()
Then my lose game function is:
def loseGame(self,entry):
entry.getIntoNodePath().getParent().self.startScene()
Any ideas? Thank you…