def createBallGeom(self, modelNode, ballBody, space): ballGeom = OdeSphereGeom(space, 1) ballGeom.setCollideBits(BitMask32(0x2)) ballGeom.setCategoryBits(BitMask32(0x1)) ballGeom.setBody(ballBody) space.setSurfaceType(ballGeom, SurfaceType.BALL) return ballGeom
class Sphere(GameObject): def __init__(self, world, parent, color, pos, dir, radius, density, posParent=None): GameObject.__init__(self, world) self.node = parent.attachNewNode("") if posParent == None: self.node.setPos(*pos) else: self.node.setPos(posParent, *pos) self.node.setColor(*color) self.node.setScale(radius) self.node.lookAt(self.node, *dir) self.parent = parent self.color = color self.scale = radius self.model = loader.loadModel("models/smiley.egg") self.model.reparentTo(self.node) self.mass = OdeMass() self.mass.setSphere(density, radius) self.body = OdeBody(world.world) self.body.setMass(self.mass) self.body.setPosition(self.node.getPos()) self.body.setQuaternion(self.node.getQuat()) self.body.setData(self) self.geom = OdeSphereGeom(world.space, radius) self.geom.setBody(self.body) world.space.setSurfaceType(self.geom, world.surfaces["sphere"]) def onCollision(self, otherBody, entry): if otherBody.isEmpty(): # Collision on a wall geom = entry.getContactGeom(0) Ripple(self.parent, self.color, geom.getPos(), geom.getNormal() * -1, self.scale * 2.5)
def createTire(self, tireIndex): if tireIndex < 0 or tireIndex >= len(self.tireMasks): self.notify.error('invalid tireIndex %s' % tireIndex) self.notify.debug('create tireindex %s' % tireIndex) zOffset = 0 body = OdeBody(self.world) mass = OdeMass() mass.setSphere(self.tireDensity, IceGameGlobals.TireRadius) body.setMass(mass) body.setPosition(IceGameGlobals.StartingPositions[tireIndex][0], IceGameGlobals.StartingPositions[tireIndex][1], IceGameGlobals.StartingPositions[tireIndex][2]) body.setAutoDisableDefaults() geom = OdeSphereGeom(self.space, IceGameGlobals.TireRadius) self.space.setSurfaceType(geom, self.tireSurfaceType) self.space.setCollideId(geom, self.tireCollideIds[tireIndex]) self.massList.append(mass) self.geomList.append(geom) geom.setCollideBits(self.allTiresMask | self.wallMask | self.floorMask | self.obstacleMask) geom.setCategoryBits(self.tireMasks[tireIndex]) geom.setBody(body) if self.notify.getDebug(): self.notify.debug('tire geom id') geom.write() self.notify.debug(' -') if self.canRender: testTire = render.attachNewNode('tire holder %d' % tireIndex) smileyModel = NodePath() if not smileyModel.isEmpty(): smileyModel.setScale(IceGameGlobals.TireRadius) smileyModel.reparentTo(testTire) smileyModel.setAlphaScale(0.5) smileyModel.setTransparency(1) testTire.setPos(IceGameGlobals.StartingPositions[tireIndex]) tireModel = loader.loadModel( 'phase_4/models/minigames/ice_game_tire') tireHeight = 1 tireModel.setZ(-(IceGameGlobals.TireRadius) + 0.01) tireModel.reparentTo(testTire) self.odePandaRelationList.append((testTire, body)) else: testTire = None self.bodyList.append((None, body)) return (testTire, body, geom)
def createTire(self, tireIndex): if tireIndex < 0 or tireIndex >= len(self.tireMasks): self.notify.error("invalid tireIndex %s" % tireIndex) self.notify.debug("create tireindex %s" % tireIndex) zOffset = 0 body = OdeBody(self.world) mass = OdeMass() mass.setSphere(self.tireDensity, IceGameGlobals.TireRadius) body.setMass(mass) body.setPosition( IceGameGlobals.StartingPositions[tireIndex][0], IceGameGlobals.StartingPositions[tireIndex][1], IceGameGlobals.StartingPositions[tireIndex][2], ) body.setAutoDisableDefaults() geom = OdeSphereGeom(self.space, IceGameGlobals.TireRadius) self.space.setSurfaceType(geom, self.tireSurfaceType) self.space.setCollideId(geom, self.tireCollideIds[tireIndex]) self.massList.append(mass) self.geomList.append(geom) geom.setCollideBits(self.allTiresMask | self.wallMask | self.floorMask | self.obstacleMask) geom.setCategoryBits(self.tireMasks[tireIndex]) geom.setBody(body) if self.notify.getDebug(): self.notify.debug("tire geom id") geom.write() self.notify.debug(" -") if self.canRender: testTire = render.attachNewNode("tire holder %d" % tireIndex) smileyModel = NodePath() if not smileyModel.isEmpty(): smileyModel.setScale(IceGameGlobals.TireRadius) smileyModel.reparentTo(testTire) smileyModel.setAlphaScale(0.5) smileyModel.setTransparency(1) testTire.setPos(IceGameGlobals.StartingPositions[tireIndex]) tireModel = loader.loadModel("phase_4/models/minigames/ice_game_tire") tireHeight = 1 tireModel.setZ(-IceGameGlobals.TireRadius + 0.01) tireModel.reparentTo(testTire) self.odePandaRelationList.append((testTire, body)) else: testTire = None self.bodyList.append((None, body)) return (testTire, body, geom)
class Sphere(GameObject): def __init__(self, world, parent, color, pos, dir, radius, density, posParent=None): GameObject.__init__(self, world) self.node = parent.attachNewNode("") if posParent == None: self.node.setPos(*pos) else: self.node.setPos(posParent, *pos) self.node.setColor(*color) self.node.setScale(radius) self.node.lookAt(self.node, *dir) self.parent = parent self.color = color self.scale = radius self.model = loader.loadModel("models/smiley.egg") self.model.reparentTo(self.node) self.mass = OdeMass() self.mass.setSphere(density, radius) self.body = OdeBody(world.world) self.body.setMass(self.mass) self.body.setPosition(self.node.getPos()) self.body.setQuaternion(self.node.getQuat()) self.body.setData(self) self.geom = OdeSphereGeom(world.space, radius) self.geom.setBody(self.body) world.space.setSurfaceType(self.geom, world.surfaces["sphere"]) def onCollision(self, otherBody, entry): if otherBody.isEmpty(): # Collision on a wall geom = entry.getContactGeom(0) Ripple(self.parent, self.color, geom.getPos(), geom.getNormal() * -1, self.scale * 2.5)
def initPlanet(self, parent, tex, pos, radius, mass, lvel, avel): planet = loader.loadModel(self.main.modeld + "planet") planet.reparentTo(parent) planet_tex = loader.loadTexture(tex) planet.setTexture(planet_tex) planet.setPos(pos) planet.setScale(radius) body = OdeBody(self.main.physicsWorld) M = OdeMass() M.setSphereTotal(mass, radius) body.setMass(M) body.setPosition(planet.getPos(parent)) body.setQuaternion(planet.getQuat(parent)) body.setLinearVel(lvel) body.setAngularVel(avel) geom = OdeSphereGeom(self.main.space, radius) geom.setBody(body) self.main.getPlist().append(planet) self.main.getBlist().append(body) self.main.getGlist().append(geom)
class Pallo(Collectible): def __init__(self, game, color): self.game = game self.visualNode = NodePath('Visual node') self.visualNode.reparentTo(render) model = loader.loadModel('testipalikka.egg') model.reparentTo(self.visualNode) plight = PointLight('plight') plight.setPoint( Point3(0.6, 0, 5) ) plight.setColor( color ) plight.setAttenuation( Vec3(0.5, 0.01, 0.01) ) plightNodePath = model.attachNewNode(plight) model.setLight(plightNodePath) self.body = OdeBody(game.physicsWorld) self.mass = OdeMass() self.mass.setBox(10,1,1,1) self.body.setMass(self.mass) self.body.setGravityMode(False) #self.juttu = OdeUtil() self.collGeom = OdeSphereGeom( self.game.physicsSpace, 2) self.collGeom.setBody(self.body) self.collGeom.setCategoryBits( BitMask32(0xffffffff) ) self.collGeom.setCollideBits( BitMask32(0xffffffff) ) def PowerUpEffect(self, ship): ship.mass.add(self.mass) ship.addPower(-20) print ship.SHIP_TYPE + " lost 20 power!!"
class Pallo(Collectible): # COLLECTIBLE_TYPE = 'PointBall' # VALUE = 1 # DRAIN = 20 def __init__(self, game, color, value = 1, drain = 20): self.game = game self.VALUE = value self.DRAIN = drain #self.idnumber = id self.visualNode = NodePath('Visual node') self.visualNode.reparentTo(render) model = loader.loadModel('Ball2.egg') model.setScale(2.5) model.reparentTo(self.visualNode) plight = PointLight('plight') plight.setPoint( Point3(0, 0, 3) ) plight.setColor( color ) plight.setAttenuation( Vec3(0.05, 0.01, 0.01) ) self.plightNodePath = model.attachNewNode(plight) model.setLight(self.plightNodePath) self.body = OdeBody(game.physicsWorld) self.mass = OdeMass() self.mass.setBox(10,1,1,1) self.body.setMass(self.mass) self.body.setGravityMode(True) #self.body.setGravityMode(False) #self.juttu = OdeUtil() self.collGeom = OdeSphereGeom( self.game.physicsSpace, 3.5) self.collGeom.setBody(self.body) self.collGeom.setCategoryBits( BitMask32(0xffffffff) ) self.collGeom.setCollideBits( BitMask32(0xffffffff) ) def getValue(self): return self.VALUE def hitShips(self, shipList): for ship in shipList: if OdeUtil.areConnected(ship.body, self.body) and not ship.hasBall(): self.PowerUpEffect(ship) def PowerUpEffect(self, ship): # ship.mass.add(self.mass) ship.addPower(-(self.DRAIN)) ship.gotBall(self) self.hideObject() ship.visualNode.setLight(self.plightNodePath) #print player print ship.SHIP_TYPE + " lost " + str(self.DRAIN) + " power!!" def Restore(self, ship): # ship.mass.add(self.mass) ship.addPower(self.DRAIN) self.showObject() ship.visualNode.clearLight(self.plightNodePath) #self.setPos( Vec3(random.randrange(30), random.randrange(40), 0)) #ship.dropBall() #print player print ship.getShipType() + " regained 20 power!!"
class Ship_1(Ship): POWER = 100 SHIP_TYPE = "UFO" def __init__(self, game, color): #self.POWER = 100 self.game = game self.thrust = False self.thrustLeft = False self.thrustRight = False self.thrustBack = False self.rotation = 0 self.body = OdeBody(game.physicsWorld) self.mass = OdeMass() self.mass.setBox(10,1,1,1) self.body.setMass(self.mass) #odespheregeom(... , size of hitbox sphere) self.collGeom = OdeSphereGeom( self.game.physicsSpace, 5) self.collGeom.setBody(self.body) self.collGeom.setCategoryBits( BitMask32(0xffffffff) ) self.collGeom.setCollideBits( BitMask32(0xffffffff) ) self.visualNode = NodePath('Visual node') self.visualNode.reparentTo(render) model = loader.loadModel('lautanen2.egg') model.reparentTo(self.visualNode) plight = PointLight('plight') plight.setPoint( Point3(0.6, 0, 5) ) plight.setColor( color ) plight.setAttenuation( Vec3(0.5, 0.01, 0.01) ) plightNodePath = model.attachNewNode(plight) model.setLight(plightNodePath) # setMoreKeys in ship class = BAD IDEA #def setMoreKeys(self): # base.accept('arrow_down', self.thrustBackOn) # base.accept('arrow_down-up', self.thrustBackOff) def applyForces(self): if self.thrust: heading = self.getHeading() self.body.addForce( -math.sin( math.radians(heading) ) * self.POWER, math.cos( math.radians(heading) ) * self.POWER, 0 ) if self.thrustLeft: heading = self.getHeading() self.body.addForce( -math.sin( math.radians(heading) + math.pi/2 ) * self.POWER, math.cos( math.radians(heading) + math.pi/2 ) * self.POWER, 0 ) if self.thrustRight: heading = self.getHeading() self.body.addForce( -math.sin( math.radians(heading) - math.pi/2 ) * self.POWER, math.cos( math.radians(heading) - math.pi/2 ) * self.POWER, 0 ) if self.thrustBack: heading = self.getHeading() self.body.addForce( -math.sin( math.radians(heading) + math.pi ) * self.POWER, math.cos( math.radians(heading) + math.pi ) * self.POWER, 0 )
class Ship_2(Ship): ROTATING_SPEED = 1 MAX_ROTATE_SPEED = 3.0 SHIP_TYPE = "RAKETTI" def __init__(self, game, color): self.POWER = 100 self.game = game self.thrust = False self.thrustLeft = False self.thrustRight = False self.thrustBack = False self.rotation = 0 self.body = OdeBody(game.physicsWorld) self.mass = OdeMass() self.mass.setBox(10,1,1,1) self.body.setMass(self.mass) #odespheregeom(... , size of hitbox sphere) self.collGeom = OdeSphereGeom( self.game.physicsSpace, 2) self.collGeom.setBody(self.body) self.collGeom.setCategoryBits( BitMask32(0xffffffff) ) self.collGeom.setCollideBits( BitMask32(0xffffffff) ) self.visualNode = NodePath('Visual node') self.visualNode.reparentTo(render) model = loader.loadModel('testipalikka.egg') model.reparentTo(self.visualNode) plight = PointLight('plight') plight.setPoint( Point3(0.6, 0, 5) ) plight.setColor( color ) plight.setAttenuation( Vec3(0.5, 0.01, 0.01) ) plightNodePath = model.attachNewNode(plight) model.setLight(plightNodePath) def rotating(self): if self.thrustLeft: self.setRotation(self.ROTATING_SPEED) if self.thrustRight: self.setRotation(-(self.ROTATING_SPEED)) if not self.thrustRight and not self.thrustLeft: self.setRotation(0) def applyForces(self): if self.thrust: heading = self.getHeading() #addForce (x, y, z) self.body.addForce( -math.sin( math.radians(heading) ) * self.POWER, math.cos( math.radians(heading) ) * self.POWER, 0 ) # uncomment for backwards thrust (eli siis pakki) # if self.thrustBack: # heading = self.getHeading() # self.body.addForce( # math.sin( math.radians(heading) ) * self.POWER, # -math.cos( math.radians(heading) ) * self.POWER, # 0 # ) self.rotating()
def createTire(self, tireIndex): """Create one physics tire. Returns a (nodePath, OdeBody, OdeGeom) tuple""" if (tireIndex < 0) or (tireIndex >= len(self.tireMasks)): self.notify.error('invalid tireIndex %s' % tireIndex) self.notify.debug("create tireindex %s" % (tireIndex)) zOffset = 0 # for now the tires are spheres body = OdeBody(self.world) mass = OdeMass() mass.setSphere(self.tireDensity, IceGameGlobals.TireRadius) body.setMass(mass) body.setPosition(IceGameGlobals.StartingPositions[tireIndex][0], IceGameGlobals.StartingPositions[tireIndex][1], IceGameGlobals.StartingPositions[tireIndex][2]) #body.setAutoDisableFlag(1) #body.setAutoDisableLinearThreshold(1.01 * MetersToFeet) # skipping AutoDisableAngularThreshold as that is radians per second #body.setAutoDisableAngularThreshold(0.1) body.setAutoDisableDefaults() geom = OdeSphereGeom(self.space, IceGameGlobals.TireRadius) self.space.setSurfaceType(geom, self.tireSurfaceType) self.space.setCollideId(geom, self.tireCollideIds[tireIndex]) self.massList.append(mass) self.geomList.append(geom) # a tire collides against other tires, the wall and the floor geom.setCollideBits(self.allTiresMask | self.wallMask | self.floorMask | self.obstacleMask) geom.setCategoryBits(self.tireMasks[tireIndex]) geom.setBody(body) if self.notify.getDebug(): self.notify.debug('tire geom id') geom.write() self.notify.debug(' -') if self.canRender: testTire = render.attachNewNode("tire holder %d" % tireIndex) smileyModel = NodePath( ) # loader.loadModel('models/misc/smiley') # smiley! if not smileyModel.isEmpty(): smileyModel.setScale(IceGameGlobals.TireRadius) smileyModel.reparentTo(testTire) smileyModel.setAlphaScale(0.5) smileyModel.setTransparency(1) testTire.setPos(IceGameGlobals.StartingPositions[tireIndex]) #debugAxis = loader.loadModel('models/misc/xyzAxis') if 0: #not debugAxis.isEmpty(): debugAxis.reparentTo(testTire) debugAxis.setScale(IceGameGlobals.TireRadius / 10.0) debugAxis2 = loader.loadModel('models/misc/xyzAxis') debugAxis2.reparentTo(testTire) debugAxis2.setScale(-IceGameGlobals.TireRadius / 10.0) # lets create a black tire #tireModel = loader.loadModel('phase_3/models/misc/sphere') tireModel = loader.loadModel( "phase_4/models/minigames/ice_game_tire") # assuming it has a radius of 1 tireHeight = 1 #tireModel.setScale(IceGameGlobals.TireRadius, IceGameGlobals.TireRadius, 1) #tireModel.setZ( 0 - IceGameGlobals.TireRadius + (tireHeight /2.0)) #tireModel.setColor(0,0,0) tireModel.setZ(-IceGameGlobals.TireRadius + 0.01) tireModel.reparentTo(testTire) #tireModel.setAlphaScale(0.5) #tireModel.setTransparency(1) self.odePandaRelationList.append((testTire, body)) else: testTire = None self.bodyList.append((None, body)) return testTire, body, geom
ballNP = ball.copyTo(render) ballNP.setPos(randint(-7, 7), randint(-7, 7), 10 + random() * 5.0) ballNP.setColor(random(), random(), random(), 1) ballNP.setHpr(randint(-45, 45), randint(-45, 45), randint(-45, 45)) # Create the body and set the mass ballBody = OdeBody(world) M = OdeMass() M.setSphere(50, 1) ballBody.setMass(M) ballBody.setPosition(ballNP.getPos(render)) ballBody.setQuaternion(ballNP.getQuat(render)) # Create a ballGeom ballGeom = OdeSphereGeom(space, 1) ballGeom.setCollideBits(BitMask32(0x00000001)) ballGeom.setCategoryBits(BitMask32(0x00000001)) ballGeom.setBody(ballBody) # Create the sound ballSound = loader.loadSfx("audio/sfx/GUI_rollover.wav") balls.append((ballNP, ballGeom, ballSound)) # Add a plane to collide with cm = CardMaker("ground") cm.setFrame(-20, 20, -20, 20) cm.setUvRange((0, 1), (1, 0)) ground = render.attachNewNode(cm.generate()) ground.setPos(0, 0, 0); ground.lookAt(0, 0, -1) groundGeom = OdePlaneGeom(space, (0, 0, 1, 0)) groundGeom.setCollideBits(BitMask32(0x00000001)) groundGeom.setCategoryBits(BitMask32(0x00000001)) # Add a texture to the ground