Beispiel #1
0
    // Use this for initialization
    protected override void Init()
    {
        var ground = API.CreateBody(world, new Vector2(0,0), 0, BodyType.STATIC_BODY );
        API.AddEdgeShape(ground, new Vector2(-40.0f,0.0f), new Vector2(40.0f,0.0f), new ShapeDef(0));

        ShapeDef sp = new ShapeDef( 20.0f );
        sp.friction = 0.2f;

        Vector2 anchor;
        RevoluteJointDef jd = new RevoluteJointDef();

        IntPtr prevBody = ground;
        float y = 25;
        for(int i=0; i<e_count; ++i)
        {
            var b = API.CreateBody(world, new Vector2(0.5f+i,y), 0, BodyType.DYNAMIC_BODY );
            API.AddBoxShape(b, 0.6f, 0.125f, Vector2.zero, 0, sp);

            anchor = new Vector2(i,y);
            jd.Initialize(prevBody,b,anchor);
            API.CreateRevoluteJoint(world,jd);

            prevBody = b;
        }
    }
Beispiel #2
0
    // Use this for initialization
    protected override void Init()
    {
        var ground = API.CreateBody(world, new Vector2(0,0), 0, BodyType.STATIC_BODY );

        BodyDef bd = new BodyDef(BodyType.DYNAMIC_BODY);
        bd.allowSleep = false;
        bd.position = new Vector2(0,10);
        m_body = API.CreateBody(world, bd);
        API.AddBoxShape(m_body,0.5f,10.0f,new Vector2(10.0f,0),0,new ShapeDef(5.0f));
        API.AddBoxShape(m_body,0.5f,10.0f,new Vector2(-10.0f,0),0,new ShapeDef(5.0f));
        API.AddBoxShape(m_body,10.0f,0.5f,new Vector2(0,10),0,new ShapeDef(5.0f));
        API.AddBoxShape(m_body,10.0f,0.5f,new Vector2(0,-10),0,new ShapeDef(5.0f));

        RevoluteJointDef rjd = new RevoluteJointDef(IntPtr.Zero, IntPtr.Zero);
        rjd.bodyA = ground;
        rjd.bodyB = m_body;
        rjd.localAnchorA = new Vector2(0,10);
        rjd.localAnchorB = Vector2.zero;
        rjd.referenceAngle = 0;
        rjd.motorSpeed = 0.05f * Mathf.PI;
        rjd.maxMotorTorque = 1e8f;
        rjd.enableMotor = true;

        API.CreateRevoluteJoint(world, rjd);
    }
 // Use this for initialization
 protected override IntPtr Init()
 {
     Vector3 a = anchor;
     RevoluteJointDef def = new RevoluteJointDef(other.body, body.body);
     def.collideConnected = collideConnected;
     def.Initialize(other.body, body.body, new Vector2(a.x,a.y));
     def.enableLimit = enableLimit;
     def.lowerAngle = lowerAngle;
     def.upperAngle = upperAngle;
     def.enableMotor = enableMotor;
     def.motorSpeed = motorSpeed;
     def.maxMotorTorque = maxMotorTorque;
     return API.CreateRevoluteJoint(B2DWorld.instance.world, def);
 }
Beispiel #4
0
    // Use this for initialization
    protected override void Init()
    {
        var ground = API.CreateBody(world, new Vector2(0,0), 0, BodyType.STATIC_BODY );
        API.AddEdgeShape(ground, new Vector2(-40.0f,0.0f), new Vector2(40.0f,0.0f), new ShapeDef(0));

        ShapeDef sp = new ShapeDef( 20.0f );
        sp.friction = 0.2f;

        RevoluteJointDef def = new RevoluteJointDef();
        IntPtr prevBody = ground;
        for(int i=0; i<e_count; ++i)
        {
            var b = API.CreateBody(world, new Vector2(-14.5f+i,5.0f), 0, BodyType.DYNAMIC_BODY );
            API.AddBoxShape(b, 0.5f, 0.125f, Vector2.zero, 0, sp);

            def.Initialize(prevBody, b, new Vector2(-15.0f+i, 5.0f));
            API.CreateRevoluteJoint(world, def );
            prevBody = b;
        }

        def.Initialize(prevBody, ground, new Vector2(-15.0f+e_count, 5.0f));
        API.CreateRevoluteJoint(world,def);

        for(int i=0; i<2; ++i)
        {
            var b = API.CreateBody(world,new Vector2(-8.0f + 8*i,12), 0, BodyType.DYNAMIC_BODY);
            Vector2[] vertices = new Vector2[3];
            vertices[0].Set(-0.5f, 0.0f);
            vertices[1].Set(0.5f, 0.0f);
            vertices[2].Set(0.0f, 1.5f);
            API.AddPolygonShape(b,vertices,vertices.Length, new ShapeDef(1.0f));
        }

        for(int i=0; i<3; ++i)
        {
            var b = API.CreateBody(world,new Vector2(-6.0f + 6*i,10), 0, BodyType.DYNAMIC_BODY);
            API.AddCircleShape(b,0.5f,new ShapeDef(1.0f));
        }
    }
