void Start()
    {
        if (connectedBody == null)
        {
            return;
        }
        var jointDef = new DistanceJointDef();

        jointDef.CollideConnected = collideConnected;

        if (fixedLength)
        {
            jointDef.Body1        = GetComponent <Box2DBody>();
            jointDef.Body2        = connectedBody;
            jointDef.LocalAnchor1 = transform.InverseTransformPoint(anchor);
            jointDef.LocalAnchor2 = connectedBody.transform.InverseTransformPoint(connectedAnchor);
            jointDef.Length       = length;
        }
        else
        {
            jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody, anchor, connectedAnchor);
        }
        jointDef.FrequencyHz  = frequencyHz;
        jointDef.DampingRatio = dampingRatio;

        var world = Box2DWorld.Instance();

        joint = (DistanceJoint)world.CreateJoint(jointDef);
    }
示例#2
0
 public static Box2DWorld Instance()
 {
     if (instance == null)
     {
         var g = GameObject.Find("/" + typeof(Box2DWorld).Name);
         if (g == null)
         {
             Debug.LogError("failed to locate Box2DWorld within scene");
         }
         instance = g.GetComponent <Box2DWorld>();
     }
     return(instance);
 }
    void Start()
    {
        defaultGround = Box2DWorld.Instance().CreateDefaultGroundBody();

        var jointDef = new MouseJointDef();

        jointDef.Body1        = defaultGround;
        jointDef.Body2        = GetComponent <Box2DBody>();
        jointDef.Target       = worldMousePosition;
        jointDef.FrequencyHz  = 30;
        jointDef.DampingRatio = 0.2f;
        jointDef.MaxForce     = jointDef.Body2.GetMass() * force;

        joint = (MouseJoint)Box2DWorld.Instance().CreateJoint(jointDef);
    }
    void Update()
    {
        var ray = GetComponent <Camera>().ScreenPointToRay(Input.mousePosition);

        float rayIntersect;

        if (!plane.Raycast(ray, out rayIntersect))
        {
            return;
        }

        var pointOfInteresect = ray.GetPoint(rayIntersect);

        if (Input.GetMouseButtonDown(0))
        {
            AABB    aabb;
            Vector2 nudge = new Vector2(0.001f, 0.001f);

            aabb.LowerBound = (Vector2)pointOfInteresect - nudge;
            aabb.UpperBound = (Vector2)pointOfInteresect + nudge;

            Box2DBody body = Box2DWorld.Instance().QueryAABB(aabb);

            if (body != null)
            {
                mouseJoint = body.gameObject.AddComponent <Box2DMouseJoint>();
                mouseJoint.worldMousePosition = pointOfInteresect;
                mouseJoint.force = force;
            }
        }

        if (Input.GetMouseButton(0))
        {
            if (mouseJoint != null)
            {
                mouseJoint.worldMousePosition = pointOfInteresect;
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            if (mouseJoint != null)
            {
                Destroy(mouseJoint);
                mouseJoint = null;
            }
        }
    }
示例#5
0
    void Start()
    {
        var jointDef = new PulleyJointDef();

#if USING_BOX2DX
        jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody,
                            groundAnchor1.ToVec2(), groundAnchor2.ToVec2(), anchor1.ToVec2(), anchor2.ToVec2(), ratio);
#else
        //jointDef.Length1 = 10f;

        jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio);

        jointDef.MaxLength1 = 10f;
        jointDef.MaxLength2 = 10f;
#endif
        joint = (PulleyJoint)Box2DWorld.Instance().CreateJoint(jointDef);
    }
示例#6
0
    void CreateBody()
    {
        BodyDef bodyDef = new BodyDef();

        bodyDef.Position       = transform.position;
        bodyDef.Angle          = transform.AngleAroundZ();
        bodyDef.LinearDamping  = drag;
        bodyDef.AngularDamping = angularDamping;
        Box2DWorld box2Dworld = Box2DWorld.Instance();

        box2Dworld.CreateBody(this, bodyDef);

        var rectShapes = GetComponents <Box2DRectangleShape>();

        if (rectShapes != null)
        {
            Array.ForEach(rectShapes, CreateFixture <Box2DRectangleShape>);
        }

        var polyShapes = GetComponents <Box2DPolyShape>();

        if (polyShapes != null)
        {
            Array.ForEach(polyShapes, CreateFixture <Box2DPolyShape>);
        }

        var edgeShapes = GetComponents <Box2DEdgeShape>();

        if (edgeShapes != null)
        {
            Array.ForEach(edgeShapes, CreateFixture <Box2DEdgeShape>);
        }

        var circleShapes = GetComponents <Box2DCircleShape>();

        if (circleShapes != null)
        {
            Array.ForEach(circleShapes, CreateFixture <Box2DCircleShape>);
        }

        if (setMassFromShapes)
        {
            body.SetMassFromShapes();
        }
    }
示例#7
0
    void Start()
    {
        if (connectedBody == null)
        {
            return;
        }
        var jointDef = new PrismaticJointDef();

#if USING_BOX2DX
        jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody, anchor.ToVec2(), axis.ToVec2());
#else
        jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody, anchor, axis);
#endif
        jointDef.MaxMotorForce = maxMotorForce;
        jointDef.EnableMotor   = enableMotor;

        joint = (PrismaticJoint)Box2DWorld.Instance().CreateJoint(jointDef);
    }
    void Start()
    {
        if (connectedBody == null)
        {
            return;
        }
        var jointDef = new RevoluteJointDef();

#if USING_BOX2DX
        jointDef.Initialize(connectedBody, GetComponent <Box2DBody>(), anchor.ToVec2());
#else
        jointDef.Initialize(connectedBody, GetComponent <Box2DBody>(), anchor);
#endif
        jointDef.MotorSpeed       = motorSpeed;
        jointDef.MaxMotorTorque   = maxMotorTorque;
        jointDef.EnableMotor      = enableMotor;
        jointDef.CollideConnected = collideConnected;

        joint = (RevoluteJoint)Box2DWorld.Instance().CreateJoint(jointDef);
    }
    void Start()
    {
        //if (connectedBody == null) {
        var jointDef = new GearJointDef();

        jointDef.Body1  = GetComponent <Box2DBody>();
        jointDef.Body2  = connectedBody;
        jointDef.Joint1 = joint1;
        jointDef.Joint2 = joint2;


        // [CHRISK] TODO
        //}

                #if USING_BOX2DX
                #else
        //	jointDef.Initialize(connectedBody, GetComponent<Box2DBody>(), anchor);
                #endif

        var world = Box2DWorld.Instance();
        joint = (GearJoint)world.CreateJoint(jointDef);
    }
示例#10
0
 void Start()
 {
     _world = Box2DWorld.Instance();
 }