def load(self, angle, radius, length): _x, _y=length*math.cos(angle),length*math.sin(angle) position= self.parent.body.position.x+_x, self.parent.body.position.y+_y bd=box2d.b2BodyDef() bd.fixedRotation=False bd.position=position sd=box2d.b2CircleDef() sd.radius=radius sd.density=1.0 sd.filter.categoryBits= PistonBall.catBits self.body=WorldConfig.world.CreateBody(bd) self.body.CreateShape(sd) self.body.SetMassFromShapes() self.body.userData=self pjd=box2d.b2PrismaticJointDef() pjd.Initialize(self.parent.body, self.body, self.parent.body.position, (math.cos(angle), math.sin(angle))) pjd.motorSpeed = 30.0 pjd.maxMotorForce = 1000.0 pjd.enableMotor = True pjd.lowerTranslation = 0.0 pjd.upperTranslation = 10.0 pjd.enableLimit = True self.joint=WorldConfig.world.CreateJoint(pjd).getAsType()
def create_physical_entity(self): body = self._engine.CreateDynamicBody(position=self.physical_position, fixedRotation=True) body.CreatePolygonFixture( box=((self.width / 2.0) / self._world.physical_scale, (self.height / 2.0) / self._world.physical_scale), density=1.0, friction=0.0, restitution=0.0) # Constrain missile movements to Y axis. joint = box_2d.b2PrismaticJointDef() joint.Initialize(body, self._world.ground, body.worldCenter, (0.0, 1.0)) joint.collideConnected = True self._engine.CreateJoint(joint) return body
def addPrismatic(self): sd1=box2d.b2CircleDef() sd1.radius = 2 sd1.density = 2.0 bd1=box2d.b2BodyDef() ppos= self.body.GetWorldCenter() bd1.position = (ppos.x+6, ppos.y) body_punch = WorldConfig.world.CreateBody(bd1) body_punch.CreateShape(sd1) body_punch.SetMassFromShapes() jointDef = box2d.b2PrismaticJointDef() jointDef.Initialize(self.body, body_punch,playerPos, (1.0,0.0)) jointDef.lowerTranslation = 0.0 jointDef.upperTranslation = 10 jointDef.enableLimit = True jointDef.motorSpeed = 100.0 jointDef.enableMotor = True WorldConfig.world.CreateJoint(jointDef).getAsType() self.bodies.append(body_punch)
def create_jointDef(jsw_joint, b2_world): """create a b2JointDef from the json parameters of the joint :param jsw_joint: dictionary defining the parameters of the joint :type jsw_joint: dict(sting: variant) :param b2_world: an handler to a b2World object :type b2_world: b2World reference :return: the joint definition object :rtype: b2JointDef """ joint_type = jsw_joint["type"] # naming # --------------------------------------------------- if joint_type == "revolute": # Done jointDef = b2.b2RevoluteJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableLimit", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setAttr(jsw_joint, "jointSpeed", jointDef, "motorSpeed") setAttr(jsw_joint, "lowerLimit", jointDef, "lowerAngle") setAttr(jsw_joint, "maxMotorTorque", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "upperLimit", jointDef, "upperAngle") # --------------------------------------------------- elif joint_type == "distance": # Done jointDef = b2.b2DistanceJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "dampingRatio", jointDef) setAttr(jsw_joint, "frequency", jointDef, "frequencyHz") setAttr(jsw_joint, "length", jointDef) # --------------------------------------------------- elif joint_type == "prismatic": # Done jointDef = b2.b2PrismaticJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableLimit", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setB2Vec2Attr(jsw_joint, "localAxisA", jointDef, "axis") setAttr(jsw_joint, "lowerLimit", jointDef, "lowerTranslation") setAttr(jsw_joint, "maxMotorForce", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "upperLimit", jointDef, "upperTranslation") # --------------------------------------------------- elif joint_type == "wheel": # Done jointDef = b2.b2WheelJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setB2Vec2Attr(jsw_joint, "localAxisA", jointDef) setAttr(jsw_joint, "maxMotorTorque", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "springDampingRatio", jointDef, "dampingRatio") setAttr(jsw_joint, "springFrequency", jointDef, "frequencyHz") # --------------------------------------------------- elif joint_type == "rope": # Done jointDef = b2.b2RopeJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxLength", jointDef) # --------------------------------------------------- elif joint_type == "motor": # Done jointDef = b2.b2MotorJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxForce", jointDef) setAttr(jsw_joint, "maxTorque", jointDef) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "linearOffset") setAttr(jsw_joint, "correctionFactor", jointDef) # --------------------------------------------------- elif joint_type == "weld": # Done jointDef = b2.b2WeldJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "dampingRatio", jointDef) setAttr(jsw_joint, "frequency", jointDef, "frequencyHz") # --------------------------------------------------- elif joint_type == "friction": # Done jointDef = b2.b2FrictionJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxForce", jointDef) setAttr(jsw_joint, "maxTorque", jointDef) else: print("unsupported joint type") return jointDef
def create_jointDef(jsw_joint, b2_world): joint_type = jsw_joint["type"] # naming #--------------------------------------------------- if joint_type == "revolute": # Done jointDef = b2.b2RevoluteJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableLimit", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setAttr(jsw_joint, "jointSpeed", jointDef, "motorSpeed") setAttr(jsw_joint, "lowerLimit", jointDef, "lowerAngle") setAttr(jsw_joint, "maxMotorTorque", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "upperLimit", jointDef, "upperAngle") #--------------------------------------------------- elif joint_type == "distance": # Done jointDef = b2.b2DistanceJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "dampingRatio", jointDef) setAttr(jsw_joint, "frequency", jointDef, "frequencyHz") setAttr(jsw_joint, "length", jointDef) #--------------------------------------------------- elif joint_type == "prismatic": # Done jointDef = b2.b2PrismaticJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableLimit", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setB2Vec2Attr(jsw_joint, "localAxisA", jointDef, "axis") setAttr(jsw_joint, "lowerLimit", jointDef, "lowerTranslation") setAttr(jsw_joint, "maxMotorForce", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "upperLimit", jointDef, "upperTranslation") #--------------------------------------------------- elif joint_type == "wheel": # Done jointDef = b2.b2WheelJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setB2Vec2Attr(jsw_joint, "localAxisA", jointDef) setAttr(jsw_joint, "maxMotorTorque", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "springDampingRatio", jointDef, "dampingRatio") setAttr(jsw_joint, "springFrequency", jointDef, "frequencyHz") #--------------------------------------------------- elif joint_type == "rope": # Done jointDef = b2.b2RopeJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxLength", jointDef) #--------------------------------------------------- elif joint_type == "motor": # Done jointDef = b2.b2MotorJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxForce", jointDef) setAttr(jsw_joint, "maxTorque", jointDef) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "linearOffset") setAttr(jsw_joint, "correctionFactor", jointDef) #--------------------------------------------------- elif joint_type == "weld": # Done jointDef = b2.b2WeldJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "dampingRatio", jointDef) setAttr(jsw_joint, "frequency", jointDef, "frequencyHz") #--------------------------------------------------- elif joint_type == "friction": # Done jointDef = b2.b2FrictionJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxForce", jointDef) setAttr(jsw_joint, "maxTorque", jointDef) else: print("unsupported joint type") return jointDef
def create_jointDef(jsw_joint, b2_world): joint_type = jsw_joint["type"] # naming #--------------------------------------------------- if joint_type == "revolute": # Done jointDef = b2.b2RevoluteJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableLimit", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setAttr(jsw_joint, "jointSpeed", jointDef, "motorSpeed") setAttr(jsw_joint, "lowerLimit", jointDef, "lowerAngle") setAttr(jsw_joint, "maxMotorTorque", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "upperLimit", jointDef, "upperAngle") #--------------------------------------------------- elif joint_type == "distance": # Done jointDef = b2.b2DistanceJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "dampingRatio", jointDef) setAttr(jsw_joint, "frequency", jointDef, "frequencyHz") setAttr(jsw_joint, "length", jointDef) #--------------------------------------------------- elif joint_type == "prismatic": # Done jointDef = b2.b2PrismaticJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableLimit", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setB2Vec2Attr(jsw_joint, "localAxisA", jointDef, "axis") setAttr(jsw_joint, "lowerLimit", jointDef, "lowerTranslation") setAttr(jsw_joint, "maxMotorForce", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "upperLimit", jointDef, "upperTranslation") #--------------------------------------------------- elif joint_type == "wheel": # Done jointDef = b2.b2WheelJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "enableMotor", jointDef) setB2Vec2Attr(jsw_joint, "localAxisA", jointDef) setAttr(jsw_joint, "maxMotorTorque", jointDef) setAttr(jsw_joint, "motorSpeed", jointDef) setAttr(jsw_joint, "springDampingRatio", jointDef, "dampingRatio") setAttr(jsw_joint, "springFrequency", jointDef, "frequencyHz") #--------------------------------------------------- elif joint_type == "rope": # Done jointDef = b2.b2RopeJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxLength", jointDef) #--------------------------------------------------- elif joint_type == "motor": # Done jointDef = b2.b2MotorJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxForce", jointDef) setAttr(jsw_joint, "maxTorque", jointDef) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "linearOffset") setAttr(jsw_joint, "correctionFactor", jointDef) #--------------------------------------------------- elif joint_type == "weld": # Done jointDef = b2.b2WeldJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "refAngle", jointDef, "referenceAngle") setAttr(jsw_joint, "dampingRatio", jointDef) setAttr(jsw_joint, "frequency", jointDef, "frequencyHz") #--------------------------------------------------- elif joint_type == "friction": # Done jointDef = b2.b2FrictionJointDef() jointDef.bodyA = get_body(b2_world, jsw_joint["bodyA"]) jointDef.bodyB = get_body(b2_world, jsw_joint["bodyB"]) setB2Vec2Attr(jsw_joint, "anchorA", jointDef, "localAnchorA") setB2Vec2Attr(jsw_joint, "anchorB", jointDef, "localAnchorB") setAttr(jsw_joint, "collideConnected", jointDef) setAttr(jsw_joint, "maxForce", jointDef) setAttr(jsw_joint, "maxTorque", jointDef) else: print ("unsupported joint type") return jointDef