Beispiel #5
0
 public static extern IntPtr CreateRevoluteJoint( IntPtr w, RevoluteJointDef def );
Beispiel #6
0
    void CreateLeg(float s, Vector2 wheelAnchor)
    {
        Vector2 p1 = new Vector2(5.4f * s, -6.1f);
        Vector2 p2 = new Vector2(7.2f * s, -1.2f);
        Vector2 p3 = new Vector2(4.3f * s, -1.9f);
        Vector2 p4 = new Vector2(3.1f * s, 0.8f);
        Vector2 p5 = new Vector2(6.0f * s, 1.5f);
        Vector2 p6 = new Vector2(2.5f * s, 3.7f);

        ShapeDef sp = new ShapeDef(1.0f);
        sp.groupIndex = -1;
        Vector2[] vertices1 = new Vector2[3];
        Vector2[] vertices2 = new Vector2[3];
        if( s > 0.0f )
        {
            vertices1[0] = p1;
            vertices1[1] = p2;
            vertices1[2] = p3;

            vertices2[0] = Vector2.zero;
            vertices2[1] = p5-p4;
            vertices2[2] = p6-p4;
        }
        else
        {
            vertices1[0] = p1;
            vertices1[1] = p3;
            vertices1[2] = p2;

            vertices2[0] = Vector2.zero;
            vertices2[1] = p6-p4;
            vertices2[2] = p5-p4;
        }

        BodyDef bd1 = new BodyDef(BodyType.DYNAMIC_BODY);
        BodyDef bd2 = new BodyDef(BodyType.DYNAMIC_BODY);
        bd1.position = m_offset;
        bd2.position = p4 + m_offset;

        bd1.angularDamping = 10.0f;
        bd2.angularDamping = 10.0f;

        var body1 = API.CreateBody(world,bd1);
        var body2 = API.CreateBody(world,bd2);
        API.AddPolygonShape(body1, vertices1,vertices1.Length,sp);
        API.AddPolygonShape(body2, vertices2,vertices2.Length,sp);

        DistanceJointDef djd = new DistanceJointDef();
        djd.dampingRatio = 0.5f;
        djd.frequencyHz = 10.0f;

        djd.Initialize(body1,body2,p2+m_offset, p5+m_offset);
        API.CreateDistanceJoint(world, djd );

        djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset);
        API.CreateDistanceJoint(world, djd );

        djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset);
        API.CreateDistanceJoint(world, djd );

        djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset);
        API.CreateDistanceJoint(world, djd );

        RevoluteJointDef rjd = new RevoluteJointDef();
        rjd.Initialize(body2, m_chassis,p4+m_offset);
        API.CreateRevoluteJoint(world,rjd);
    }
