Beispiel #1
0
    def add_joint(self, name, joint_type, obj1_name, obj2_name, joint_args, anchor_offset=None):
        joint = None

        if anchor_offset is None:
            anchor_offset = 0
        anchor = self.bodies[obj1_name].worldCenter + anchor_offset

        if joint_type == 'revolute':
            joint = self.world.CreateRevoluteJoint(
                bodyA = self.bodies[obj1_name],
                bodyB = self.bodies[obj2_name],
                anchor = anchor,
                **joint_args
            )
        elif joint_type == 'prismatic':
            joint = self.world.CreatePrismaticJoint(
                bodyA = self.bodies[obj1_name],
                bodyB = self.bodies[obj2_name],
                anchor = anchor,
                **joint_args
            )
        elif joint_type == 'weld':
            joint = self.world.CreateWeldJoint(
                bodyA = self.bodies[obj1_name],
                bodyB = self.bodies[obj2_name],
                anchor = anchor
            )
            joint.frequencyHz = 0
            joint.dampingRatio = 1
        elif joint_type == 'rope':
            r_joint = Box2D.b2RopeJointDef(
                bodyA = self.bodies[obj1_name],
                bodyB = self.bodies[obj2_name],
                **joint_args
            )
            joint = self.world.CreateJoint(r_joint)

        if joint is not None:
            self.joints[name] = joint
        else:
            raise ValueError('Unsupported joint type ' + joint_type + ' was specified')
Beispiel #2
0
    def __init__(self, figura_1, figura_2, figura_1_punto, figura_2_punto, longitud_maxima, 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 figura_1_punto: Punto de conexiin de la figura_1
        :param figura_2_punto: Punto de conexion de la figura_2
        :param longitud_maxima: Longitu Maxima de distancia que puede alcanzar la conexion
        :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.b2RopeJointDef(bodyA=figura_1._cuerpo, bodyB=figura_2._cuerpo)
        constante.localAnchorA = convertir_a_metros(figura_1_punto[0]), convertir_a_metros(figura_1_punto[1])
        constante.localAnchorB = convertir_a_metros(figura_2_punto[0]), convertir_a_metros(figura_2_punto[1])
        constante.maxLength = convertir_a_metros(longitud_maxima)
        constante.collideConnected = con_colision
        self.constante = fisica.mundo.CreateJoint(constante)
Beispiel #3
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