Ejemplo n.º 1
0
    def __init__(self, new_world, new_display, new_x, new_y, new_index,
                 new_type):
        box_base = Box(new_world, new_display, new_x, new_y, 1.6, 1.6,
                       new_index, new_type)
        self.boxes.append(box_base)

        shape = self.shapes[new_type]
        for pos in shape:
            box_add = Box(new_world, new_display, new_x, new_y, 1.6, 1.6,
                          new_index, new_type)
            rd = Box2D.b2WeldJointDef(
                bodyA=box_base.body,
                bodyB=box_add.body,
                localAnchorA=(pos[0] * self.SEPERATION,
                              pos[1] * self.SEPERATION),
            )
            new_world.b2_game_world.CreateJoint(rd)
            self.boxes.append(box_add)
Ejemplo n.º 2
0
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