Beispiel #7
0
    // Use this for initialization
    protected override void Init()
    {
        m_offset = new Vector2(0,8);
        m_motorSpeed = 2.0f;
        m_motorOn = true;
        Vector2 pivot = new Vector2(0,0.8f);

        //ground
        var ground = API.CreateBody(world, new Vector2(0,0), 0, BodyType.STATIC_BODY );
        API.AddEdgeShape( ground, new Vector2(-50,0), new Vector2(50,0), new ShapeDef(0) );
        API.AddEdgeShape( ground, new Vector2(-50,0), new Vector2(-50,10), new ShapeDef(0) );
        API.AddEdgeShape( ground, new Vector2(50,0), new Vector2(50,10), new ShapeDef(0) );

        //Balls
        for(int i=0; i<40; ++i)
        {
            var b = API.CreateBody(world, new Vector2(-40+2*i,0.5f), 0, BodyType.DYNAMIC_BODY);
            API.AddCircleShape(b, 0.25f, new ShapeDef(1.0f));
        }

        //Chassis
        ShapeDef sp = new ShapeDef(1.0f);
        sp.groupIndex = -1;
        m_chassis = API.CreateBody(world, pivot+m_offset,0,BodyType.DYNAMIC_BODY);
        API.AddBoxShape(m_chassis,2.5f,1.0f,Vector2.zero,0,sp);

        m_wheel = API.CreateBody(world, pivot+m_offset,0,BodyType.DYNAMIC_BODY);
        API.AddCircleShape(m_wheel,1.6f,sp);

        //		Vector2 localArchorA,localArchorB;
        //
        //		API.GetLocalPoint(m_wheel,pivot + m_offset,out localArchorA);
        //		API.GetLocalPoint(m_chassis,pivot + m_offset,out localArchorB);
        //		float referenceAngle = API.GetBodyAngle(m_chassis) - API.GetBodyAngle(m_wheel);
        //		API.CreateRevoluteJoint(m_wheel, m_chassis, localArchorA, localArchorB, false,
        //			referenceAngle,false, 0, 0, m_motorOn, m_motorSpeed, 400.0f);

        RevoluteJointDef rjd = new RevoluteJointDef();
        rjd.Initialize(m_wheel,m_chassis,pivot+m_offset);
        rjd.collideConnected = false;
        rjd.motorSpeed = m_motorSpeed;
        rjd.maxMotorTorque = 400.0f;
        rjd.enableMotor = m_motorOn;
        API.CreateRevoluteJoint(world, rjd );

        Vector2 wheelAnchor = pivot + new Vector2(0,-0.8f);

        CreateLeg(-1.0f, wheelAnchor);
        CreateLeg(1.0f, wheelAnchor);

        Vector2 pos;
        API.GetPosition(m_wheel, out pos);
        API.SetTransform(m_wheel, pos, 120.0f*Mathf.PI/180.0f);
        CreateLeg(-1.0f, wheelAnchor);
        CreateLeg(1.0f, wheelAnchor);

        API.GetPosition(m_wheel, out pos);
        API.SetTransform(m_wheel, pos, -120.0f*Mathf.PI/180.0f);
        CreateLeg(-1.0f, wheelAnchor);
        CreateLeg(1.0f, wheelAnchor);
    }
