Exemplo n.º 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;
        }
    }
Exemplo n.º 2
0
    // Use this for initialization
    protected override IntPtr Create( ShapeDef def )
    {
        Vector2[] vs = new Vector2[vertexTransform.Length];
        for(int i=0; i<vs.Length; ++i)
            vs[i] = new Vector2( vertexTransform[i].localPosition.x, vertexTransform[i].localPosition.y );

        if( destroyTransform )
        {
            for(int i=0; i<vertexTransform.Length; ++i)
            {
                Destroy( vertexTransform[i].gameObject );
            }
        }

        return API.AddPolygonShape(body.body, vs, vs.Length, def);
    }
Exemplo n.º 3
0
    // Use this for initialization
    public void Start()
    {
        if (started) return;
        started = true;

        if (!body.Started) body.Start();

        ShapeDef def = new ShapeDef(density);
        def.restitution = restitution;
        def.friction = friction;
        def.sensor = sensor;
        def.catogoryBits = catogoryBits;
        def.maskBits = def.maskBits;
        def.groupIndex = def.groupIndex;

        fixture = Create( def );
    }
Exemplo n.º 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));
        API.AddEdgeShape(ground, new Vector2(20.0f,0.0f), new Vector2(20.0f,20.0f), new ShapeDef(0));

        float[] xs = new float[5] {0.0f, -10.0f, -5.0f, 5.0f, 10.0f};

        ShapeDef sp = new ShapeDef(1.0f);
        sp.friction = 0.3f;

        for (int j = 0; j < e_columnCount; ++j)
        {
            for (int i = 0; i < e_rowCount; ++i)
            {
                var b = API.CreateBody( world, new Vector2(xs[j], 0.752f + 1.54f * i), 0, BodyType.DYNAMIC_BODY );
                API.AddBoxShape(b, 0.5f, 0.5f, Vector2.zero,0,sp);
            }
        }
    }
Exemplo n.º 5
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));
        }
    }
Exemplo n.º 6
0
 public static extern IntPtr AddEdgeShape(IntPtr b, Vector2 p0, Vector2 p1, ShapeDef p);
Exemplo n.º 7
0
 public static extern IntPtr AddCircleShape(IntPtr b, float r, ShapeDef p);
Exemplo n.º 8
0
 public static extern IntPtr AddBoxShape(IntPtr b, float w, float h, Vector2 center, float angle, ShapeDef p);
Exemplo n.º 9
0
 public static extern IntPtr AddPolygonShape(IntPtr b, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] Vector2[] v, int len, ShapeDef p);
Exemplo n.º 10
0
    // Use this for initialization
    protected override void Init()
    {
        API.SetGravity(world,Vector2.zero);
        var ground = API.CreateBody(world, new Vector2(0,20), 0, BodyType.STATIC_BODY );
        ShapeDef sp = new ShapeDef(0);
        sp.restitution = 0.4f;

        API.AddEdgeShape(ground, new Vector2(-20,-20), new Vector2(-20,20), sp);
        API.AddEdgeShape(ground, new Vector2(20,-20), new Vector2(20,20), sp);
        API.AddEdgeShape(ground, new Vector2(-20,20), new Vector2(20,20), sp);
        API.AddEdgeShape(ground, new Vector2(-20,-20), new Vector2(20,-20), sp);

        BodyDef bd = new BodyDef(BodyType.DYNAMIC_BODY);
        bd.allowSleep = false;
        bd.angle = 3.14159265359f;
        bd.angularDamping = 5.0f;
        bd.linearDamping = 0.1f;
        bd.position = new Vector2(0,2);
        m_body = API.CreateBody(world,bd);

        sp.restitution = 0.0f;
        sp.density = 4.0f;

        b2Transform xf1 = new b2Transform();
        xf1.q = new b2Rot(0.3524f * Mathf.PI);
        xf1.p = xf1.q.GetXAxis();

        Vector2[] vertices = new Vector2[3];
        vertices[0] = b2Math.b2Mul(xf1, new Vector2(-1.0f, 0.0f));
        vertices[1] = b2Math.b2Mul(xf1, new Vector2(1.0f, 0.0f));
        vertices[2] = b2Math.b2Mul(xf1, new Vector2(0.0f, 0.5f));

        API.AddPolygonShape(m_body, vertices, vertices.Length, sp);

        sp.density = 2.0f;
        xf1.q = new b2Rot(-0.3524f * Mathf.PI);
        xf1.p = -xf1.q.GetXAxis();

        vertices[0] = b2Math.b2Mul(xf1, new Vector2(-1.0f, 0.0f));
        vertices[1] = b2Math.b2Mul(xf1, new Vector2(1.0f, 0.0f));
        vertices[2] = b2Math.b2Mul(xf1, new Vector2(0.0f, 0.5f));

        API.AddPolygonShape(m_body, vertices, vertices.Length, sp);

        sp.density = 1.0f;
        sp.friction = 0.3f;
        float gravity = 10;

        FrictionJointDef jd = new FrictionJointDef();
        for(int i=0; i<10; ++i)
        {
            var b = API.CreateBody(world, new Vector2(0,5.0f+1.54f*i), 0, BodyType.DYNAMIC_BODY );
            API.AddBoxShape(b, 0.5f, 0.5f, Vector2.zero, 0, sp);
            float I = API.GetInertia(b);
            float mass = API.GetMass(b);
            float radius = Mathf.Sqrt(2.0f*I/mass);

            jd.bodyA = ground;
            jd.bodyB = b;
            jd.collideConnected = true;
            jd.maxForce = mass*gravity;
            jd.maxTorque = mass*radius*gravity;

            API.CreateFrictionJoint(world,jd);
        }
    }
Exemplo n.º 11
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);
    }
Exemplo n.º 12
0
 protected virtual IntPtr Create( ShapeDef def )
 {
     return IntPtr.Zero;
 }
Exemplo n.º 13
0
 public static extern IntPtr AddEdgeShape(IntPtr b, Vector2 p0, Vector2 p1, ShapeDef p );
Exemplo n.º 14
0
 public static extern IntPtr AddBoxShape(IntPtr b, float w, float h, Vector2 center, float angle, ShapeDef p );
Exemplo n.º 15
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);
        }
    }
Exemplo n.º 16
0
 // Use this for initialization
 protected override IntPtr Create( ShapeDef def )
 {
     Vector3 p00 = p0 + transform.position;
     Vector3 p11 = p1 + transform.position;
     return API.AddEdgeShape(body.body, new Vector2(p00.x, p00.y), new Vector2(p11.x,p11.y), def);
 }
Exemplo n.º 17
0
    // Use this for initialization
    protected override IntPtr Create( ShapeDef def )
    {
        scale = transform.localScale;

        return API.AddBoxShape(body.body, width*scale.x, height*scale.y, new Vector2(center.x,center.y), angle, def);
    }
Exemplo n.º 18
0
 public static extern IntPtr AddCircleShape(IntPtr b, float r, ShapeDef p );
Exemplo n.º 19
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);
    }
Exemplo n.º 20
0
 public static extern IntPtr AddPolygonShape( IntPtr b, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] Vector2[] v, int len, ShapeDef p );
Exemplo n.º 21
0
 // Use this for initialization
 protected override IntPtr Create( ShapeDef def )
 {
     scale = transform.localScale.x;
     return API.AddCircleShape(body.body, radius*scale, def);
 }