def __init__(self, bridgeLoc, switchLoc, fallsLeft): self.isSwitchHit = False # create bridge bodyDef = box2d.b2BodyDef() bodyDef.position.Set( bridgeLoc[0], bridgeLoc[1] + BridgeAndSwitch.bridgeLength * .5 ) self.bridgeBody = X2OGame.game.world.CreateBody(bodyDef) self.bridgeBody.SetUserData(BRIDGE) self.fallsLeft = fallsLeft shapeDef = box2d.b2PolygonDef() shapeDef.SetAsBox(BridgeAndSwitch.bridgeThickness*.5, BridgeAndSwitch.bridgeLength*.5) shapeDef.density = 1.0 shape = self.bridgeBody.CreateShape(shapeDef) self.bridgeBody.SetMassFromShapes() shape.SetUserData(BRIDGE) rjd = box2d.b2RevoluteJointDef() rjd.Initialize(self.bridgeBody, X2OGame.game.world.GetGroundBody(), box2d.b2Vec2(bridgeLoc[0],bridgeLoc[1])) rjd.enableLimit = True rjd.lowerAngle = -.05 rjd.upperAngle = .05 rjd.collideConnected = True self.bridgeJoint = X2OGame.game.world.CreateJoint(rjd) bodyDef2 = box2d.b2BodyDef() bodyDef2.position.Set( switchLoc[0], switchLoc[1] + BridgeAndSwitch.switchLength*.5 ) self.switchBody = X2OGame.game.world.CreateBody(bodyDef2) self.switchBody.SetUserData(BRIDGE_SWITCH) shapeDef2 = box2d.b2PolygonDef() shapeDef2.SetAsBox(BridgeAndSwitch.switchThickness*.5, BridgeAndSwitch.switchLength*.5) shapeDef2.density = 5.0 shape2 = self.switchBody.CreateShape(shapeDef2) self.switchBody.SetMassFromShapes() shape2.SetUserData(BRIDGE_SWITCH) rjd2 = box2d.b2RevoluteJointDef() rjd2.Initialize(self.switchBody, X2OGame.game.world.GetGroundBody(), box2d.b2Vec2(switchLoc[0],switchLoc[1])) rjd2.enableLimit = True rjd2.lowerAngle = -.02 rjd2.upperAngle = .02 rjd2.collideConnected = True self.switchJoint = X2OGame.game.world.CreateJoint(rjd2) BodySprite(self.bridgeBody,BridgeAndSwitch.bridgePicLoc,BridgeAndSwitch.bridgeThickness,BridgeAndSwitch.bridgeLength,0,0,X2OGame.game.worldToScreen,X2OGame.game.screenToWorld) BodySprite(self.switchBody,BridgeAndSwitch.switchPicLoc,BridgeAndSwitch.switchThickness,BridgeAndSwitch.switchLength,0,0,X2OGame.game.worldToScreen,X2OGame.game.screenToWorld) BodySprite(X2OGame.game.world.GetGroundBody(), BridgeAndSwitch.basePicLoc, BridgeAndSwitch.baseWidth, BridgeAndSwitch.baseHeight, -switchLoc[0], switchLoc[1],X2OGame.game.worldToScreen,X2OGame.game.screenToWorld)
def __init__(self, hingeLoc, opensLeft): self.resetTimer = 0 bodyDef = box2d.b2BodyDef() endX = hingeLoc[0] + Launcher.launcherLength if opensLeft: endX = hingeLoc[0] - Launcher.launcherLength bodyDef.position.Set( (hingeLoc[0]+endX) * .5, hingeLoc[1] ) self.launcherBody = X2OGame.game.world.CreateBody(bodyDef) self.launcherBody.SetUserData(LAUNCHER) self.opensLeft = opensLeft shapeDef = box2d.b2PolygonDef() shapeDef.SetAsBox(Launcher.launcherLength*.5, Launcher.launcherThickness*.5) shapeDef.density = 1.0 shape = self.launcherBody.CreateShape(shapeDef) self.launcherBody.SetMassFromShapes() shape.SetUserData(LAUNCHER) rjd = box2d.b2RevoluteJointDef() rjd.Initialize(self.launcherBody, X2OGame.game.world.GetGroundBody(), box2d.b2Vec2(hingeLoc[0],hingeLoc[1])) rjd.enableLimit = True rjd.lowerAngle = -3.1415 * .333 rjd.upperAngle = 0 if opensLeft: rjd.lowerAngle = 0 rjd.upperAngle = 3.1415 * .333 self.hingeJoint = X2OGame.game.world.CreateJoint(rjd) BodySprite(self.launcherBody,Launcher.launcherPicLoc,Launcher.launcherLength,Launcher.launcherThickness,0,0,X2OGame.game.worldToScreen,X2OGame.game.screenToWorld)
def makeBoundingBox(self): body = self.addBody((0,80),self) boxDef = box2d.b2PolygonDef() width = 80 height = 5 boxDef.SetAsBox(width,height) boxDef.density = 0 body.CreateShape(boxDef) body = self.addBody((0,-80),self) body.CreateShape(boxDef) body = self.addBody((-80,0),self) boxDef = box2d.b2PolygonDef() width = 5 height = 80 boxDef.SetAsBox(width,height) boxDef.density = 0 body.CreateShape(boxDef) body = self.addBody((80,0),self) body.CreateShape(boxDef)
def createGround(game, world, vertexList, thickness, pic): list = [] for i in range(0, len(vertexList)-1): va = vertexList[i] vb = vertexList[i+1] dx = vb[0]-va[0] dy = vb[1]-va[1] angle = atan2(dy,dx) distSqr = dx*dx + dy*dy dist = sqrt(distSqr) bodyDef = box2d.b2BodyDef() bodyDef.position.Set((va[0]+vb[0])*.5, (va[1]+vb[1])*.5) body = world.CreateBody(bodyDef) shapeDef = box2d.b2PolygonDef() shapeDef.SetAsBox(dist*.5, thickness*.5) shape = body.CreateShape(shapeDef) shape.SetUserData(SCENERY) body.SetXForm(body.GetXForm().position,angle) BodySprite(body,pic,dist,thickness*3,0,0,game.worldToScreen, game.screenToWorld) body.SetUserData(SCENERY) list.append( body) return list
def __init__(self, game, location, bottomGraphicFile, topGraphicFile): self.jumpTimer = RollCat.initialJumpTimer self.game = game self.world = game.world self.cachedOnGround = False self.jumpState = RollCat.NO_GROUND_TOUCH bodyDef = box2d.b2BodyDef() bodyDef.position.Set(location[0], location[1]+1) #bodyDef.fixedRotation = True body = self.world.CreateBody(bodyDef) shapeDef = box2d.b2PolygonDef() shapeDef.SetAsBox(1, 1) shapeDef.density = 1 shapeDef.restitution = 0.3 shapeDef.friction = 1.0 shape = body.CreateShape(shapeDef) shape.SetUserData(-1) body.SetMassFromShapes() self.torso = body self.torso.SetUserData(ROLLCAT_BODY) bodyDef3 = box2d.b2BodyDef() bodyDef3.position.Set(location[0],location[1]) body3 = self.world.CreateBody(bodyDef3) shapeDef3 = box2d.b2CircleDef() shapeDef3.radius = 0.95 shapeDef3.density = 1 shapeDef3.restitution = 0.1 shapeDef3.friction = 3.0 shape3 = body3.CreateShape(shapeDef3) shape3.SetUserData(-1) #don't need body3.SetMassFromShapes() self.wheel = body3 self.wheel.SetUserData(ROLLCAT_WHEEL) sensorDef = box2d.b2CircleDef() sensorDef.radius = 0.9 sensorDef.isSensor = True sensorDef.localPosition = box2d.b2Vec2(0,-1.1) sensorShape = self.torso.CreateShape(sensorDef) sensorShape.SetUserData(ROLLCAT_JUMP_SENSOR) sensorDef2 = box2d.b2PolygonDef() sensorDef2.SetAsBox(1.1,1.6,box2d.b2Vec2(0,.5),0) sensorDef2.isSensor = True sensorShape2 = self.torso.CreateShape(sensorDef2) sensorShape2.SetUserData(ROLLCAT_WIN_SENSOR) jd = box2d.b2RevoluteJointDef() jd.Initialize(body, body3, box2d.b2Vec2(location[0],location[1])) revJoint = self.world.CreateJoint(jd).getAsType() self.axle = revJoint revJoint.EnableMotor(True) revJoint.SetMotorSpeed(0) revJoint.SetMaxMotorTorque(4000) self.motorSpeed = 0 self.targetAngle = 0 BodySprite(body3,bottomGraphicFile,2, 2, 0, 0, self.game.worldToScreen, self.game.screenToWorld) BodySprite(body,topGraphicFile,2, 2, 0, 0,self.game.worldToScreen, self.game.screenToWorld)
def json_load(self, path, additional_vars={}): import json self.world.GetGroundBody().userData = {"saveid": 0} f = open(path, 'r') worldmodel = json.loads(f.read()) f.close() # clean world for joint in self.world.GetJointList(): self.world.DestroyJoint(joint) for body in self.world.GetBodyList(): if body != self.world.GetGroundBody(): self.world.DestroyBody(body) # load bodys for body in worldmodel['bodylist']: bodyDef = box2d.b2BodyDef() bodyDef.position = body['position'] bodyDef.userData = body['userData'] bodyDef.angle = body['angle'] newBody = self.world.CreateBody(bodyDef) #_logger.debug(newBody) newBody.angularVelocity = body['angularVelocity'] newBody.linearVelocity = body['linearVelocity'] if 'shapes' in body: for shape in body['shapes']: if shape['type'] == 'polygon': polyDef = box2d.b2PolygonDef() polyDef.setVertices(shape['vertices']) polyDef.density = shape['density'] polyDef.restitution = shape['restitution'] polyDef.friction = shape['friction'] newBody.CreateShape(polyDef) if shape['type'] == 'circle': circleDef = box2d.b2CircleDef() circleDef.radius = shape['radius'] circleDef.density = shape['density'] circleDef.restitution = shape['restitution'] circleDef.friction = shape['friction'] circleDef.localPosition = shape['localPosition'] newBody.CreateShape(circleDef) newBody.SetMassFromShapes() for joint in worldmodel['jointlist']: if joint['type'] == 'distance': jointDef = box2d.b2DistanceJointDef() body1 = self.getBodyWithSaveId(joint['body1']) anch1 = joint['anchor1'] body2 = self.getBodyWithSaveId(joint['body2']) anch2 = joint['anchor2'] jointDef.collideConnected = joint['collideConnected'] jointDef.Initialize(body1, body2, anch1, anch2) jointDef.SetUserData(joint['userData']) self.world.CreateJoint(jointDef) if joint['type'] == 'revolute': jointDef = box2d.b2RevoluteJointDef() body1 = self.getBodyWithSaveId(joint['body1']) body2 = self.getBodyWithSaveId(joint['body2']) anchor = joint['anchor'] jointDef.Initialize(body1, body2, anchor) jointDef.SetUserData(joint['userData']) jointDef.enableMotor = joint['enableMotor'] jointDef.motorSpeed = joint['motorSpeed'] jointDef.maxMotorTorque = joint['maxMotorTorque'] self.world.CreateJoint(jointDef) for (k, v) in worldmodel['additional_vars'].items(): additional_vars[k] = v for body in self.world.GetBodyList(): del body.userData['saveid'] # remove temporary data