Beispiel #8
0
    // Use this for initialization
    protected override void Init()
    {
        m_hz = 4.0f;
        m_zeta = 0.7f;
        m_speed = 50.0f;

        IntPtr ground = IntPtr.Zero;
        {
            ground = API.CreateBody(world,new Vector2(0,0), 0, BodyType.STATIC_BODY);
            ShapeDef sp = new ShapeDef(0);
            sp.friction = 0.6f;
            API.AddEdgeShape(ground, new Vector2(-20,0),new Vector2(20,0), sp);

            float[] hs = new float[]{0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f};

            float x = 20.0f, y1 = 0.0f, dx = 5.0f;

            for (int i = 0; i < 10; ++i)
            {
                float y2 = hs[i];
                API.AddEdgeShape(ground, new Vector2(x,y1), new Vector2(x+dx,y2),sp);
                y1 = y2;
                x += dx;
            }

            for (int i = 0; i < 10; ++i)
            {
                float y2 = hs[i];
                API.AddEdgeShape(ground, new Vector2(x,y1), new Vector2(x+dx,y2),sp);
                y1 = y2;
                x += dx;
            }

            API.AddEdgeShape(ground, new Vector2(x,0), new Vector2(x+40,0),sp);

            x += 80.0f;
            API.AddEdgeShape(ground, new Vector2(x,0), new Vector2(x+40,0),sp);

            x += 40.0f;
            API.AddEdgeShape(ground, new Vector2(x,0), new Vector2(x+10,5),sp);

            x += 20.0f;
            API.AddEdgeShape(ground, new Vector2(x,0), new Vector2(x+40,0),sp);

            x += 40.0f;
            API.AddEdgeShape(ground, new Vector2(x,0), new Vector2(x,20),sp);
        }

        // Teeter
        {
            IntPtr body = API.CreateBody(world,new Vector2(140,1.0f), 0, BodyType.DYNAMIC_BODY);
            API.AddBoxShape(body,10,0.25f,Vector2.zero,0,new ShapeDef(1.0f));

            Vector2 archor;
            API.GetPosition(body, out archor);
            RevoluteJointDef jd = new RevoluteJointDef();
            jd.Initialize(ground,body,archor);
            jd.lowerAngle = -8.0f * Mathf.Deg2Rad;
            jd.upperAngle = 8.0f * Mathf.Deg2Rad;
            jd.enableLimit = true;

            API.CreateRevoluteJoint(world,jd);

            API.ApplyAngularImpulse(body, 100.0f);
        }

        // Bridge
        {
            int N = 20;
            ShapeDef sp = new ShapeDef(1.0f);
            sp.friction = 0.6f;

            Vector2 anchor;

            RevoluteJointDef jd = new RevoluteJointDef();
            IntPtr prevBody = ground;
            for (int i = 0; i < N; ++i)
            {
                var body = API.CreateBody(world,new Vector2(161.0f+2.0f*i, -0.125f), 0, BodyType.DYNAMIC_BODY);
                API.AddBoxShape(body,1.0f, 0.125f, Vector2.zero, 0, sp);

                anchor = new Vector2(160.0f + 2.0f * i, -0.125f);
                jd.Initialize(prevBody,body,anchor);
                API.CreateRevoluteJoint(world,jd);

                prevBody = body;
            }

            anchor = new Vector2(160.0f + 2.0f * N, -0.125f);
            jd.Initialize(prevBody,ground,anchor);
            API.CreateRevoluteJoint(world,jd);
        }

        // Boxes
        {
            var body = API.CreateBody(world,new Vector2(230.0f,0.5f), 0, BodyType.DYNAMIC_BODY);
            API.AddBoxShape(body,0.5f,0.5f, Vector2.zero,0,new ShapeDef(0.5f));

            body = API.CreateBody(world,new Vector2(230.0f,1.5f), 0, BodyType.DYNAMIC_BODY);
            API.AddBoxShape(body,0.5f,0.5f, Vector2.zero,0,new ShapeDef(0.5f));

            body = API.CreateBody(world,new Vector2(230.0f,2.5f), 0, BodyType.DYNAMIC_BODY);
            API.AddBoxShape(body,0.5f,0.5f, Vector2.zero,0,new ShapeDef(0.5f));

            body = API.CreateBody(world,new Vector2(230.0f,3.5f), 0, BodyType.DYNAMIC_BODY);
            API.AddBoxShape(body,0.5f,0.5f, Vector2.zero,0,new ShapeDef(0.5f));

            body = API.CreateBody(world,new Vector2(230.0f,4.5f), 0, BodyType.DYNAMIC_BODY);
            API.AddBoxShape(body,0.5f,0.5f, Vector2.zero,0,new ShapeDef(0.5f));
        }

        // Car
        {
            ShapeDef sp = new ShapeDef(1.0f);

            Vector2[] vertices = new Vector2[6];
            vertices[0] = new Vector2(-1.5f, -0.5f);
            vertices[1] = new Vector2(1.5f, -0.5f);
            vertices[2] = new Vector2(1.5f, 0.0f);
            vertices[3] = new Vector2(0.0f, 0.9f);
            vertices[4] = new Vector2(-1.15f, 0.9f);
            vertices[5] = new Vector2(-1.5f, 0.2f);

            m_car = API.CreateBody(world,new Vector2(0,1.0f),0,BodyType.DYNAMIC_BODY);
            API.AddPolygonShape(m_car,vertices,vertices.Length,sp);

            sp.friction = 0.9f;
            m_wheel1 = API.CreateBody(world,new Vector2(-1.0f,0.35f),0,BodyType.DYNAMIC_BODY);
            API.AddCircleShape(m_wheel1, 0.4f, sp);
            m_wheel2 = API.CreateBody(world,new Vector2(1.0f,0.4f),0,BodyType.DYNAMIC_BODY);
            API.AddCircleShape(m_wheel2, 0.4f, sp);

            WheelJointDef jd = new WheelJointDef();
            Vector2 axis = new Vector2(0.0f, 1.0f);
            Vector2 pos1,pos2;
            API.GetPosition(m_wheel1,out pos1);
            API.GetPosition(m_wheel2,out pos2);
            jd.Initialize(m_car, m_wheel1, pos1, axis);
            jd.motorSpeed = 0.0f;
            jd.maxMotorTorque = 20.0f;
            jd.enableMotor = true;
            jd.frequencyHz = m_hz;
            jd.dampingRatio = m_zeta;
            m_spring1 = API.CreateWheelJoint(world,jd);

            jd.Initialize(m_car, m_wheel2, pos2, axis);
            jd.motorSpeed = 0.0f;
            jd.maxMotorTorque = 10.0f;
            jd.enableMotor = false;
            jd.frequencyHz = m_hz;
            jd.dampingRatio = m_zeta;
            m_spring2 = API.CreateWheelJoint(world,jd);
        }
    }