def joint(self, *args): print "* Add Joint:", args if len(args) == 5: # Tracking Joint b1, b2, p1, p2, flag = args p1 = self.to_b2vec(p1) p2 = self.to_b2vec(p2) jointDef = box2d.b2DistanceJointDef() jointDef.Initialize(b1, b2, p1, p2) jointDef.collideConnected = flag self.parent.world.CreateJoint(jointDef) elif len(args) == 4: # Distance Joint b1, b2, p1, p2 = args p1 = self.to_b2vec(p1) p2 = self.to_b2vec(p2) jointDef = box2d.b2DistanceJointDef() jointDef.Initialize(b1, b2, p1, p2) jointDef.collideConnected = True self.parent.world.CreateJoint(jointDef) elif len(args) == 3: # Revolute Joint between two bodies (unimplemented) pass elif len(args) == 2: # Revolute Joint to the Background, at point b1 = self.parent.world.groundBody b2 = args[0] p1 = self.to_b2vec(args[1]) jointDef = box2d.b2RevoluteJointDef() jointDef.Initialize(b1, b2, p1) self.parent.world.CreateJoint(jointDef) elif len(args) == 1: # Revolute Joint to the Background, body center b1 = self.parent.world.groundBody b2 = args[0] p1 = b2.GetWorldCenter() jointDef = box2d.b2RevoluteJointDef() jointDef.Initialize(b1, b2, p1) self.parent.world.CreateJoint(jointDef)
def Grapple(self,pos): if self.joint: self.UnGrapple() return diff = pos - self.GetPos() distance,angle = cmath.polar(complex(diff.x,diff.y)) angle = (angle - (math.pi/2) - self.GetAngle())%(math.pi*2) #0 = pi*2 is straight ahead, pi is behind. #so 0.75 pi to 1.25 pi is allowed if not (angle <= self.min_shoot and angle >= self.max_shoot): return if distance > self.max_grapple: return #We need to determine if this point is in any objects... #We'll create a small AABB around the point, and get the list of potentially intersecting shapes, #then test each one to see if the point is inside it aabb = box2d.b2AABB() phys_pos = pos*self.physics.scale_factor #First of all make sure it's not inside us trans = box2d.b2XForm() trans.SetIdentity() p = phys_pos - Point(*self.body.position) if self.shapeI.TestPoint(trans,tuple(p)): return aabb.lowerBound.Set(phys_pos.x-0.1,phys_pos.y-0.1) aabb.upperBound.Set(phys_pos.x+0.1,phys_pos.y+0.1) (count,shapes) = self.physics.world.Query(aabb,10) for shape in shapes: trans = box2d.b2XForm() trans.SetIdentity() p = phys_pos - Point(*shape.GetBody().position) if shape.TestPoint(trans,tuple(p)): self.touching = shape self.contact = p break else: self.touching = None self.contact = None if not self.touching: return globals.sounds.grapple.play() #Tell the other body that it's in a joint with us so that target = self.touching.userData if target == None: self.touching = None self.contact = None return target.parent_joint = self self.child_joint = target joint = box2d.b2DistanceJointDef() joint.Initialize(self.body,self.touching.GetBody(),self.body.GetWorldCenter(),tuple(phys_pos)) joint.collideConnected = True self.joint = self.physics.world.CreateJoint(joint) self.grappled = True self.grapple_quad.Enable()
def distanceJoint(self, b1, b2, p1, p2): # Distance Joint p1 = self.to_b2vec(p1) p2 = self.to_b2vec(p2) jointDef = box2d.b2DistanceJointDef() jointDef.Initialize(b1, b2, p1, p2) jointDef.collideConnected = True self.parent.world.CreateJoint(jointDef)
def startGrab(self): assert not self.grabJoint candy = self.level.pickClosestCandy(self.body.position, 2*self.radius) if not candy: return self.candy = candy self.candy.grab() djd = b2d.b2DistanceJointDef() djd.body1 = self.body djd.body2 = candy.body djd.localAnchor1 = (0,0) djd.localAnchor2 = (0,0) djd.length = self.radius*2 self.grabJoint = self.level.world.CreateJoint(djd)
def Grab(self, obj, pos): #First we need to decide if we're close enough to grab it if self.joints: self.Ungrab() phys_pos = pos * self.physics.scale_factor centre = self.body.position diff = phys_pos - Point(centre[0], centre[1]) if diff.SquareLength() > self.stretching_arm_length: #Maybe waggle arms here? return distance, angle = cmath.polar(complex(diff.x, diff.y)) angle = (angle - (math.pi / 2) - self.GetAngle()) % (math.pi * 2) globals.sounds.grab.play() #You can catch a fire extinguisher from any angle if isinstance(obj, FloatingFireExtinguisher): #Need to add an impulse to this badger vel = obj.body.GetLinearVelocity() mass = obj.body.GetMass() momentum = vel * obj.body.GetMass() impulse = momentum / self.body.GetMass() self.body.ApplyImpulse(impulse, self.body.position) obj.Destroy() self.EquipFireExtinguisher(obj.level) return if not (angle < self.grab_angle or (math.pi * 2 - angle) < self.grab_angle): return for shoulder in self.shoulders: joint = box2d.b2DistanceJointDef() joint.Initialize(self.body, obj.body, self.body.GetWorldPoint(shoulder.to_vec()), tuple(phys_pos)) joint.collideConnected = True joint.frequencyHz = 4.0 joint.dampingRatio = 0.01 joint.length = self.resting_arm_length self.joints.append(self.physics.world.CreateJoint(joint)) self.other_obj = obj if isinstance(obj, Player): obj.parent_joint = self other_local_pos = Point(*obj.body.GetLocalPoint(phys_pos.to_vec())) self.arms[0].SetHand(obj, other_local_pos) self.arms[1].SetHand(obj, other_local_pos)
def __init__(self, figura_1, figura_2, fisica=None, con_colision=True): """Inicializa la constante. :param figura_1: Una de las figuras a conectar por la constante. :param figura_2: La otra figura a conectar por la constante. :param fisica: Referencia al motor de física. :param con_colision: Indica si se permite colisión entre las dos figuras. """ if not fisica: fisica = pilas.escena_actual().fisica if not isinstance(figura_1, Figura) or not isinstance(figura_2, Figura): raise Exception("Las dos figuras tienen que ser objetos de la clase Figura.") constante = box2d.b2DistanceJointDef() constante.Initialize(figura_1._cuerpo, figura_2._cuerpo, (0,0), (0,0)) constante.collideConnected = con_colision self.constante = fisica.mundo.CreateJoint(constante)
def json_load(self, path, serialized=False): import json self.world.groundBody.userData = {"saveid": 0} f = open(path, 'r') worldmodel = json.loads(f.read()) f.close() # clean world for joint in self.world.joints: self.world.DestroyJoint(joint) for body in self.world.bodies: if body != self.world.groundBody: self.world.DestroyBody(body) # load bodies for body in worldmodel['bodylist']: bodyDef = box2d.b2BodyDef() if body['dynamic']: bodyDef.type = box2d.b2_dynamicBody 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.b2FixtureDef() polyShape = box2d.b2PolygonShape() polyShape.vertices = shape['vertices'] polyDef.shape = polyShape polyDef.density = shape['density'] polyDef.restitution = shape['restitution'] polyDef.friction = shape['friction'] newBody.CreateFixture(polyDef) if shape['type'] == 'circle': circleDef = box2d.b2FixtureDef() circleShape = box2d.b2CircleShape() circleShape.radius = shape['radius'] circleShape.pos = shape['localPosition'] circleDef.shape = circleShape circleDef.density = shape['density'] circleDef.restitution = shape['restitution'] circleDef.friction = shape['friction'] newBody.CreateFixture(circleDef) 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.userData = 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.userData = joint['userData'] jointDef.motorEnabled = joint['enableMotor'] jointDef.motorSpeed = joint['motorSpeed'] jointDef.maxMotorTorque = joint['maxMotorTorque'] self.world.CreateJoint(jointDef) self.additional_vars = {} addvars = {} for (k, v) in list(worldmodel['additional_vars'].items()): addvars[k] = v if serialized and 'trackinfo' in addvars: trackinfo = addvars['trackinfo'] for key, info in trackinfo.items(): if not info[3]: addvars['trackinfo'][key][0] = \ self.getBodyWithSaveId(info[0]) addvars['trackinfo'][key][1] = \ self.getBodyWithSaveId(info[1]) else: addvars['trackinfo'][key][0] = None addvars['trackinfo'][key][1] = None self.additional_vars = addvars for body in self.world.bodies: del body.userData['saveid'] # remove temporary data
def json_load(self, path, additional_vars = {}): import cjson self.world.GetGroundBody().userData = {"saveid" : 0} f = open(path, 'r') worldmodel = cjson.decode(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 body.has_key('shapes'): 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
def json_load(self, path, serialized=False): 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 bodies 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) self.additional_vars = {} addvars = {} for (k, v) in worldmodel['additional_vars'].items(): addvars[k] = v if serialized and 'trackinfo' in addvars: trackinfo = addvars['trackinfo'] for key, info in trackinfo.iteritems(): if not info[3]: addvars['trackinfo'][key][0] = \ self.getBodyWithSaveId(info[0]) addvars['trackinfo'][key][1] = \ self.getBodyWithSaveId(info[1]) else: addvars['trackinfo'][key][0] = None addvars['trackinfo'][key][1] = None self.additional_vars = addvars for body in self.world.GetBodyList(): del body.userData['saveid'] # remove temporary data
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