Пример #1
0
        public void Set(b2Shape shape, int index)
        {
            switch (shape.ShapeType)
            {
            case b2ShapeType.e_circle:
            {
                b2CircleShape circle = (b2CircleShape)shape;
                m_buffer[0] = circle.Position;
                m_vertices  = m_buffer;
                m_count     = 1;
                m_radius    = circle.Radius;
            }
            break;

            case b2ShapeType.e_polygon:
            {
                b2PolygonShape polygon = (b2PolygonShape)shape;
                m_vertices = polygon.Vertices;
                m_count    = polygon.VertexCount;
                m_radius   = polygon.Radius;
            }
            break;

            case b2ShapeType.e_chain:
            {
                b2ChainShape chain = (b2ChainShape)shape;
                Debug.Assert(0 <= index && index < chain.Count);

                m_buffer[0] = chain.Vertices[index];
                if (index + 1 < chain.Count)
                {
                    m_buffer[1] = chain.Vertices[index + 1];
                }
                else
                {
                    m_buffer[1] = chain.Vertices[0];
                }

                m_vertices = m_buffer;
                m_count    = 2;
                m_radius   = chain.Radius;
            }
            break;

            case b2ShapeType.e_edge:
            {
                b2EdgeShape edge = (b2EdgeShape)shape;
                m_buffer[0] = edge.Vertex1;
                m_buffer[1] = edge.Vertex2;
                m_vertices  = m_buffer;
                m_count     = 2;
                m_radius    = edge.Radius;
            }
            break;

            default:
                Debug.Assert(false);
                break;
            }
        }
Пример #2
0
        public PolyShapes()
        {
            // Ground body
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2Vec2[] vertices = new b2Vec2[3];
                vertices[0].Set(-0.5f, 0.0f);
                vertices[1].Set(0.5f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);
                m_polygons[0] = new b2PolygonShape();
                m_polygons[0].Set(vertices, 3);
            }

            {
                b2Vec2[] vertices = new b2Vec2[3];
                vertices[0].Set(-0.1f, 0.0f);
                vertices[1].Set(0.1f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);
                m_polygons[1] = new b2PolygonShape();
                m_polygons[1].Set(vertices, 3);
            }

            {
                float w = 1.0f;
                float b = w / (2.0f + b2Math.b2Sqrt(2.0f));
                float s = b2Math.b2Sqrt(2.0f) * b;

                b2Vec2[] vertices = new b2Vec2[8];
                vertices[0].Set(0.5f * s, 0.0f);
                vertices[1].Set(0.5f * w, b);
                vertices[2].Set(0.5f * w, b + s);
                vertices[3].Set(0.5f * s, w);
                vertices[4].Set(-0.5f * s, w);
                vertices[5].Set(-0.5f * w, b + s);
                vertices[6].Set(-0.5f * w, b);
                vertices[7].Set(-0.5f * s, 0.0f);

                m_polygons[2] = new b2PolygonShape();
                m_polygons[2].Set(vertices, 8);
            }

            {
                m_polygons[3] = new b2PolygonShape();
                m_polygons[3].SetAsBox(0.5f, 0.5f);
            }

            {
                m_circle.Radius = 0.5f;
            }

            m_bodyIndex = 0;
        }
Пример #3
0
        void SetPhysicsToSquare(Square square)
        {
            // Define the dynamic body.
            //Set up a 1m squared box in the physics world
            b2BodyDef def = new b2BodyDef();

            def.position = new b2Vec2(square.Position.X / PTM_RATIO, square.Position.Y / PTM_RATIO);
            def.type     = b2BodyType.b2_dynamicBody;
            b2Body body = _world.CreateBody(def);
            // Define another box shape for our dynamic body.
            var dynamicBox = new b2PolygonShape();

            dynamicBox.SetAsBox(0.5f, 0.5f); //These are mid points for our 1m box

            // Define the dynamic body fixture.
            b2FixtureDef fd = new b2FixtureDef();

            fd.shape    = dynamicBox;
            fd.density  = 1f;
            fd.friction = 0.3f;
            b2Fixture fixture = body.CreateFixture(fd);

            square.PhysicsBody = body;
            //_world.SetContactListener(new Myb2Listener());

            // _world.Dump();
        }
Пример #4
0
        void MakeBox(b2Vec2 pos, float w, float h)
        {
            // Define the dynamic body. We set its position and call the body factory.
            b2BodyDef bodyDef = new b2BodyDef();

            bodyDef.type     = b2BodyType.b2_dynamicBody;
            bodyDef.position = pos;
            var body = world.CreateBody(bodyDef);

            // Define another box shape for our dynamic body.
            b2PolygonShape dynamicBox = new b2PolygonShape();

            dynamicBox.SetAsBox(w, h);

            // Define the dynamic body fixture.
            b2FixtureDef fixtureDef = new b2FixtureDef();

            fixtureDef.shape = dynamicBox;

            // Set the box density to be non-zero, so it will be dynamic.
            fixtureDef.density = 1.0f;

            // Override the default friction.
            fixtureDef.friction = 0.3f;

            // Add the shape to the body.
            body.CreateFixture(fixtureDef);
        }
Пример #5
0
 private void scaleShape(b2Shape shape, bool isOnlyCenter = false)
 {
     if (shape is b2PolygonShape)
     {
         b2PolygonShape poly     = shape as b2PolygonShape;
         List <b2Vec2>  vertices = poly.GetVertices();
         for (int i = 0; i < vertices.Count; i++)
         {
             vertices[i].x *= transform.lossyScale.x;
             vertices[i].y *= transform.lossyScale.y;
         }
     }
     else if (shape is b2CircleShape)
     {
         b2CircleShape circle = shape as b2CircleShape;
         if (!isOnlyCenter)
         {
             float scale = Mathf.Max(transform.lossyScale.x, transform.lossyScale.y);
             circle.SetRadius(circle.GetRadius() * scale);
         }
         var offset = circle.GetLocalPosition();
         offset.x *= transform.lossyScale.x;
         offset.y *= transform.lossyScale.y;
         circle.SetLocalPosition(offset);
     }
 }
 /// Compute the collision manifold between an edge and a circle.
 public static void b2CollideEdgeAndPolygon(ref b2Manifold manifold,
                                 b2EdgeShape edgeA, ref b2Transform xfA,
                                 b2PolygonShape polygonB, ref b2Transform xfB)
 {
     b2EPCollider b = new b2EPCollider();
     b.Collide(ref manifold, edgeA, ref xfA, polygonB, ref xfB);
 }
        public static float b2EdgeSeparation(b2PolygonShape poly1, b2Transform xf1, int edge1,
                                      b2PolygonShape poly2, b2Transform xf2)
        {
            b2Vec2[] vertices1 = poly1.Vertices;
            b2Vec2[] normals1 = poly1.Normals;

            int count2 = poly2.VertexCount;
            b2Vec2[] vertices2 = poly2.Vertices;

            // Convert normal from poly1's frame into poly2's frame.
            b2Vec2 normal1World = b2Math.b2Mul(xf1.q, normals1[edge1]);
            b2Vec2 normal1 = b2Math.b2MulT(xf2.q, normal1World);

            // Find support vertex on poly2 for -normal.
            int index = 0;
            float minDot = b2Settings.b2_maxFloat;

            for (int i = 0; i < count2; ++i)
            {
                float dot = b2Math.b2Dot(ref vertices2[i], ref normal1);
                if (dot < minDot)
                {
                    minDot = dot;
                    index = i;
                }
            }

            b2Vec2 v1 = b2Math.b2Mul(xf1, vertices1[edge1]);
            b2Vec2 v2 = b2Math.b2Mul(xf2, vertices2[index]);
            float separation = b2Math.b2Dot(v2 - v1, normal1World);
            return separation;
        }
Пример #8
0
        public void DrawFixture(b2Fixture fixture)
        {
            b2Color     color = new b2Color(0.95f, 0.95f, 0.6f);
            b2Transform xf    = fixture.Body.Transform;

            switch (fixture.ShapeType)
            {
            case b2ShapeType.e_circle:
            {
                b2CircleShape circle = (b2CircleShape)fixture.Shape;

                b2Vec2 center = b2Math.b2Mul(xf, circle.Position);
                float  radius = circle.Radius;

                m_debugDraw.DrawCircle(center, radius, color);
            }
            break;

            case b2ShapeType.e_polygon:
            {
                b2PolygonShape poly        = (b2PolygonShape)fixture.Shape;
                int            vertexCount = poly.VertexCount;
                Debug.Assert(vertexCount <= b2Settings.b2_maxPolygonVertices);
                b2Vec2[] vertices = new b2Vec2[b2Settings.b2_maxPolygonVertices];

                for (int i = 0; i < vertexCount; ++i)
                {
                    vertices[i] = b2Math.b2Mul(xf, poly.Vertices[i]);
                }

                m_debugDraw.DrawPolygon(vertices, vertexCount, color);
            }
            break;
            }
        }
Пример #9
0
        public ShapeEditing()
        {
            {
                b2BodyDef bd1    = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd1);

                b2EdgeShape shape1 = new b2EdgeShape();
                shape1.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape1, 0.0f);
            }

            b2BodyDef bd = new b2BodyDef();

            bd.type = b2BodyType.b2_dynamicBody;
            bd.position.Set(0.0f, 10.0f);
            m_body = m_world.CreateBody(bd);

            b2PolygonShape shape = new b2PolygonShape();

            shape.SetAsBox(4.0f, 4.0f, new b2Vec2(0.0f, 0.0f), 0.0f);
            m_fixture1 = m_body.CreateFixture(shape, 10.0f);

            m_fixture2 = null;

            m_sensor = false;
        }
Пример #10
0
    private void testQueryShape_poly()
    {
        Vector3 mousePos        = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        b2WorldQueryCallback cb = delegate(b2Fixture fixture) {
            b2Body body = fixture.GetBody();
            body.SetAwake(true);
            return(true);
        };

        b2Vec2[] vertices = new b2Vec2[4];
        vertices [0] = new b2Vec2(-100.0f / ptm_ratio, 0.0f / ptm_ratio);
        vertices [1] = new b2Vec2(0.0f / ptm_ratio, 100.0f / ptm_ratio);
        vertices [2] = new b2Vec2(100.0f / ptm_ratio, 0.0f / ptm_ratio);
        vertices [3] = new b2Vec2(0.0f / ptm_ratio, -200.0f / ptm_ratio);
        b2PolygonShape shape = b2PolygonShape.AsArray(vertices, vertices.Length);

        b2Transform transform = new b2Transform(new b2Vec2(mousePos.x, mousePos.y), b2Mat22.FromAngle(0));

        _world.QueryShape(cb, shape, transform);

        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i].x += mousePos.x;
            vertices[i].y += mousePos.y;
        }
        b2Color color = new b2Color(1.0f, 0.0f, 0.0f);

        _debugDraw.DrawPolygon(vertices, vertices.Length, color);
    }
Пример #11
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override b2Shape CreatePhysicsShape()
        {
            var shape = new b2PolygonShape();

            shape.SetAsBox(PhysicWidth / 2, PhysicHeight / 2);
            return(shape);
        }
Пример #12
0
 public static void b2CollidePolygons(b2Manifold manifold, b2PolygonShape polygonA, b2Transform xfA, b2PolygonShape polygonB, b2Transform xfB)
 {
     Box2DPINVOKE.b2CollidePolygons(b2Manifold.getCPtr(manifold), b2PolygonShape.getCPtr(polygonA), b2Transform.getCPtr(xfA), b2PolygonShape.getCPtr(polygonB), b2Transform.getCPtr(xfB));
     if (Box2DPINVOKE.SWIGPendingException.Pending)
     {
         throw Box2DPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Пример #13
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override b2Shape CreatePhysicsShape()
        {
            var shape = new b2PolygonShape();

            shape.SetAsBox(PhysicWidth / 2f, PhysicHeight / 2f);
            GroundSprite.DrawRect(new CCRect(0, 0, Width, Height), CCColor4B.Black);
            return(shape);
        }
Пример #14
0
        private void createWithCollider2d(Collider2D coll)
        {
            b2FixtureDef      fixtureDef = new b2FixtureDef();
            PhysicsMaterial2D material   = coll.sharedMaterial;

            if (material != null)
            {
                fixtureDef.restitution = material.bounciness;
                fixtureDef.friction    = material.friction;
            }
            fixtureDef.isSensor = coll.isTrigger;

            if (coll is BoxCollider2D)
            {
                BoxCollider2D  boxColl = coll as BoxCollider2D;
                b2PolygonShape s       = b2PolygonShape.AsOrientedBox(boxColl.size.x * 0.5f,
                                                                      boxColl.size.y * 0.5f,
                                                                      new b2Vec2(boxColl.offset.x, boxColl.offset.y),
                                                                      0 /*transform.eulerAngles.z*Mathf.Deg2Rad*/);
                scaleShape(s);
                fixtureDef.shape   = s;
                _fixtureDict[coll] = new b2Fixture[] { _body.CreateFixture(fixtureDef) };
            }
            else if (coll is CircleCollider2D)
            {
                CircleCollider2D circleColl = coll as CircleCollider2D;
                b2CircleShape    s          = new b2CircleShape(circleColl.radius);
                s.SetLocalPosition(new b2Vec2(circleColl.offset.x, circleColl.offset.y));
                scaleShape(s);
                fixtureDef.shape   = s;
                _fixtureDict[coll] = new b2Fixture[] { _body.CreateFixture(fixtureDef) };
            }
            else if (coll is PolygonCollider2D)
            {
                int i, j;
                PolygonCollider2D polyColl = coll as PolygonCollider2D;

                List <b2Fixture> fixtureList = new List <b2Fixture>();
                int pathCount = polyColl.pathCount;
                for (i = 0; i < pathCount; i++)
                {
                    Vector2[] path     = polyColl.GetPath(i);
                    b2Vec2[]  vertices = new b2Vec2[path.Length];
                    for (j = 0; j < path.Length; j++)
                    {
                        vertices[j] = new b2Vec2(path[j].x, path[j].y);
                    }
                    b2Separator sep      = new b2Separator();
                    b2Fixture[] fixtures = sep.Separate(_body, fixtureDef, vertices, 100, polyColl.offset.x, polyColl.offset.y);             //必须放大100倍进行计算
                    for (j = 0; j < fixtures.Length; j++)
                    {
                        scaleShape(fixtures[j].GetShape());
                    }
                    fixtureList.AddRange(fixtures);
                }
                _fixtureDict[coll] = fixtureList.ToArray();
            }
        }
Пример #15
0
    /// Initialize the proxy using the given shape. The shape
    /// must remain in scope while the proxy is in use.
    public void Set(b2Shape shape, int index)
    {
        switch (shape.GetType())
        {
        case b2Shape.Type.e_circle:
        {
            b2CircleShape circle = (b2CircleShape)shape;
            m_vertices = new [] { circle.m_p };
            m_count    = 1;
            m_radius   = circle.m_radius;
        }
        break;

        case b2Shape.Type.e_polygon:
        {
            b2PolygonShape polygon = (b2PolygonShape)shape;
            m_vertices = (b2Vec2[])polygon.m_vertices.Clone();
            m_count    = polygon.m_count;
            m_radius   = polygon.m_radius;
        }
        break;

        case b2Shape.Type.e_chain:
        {
            b2ChainShape chain = (b2ChainShape)shape;
            Debug.Assert(0 <= index && index < chain.m_count);

            m_buffer[0] = chain.m_vertices[index];
            if (index + 1 < chain.m_count)
            {
                m_buffer[1] = chain.m_vertices[index + 1];
            }
            else
            {
                m_buffer[1] = chain.m_vertices[0];
            }

            m_vertices = m_buffer;
            m_count    = 2;
            m_radius   = chain.m_radius;
        }
        break;

        case b2Shape.Type.e_edge:
        {
            b2EdgeShape edge = (b2EdgeShape)shape;
            m_vertices = new [] { edge.m_vertex1, edge.m_vertex2 };
            m_count    = 2;
            m_radius   = edge.m_radius;
        }
        break;

        default:
            Debug.Assert(false);
            break;
        }
    }
Пример #16
0
    /// Implement b2Shape.
    public override b2Shape Clone()
    {
        b2PolygonShape clone = new b2PolygonShape();

        clone.m_centroid = m_centroid;
        clone.m_count    = m_count;
        clone.m_vertices = (b2Vec2[])m_vertices.Clone();
        clone.m_normals  = (b2Vec2[])m_normals.Clone();
        return(clone);
    }
Пример #17
0
        public ContinuousTest()
        {
            {
                b2BodyDef bd = new b2BodyDef();
                bd.position.Set(0.0f, 0.0f);
                b2Body body = m_world.CreateBody(bd);

                b2EdgeShape edge = new b2EdgeShape();

                edge.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f));
                body.CreateFixture(edge, 0.0f);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.2f, 1.0f, new b2Vec2(0.5f, 1.0f), 0.0f);
                body.CreateFixture(shape, 0.0f);
            }

#if true
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 20.0f);
                //bd.angle = 0.1f;

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(2.0f, 0.1f);

                m_body = m_world.CreateBody(bd);
                m_body.CreateFixture(shape, 1.0f);

                m_angularVelocity = Rand.RandomFloat(-50.0f, 50.0f);
                //m_angularVelocity = 46.661274f;
                m_body.LinearVelocity  = new b2Vec2(0.0f, -100.0f);
                m_body.AngularVelocity = m_angularVelocity;
            }
#else
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 2.0f);
                b2Body body = m_world.CreateBody(bd);

                b2CircleShape shape = new b2CircleShape();
                shape.Position = b2Vec2.Zero;
                shape.Radius   = 0.5f;
                body.CreateFixture(shape, 1.0f);

                bd.bullet = true;
                bd.position.Set(0.0f, 10.0f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);
                body.LinearVelocity = new b2Vec2(0.0f, -100.0f);
            }
#endif
        }
Пример #18
0
        void CreatePlatform()
        {
            // Define the dynamic body.
            var bodyDef = new b2BodyDef();

            bodyDef.type = b2BodyType.b2_staticBody;             //or you could use b2_staticBody

            bodyDef.position.Set(initialLocation.X / Constants.PTM_RATIO, initialLocation.Y / Constants.PTM_RATIO);

            var shape = new b2PolygonShape();

            var num = 4;

            b2Vec2[] vertices =
            {
                new b2Vec2(-102.0f / Constants.PTM_RATIO, -49.5f / Constants.PTM_RATIO),
                new b2Vec2(-113.0f / Constants.PTM_RATIO, -81.5f / Constants.PTM_RATIO),
                new b2Vec2(113.0f / Constants.PTM_RATIO,  -84.5f / Constants.PTM_RATIO),
                new b2Vec2(106.0f / Constants.PTM_RATIO, -47.5f / Constants.PTM_RATIO)
            };

            shape.Set(vertices, num);


            // Define the dynamic body fixture.
            var fixtureDef = new b2FixtureDef();

            fixtureDef.shape       = shape;
            fixtureDef.density     = 1.0f;
            fixtureDef.friction    = 0.3f;
            fixtureDef.restitution = 0.1f;

            CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);

            //CONTINUING TO ADD BODY SHAPE....

            // THIS IS THE Sling base....

            //row 1, col 1
            var num2 = 4;

            b2Vec2[] vertices2 =
            {
                new b2Vec2(41.0f / Constants.PTM_RATIO,  -6.5f / Constants.PTM_RATIO),
                new b2Vec2(35.0f / Constants.PTM_RATIO, -57.5f / Constants.PTM_RATIO),
                new b2Vec2(57.0f / Constants.PTM_RATIO, -65.5f / Constants.PTM_RATIO),
                new b2Vec2(49.0f / Constants.PTM_RATIO, -7.5f / Constants.PTM_RATIO)
            };

            shape.Set(vertices2, num2);
            fixtureDef.shape = shape;
            body.CreateFixture(fixtureDef);
        }
        public void createShapeWithDictionary(PlistDictionary dict, PlistArray shapePoints, b2Body body, CCNode node, LHScene scene, CCPoint scale)
        {
            _shapeID   = dict ["shapeID"].AsInt;
            _shapeName = dict ["name"].AsString;

            int flipx = scale.X < 0 ? -1 : 1;
            int flipy = scale.Y < 0 ? -1 : 1;


            for (int f = 0; f < shapePoints.Count; ++f)
            {
                PlistArray fixPoints = shapePoints [f].AsArray;
                int        count     = fixPoints.Count;
                if (count > 2)
                {
                    b2Vec2[]       verts    = new b2Vec2[count];
                    b2PolygonShape shapeDef = new b2PolygonShape();

                    int i = 0;
                    for (int j = count - 1; j >= 0; --j)
                    {
                        int idx = (flipx < 0 && flipy >= 0) || (flipx >= 0 && flipy < 0) ? count - i - 1 : i;

                        String  pointStr = fixPoints [j].AsString;
                        CCPoint point    = CCPoint.Parse(pointStr);

                        point.X *= scale.X;
                        point.Y *= scale.Y;

                        point.Y = -point.Y;

                        b2Vec2 vec = new b2Vec2(point.X, point.Y);

                        verts[idx] = vec;
                        ++i;
                    }

                    if (LHValidateCentroid(verts, count))
                    {
                        shapeDef.Set(verts, count);

                        b2FixtureDef fixture = new b2FixtureDef();

                        LHSetupb2FixtureWithInfo(fixture, dict);

                        fixture.userData = this;
                        fixture.shape    = shapeDef;
                        body.CreateFixture(fixture);
                    }
                }
            }
        }
Пример #20
0
        public Pulleys()
        {
            float y = 16.0f;
            float L = 12.0f;
            float a = 1.0f;
            float b = 2.0f;

            b2Body ground = null;
            {
                b2BodyDef bd = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape edge = new b2EdgeShape();
                edge.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                //ground->CreateFixture(&shape, 0.0f);

                b2CircleShape circle = new b2CircleShape();
                circle.Radius = 2.0f;

                circle.Position = new b2Vec2(-10.0f, y + b + L);
                ground.CreateFixture(circle, 0.0f);

                circle.Position = new b2Vec2(10.0f, y + b + L);
                ground.CreateFixture(circle, 0.0f);
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(a, b);

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;

                //bd.fixedRotation = true;
                bd.position.Set(-10.0f, y);
                b2Body body1 = m_world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0f);

                bd.position.Set(10.0f, y);
                b2Body body2 = m_world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0f);

                b2PulleyJointDef pulleyDef     = new b2PulleyJointDef();
                b2Vec2           anchor1       = new b2Vec2(-10.0f, y + b);
                b2Vec2           anchor2       = new b2Vec2(10.0f, y + b);
                b2Vec2           groundAnchor1 = new b2Vec2(-10.0f, y + b + L);
                b2Vec2           groundAnchor2 = new b2Vec2(10.0f, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f);

                m_joint1 = (b2PulleyJoint)m_world.CreateJoint(pulleyDef);
            }
        }
Пример #21
0
        public PhysicalShapeEditor(Rect frameRect)
        {
            InitializeComponent();
            this.fixtureDef = new b2FixtureDef(IntPtr.Zero);
            this.propertyGrid.SelectedObject = this.fixtureDef;
            this.rect = frameRect;

            this.circleShape             = new b2CircleShape(IntPtr.Zero);
            this.polygonShape            = new b2PolygonShape(IntPtr.Zero);
            this.nBox_Rect_H_meter.Value = (decimal)(Math.Abs(rect.GetHeight()) / Scene.meterPixelRatio);
            this.nBox_Rect_W_meter.Value = (decimal)(Math.Abs(rect.GetWidth()) / Scene.meterPixelRatio);
            CreateMohitCircle();
            this.fixtureDef.شکل = this.circleShape;
        }
Пример #22
0
        //public const int e_columnCount = 1;
        //public const int e_rowCount = 1;

        public VerticalStack()
        {
            {
                b2BodyDef bd     = new b2BodyDef();
                b2Body    ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);

                shape.Set(new b2Vec2(20.0f, 0.0f), new b2Vec2(20.0f, 20.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

            for (int j = 0; j < e_columnCount; ++j)
            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 0.5f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape    = shape;
                fd.density  = 1.0f;
                fd.friction = 0.3f;

                for (int i = 0; i < e_rowCount; ++i)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;

                    int n = j * e_rowCount + i;
                    Debug.Assert(n < e_rowCount * e_columnCount);
                    m_indices[n] = n;
                    bd.userData  = m_indices[n];

                    float x = 0.0f;
                    //float32 x = RandomFloat(-0.02f, 0.02f);
                    //float32 x = i % 2 == 0 ? -0.025f : 0.025f;
                    bd.position.Set(xs[j] + x, 0.752f + 1.54f * i);
                    b2Body body = m_world.CreateBody(bd);

                    m_bodies[n] = body;

                    body.CreateFixture(fd);
                }
            }

            m_bullet = null;
        }
Пример #23
0
        /**在编辑器更改碰撞器Center时*/
        public void onEditShapeCenter(object[] args)
        {
            Collider2D collider = (Collider2D)args [0];
            float      cx       = (float)args [1];
            float      cy       = (float)args [2];
            float      oldCX    = (float)args[3];
            float      oldCY    = (float)args[4];

            b2Fixture[] fixtures = _fixtureDict [collider];
            if (fixtures != null)
            {
                for (int i = 0; i < fixtures.Length; i++)
                {
                    b2Fixture fixture = fixtures[i];
                    b2Shape   s       = fixture.GetShape();
                    if (collider is BoxCollider2D)
                    {
                        b2PolygonShape boxShape = s as b2PolygonShape;
                        BoxCollider2D  boxColl  = collider as BoxCollider2D;
                        boxShape.SetAsOrientedBox(boxColl.size.x * 0.5f, boxColl.size.y * 0.5f, new b2Vec2(cx, cy), 0);
                        scaleShape(boxShape);
                        _body.SetAwake(true);
                    }
                    else if (collider is CircleCollider2D)
                    {
                        b2CircleShape circleShape = s as b2CircleShape;
                        circleShape.SetLocalPosition(new b2Vec2(cx, cy));
                        scaleShape(circleShape, true);
                        _body.SetAwake(true);
                    }
                    else if (collider is PolygonCollider2D)
                    {
                        b2PolygonShape    polyShape = s as b2PolygonShape;
                        PolygonCollider2D polyColl  = collider as PolygonCollider2D;
                        List <b2Vec2>     vertices  = polyShape.GetVertices();
                        for (int j = 0; j < vertices.Count; j++)
                        {
                            b2Vec2 v = vertices[j];
                            v.x -= oldCX;
                            v.y -= oldCY;
                            v.x += cx;
                            v.y += cy;
                        }
                        //scaleShape(polyShape);
                        _body.SetAwake(true);
                    }
                }
            }
        }
Пример #24
0
        public static float b2EdgeSeparation(b2PolygonShape poly1, ref b2Transform xf1, int edge1,
                                             b2PolygonShape poly2, ref b2Transform xf2)
        {
            b2Vec2 normal = poly1.Normals[edge1];

            int count2 = poly2.m_vertexCount;

            b2Vec2[] vertices2 = poly2.Vertices;

            // Convert normal from poly1's frame into poly2's frame.
            float normal1Worldx = xf1.q.c * normal.x - xf1.q.s * normal.y;
            float normal1Worldy = xf1.q.s * normal.x + xf1.q.c * normal.y;

            float normal1x = xf2.q.c * normal1Worldx + xf2.q.s * normal1Worldy;
            float normal1y = -xf2.q.s * normal1Worldx + xf2.q.c * normal1Worldy;

            // Find support vertex on poly2 for -normal.
            int   index  = 0;
            float minDot = b2Settings.b2_maxFloat;

            for (int i = 0; i < count2; ++i)
            {
                var vert = vertices2[i];

                float dot = vert.x * normal1x + vert.y * normal1y;

                if (dot < minDot)
                {
                    minDot = dot;
                    index  = i;
                }
            }

            var v1e1 = poly1.Vertices[edge1];
            var v2i  = vertices2[index];

            float v1x = (xf1.q.c * v1e1.x - xf1.q.s * v1e1.y) + xf1.p.x;
            float v1y = (xf1.q.s * v1e1.x + xf1.q.c * v1e1.y) + xf1.p.y;

            float v2x = (xf2.q.c * v2i.x - xf2.q.s * v2i.y) + xf2.p.x;
            float v2y = (xf2.q.s * v2i.x + xf2.q.c * v2i.y) + xf2.p.y;

            v2x -= v1x;
            v2y -= v1y;

            float separation = v2x * normal1Worldx + v2y * normal1Worldy;

            return(separation);
        }
Пример #25
0
        /**
         * Separates a non-convex polygon into convex polygons and adds them as fixtures to the <code>body</code> parameter.<br/>
         * There are some rules you should follow (otherwise you might get unexpected results) :
         * <ul>
         * <li>This class is specifically for non-convex polygons. If you want to create a convex polygon, you don't need to use this class - Box2D's <code>b2PolygonShape</code> class allows you to create convex shapes with the <code>setAsArray()</code>/<code>setAsVector()</code> method.</li>
         * <li>The vertices must be in clockwise order.</li>
         * <li>No three neighbouring points should lie on the same line segment.</li>
         * <li>There must be no overlapping segments and no "holes".</li>
         * </ul> <p/>
         * @param body The b2Body, in which the new fixtures will be stored.
         * @param fixtureDef A b2FixtureDef, containing all the properties (friction, density, etc.) which the new fixtures will inherit.
         * @param verticesVec The vertices of the non-convex polygon, in clockwise order.
         * @param scale <code>[optional]</code> The scale which you use to draw shapes in Box2D. The bigger the scale, the better the precision. The default value is 30.
         * @see b2PolygonShape
         * @see b2PolygonShape.SetAsArray()
         * @see b2PolygonShape.SetAsVector()
         * @see b2Fixture
         * */

        public b2Fixture[] Separate(b2Body body, b2FixtureDef fixtureDef, b2Vec2[] verticesVec, float scale = 100, float offsetX = 0, float offsetY = 0)
        {
            int            i;
            int            n = verticesVec.Length;
            int            j;
            int            m;
            List <b2Vec2>  vec = new List <b2Vec2>();
            ArrayList      figsVec;
            b2PolygonShape polyShape;

            for (i = 0; i < n; i++)
            {
                vec.Add(new b2Vec2((verticesVec[i].x + offsetX) * scale, (verticesVec[i].y + offsetY) * scale));
            }
            if (!getIsConvexPolygon(vec, vec.Count))
            {
                int validate = Validate(vec);
                if (validate == 2 || validate == 3)
                {
                    vec.Reverse();                    //add by kingBook 2017.2.14
                }
            }
            //Debug.Log(string.Format("Validate:{0}",Validate(vec)));
            figsVec = calcShapes(vec);
            n       = figsVec.Count;



            b2Fixture[] fixtures = new b2Fixture[n];

            for (i = 0; i < n; i++)
            {
                vec = (List <b2Vec2>)figsVec[i];
                vec.Reverse();//add by kingBook 2017.2.14
                m           = vec.Count;
                verticesVec = new b2Vec2[m];
                for (j = 0; j < m; j++)
                {
                    verticesVec[j] = new b2Vec2(vec[j].x / scale, vec[j].y / scale);
                }

                polyShape = new b2PolygonShape();
                polyShape.SetAsArray(verticesVec, m);
                fixtureDef.shape = polyShape;
                fixtures[i]      = body.CreateFixture(fixtureDef);
            }
            return(fixtures);
        }
Пример #26
0
        private void CreateGround()
        {
            // Define the dynamic body.
            var bodyDef = new b2BodyDef();

            bodyDef.type = b2BodyType.b2_staticBody;             //or you could use b2_staticBody

            bodyDef.position.Set(initialLocation.X / Constants.PTM_RATIO, initialLocation.Y / Constants.PTM_RATIO);

            b2PolygonShape shape = new b2PolygonShape();

            int num = 4;

            b2Vec2[] vertices =
            {
                new b2Vec2(-1220.0f / Constants.PTM_RATIO,  54.0f / Constants.PTM_RATIO),
                new b2Vec2(-1220.0f / Constants.PTM_RATIO, -52.0f / Constants.PTM_RATIO),
                new b2Vec2(1019.0f / Constants.PTM_RATIO,  -52.0f / Constants.PTM_RATIO),
                new b2Vec2(1019.0f / Constants.PTM_RATIO, 54.0f / Constants.PTM_RATIO)
            };

            shape.Set(vertices, num);


            // Define the dynamic body fixture.
            var fixtureDef = new b2FixtureDef();

            fixtureDef.shape       = shape;
            fixtureDef.density     = 1.0f;
            fixtureDef.friction    = 1.0f;
            fixtureDef.restitution = 0.1f;

            CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName);

            if (!TheLevel.SharedLevel.IS_RETINA)
            {
                //non retina adjustment
                sprite.ScaleX = 1.05f;
            }
            else
            {
                // retina adjustment

                sprite.ScaleX = 2.05f;
                sprite.ScaleY = 2.0f;
            }
        }
Пример #27
0
        private void checkBounds()
        {
            for (int i = 0; i < liquid.Length; ++i)
            {
                if (liquid[i].WorldCenter.y < -10.0f)
                {
                    m_world.DestroyBody(liquid[i]);
                    float massPerParticle = totalMass / nParticles;

                    var pd = new b2CircleShape();
                    var fd = new b2FixtureDef();
                    fd.shape             = pd;
                    fd.density           = 1.0f;
                    fd.filter.groupIndex = -10;
                    pd.Radius            = .05f;
                    fd.restitution       = 0.4f;
                    fd.friction          = 0.0f;
                    float cx = 0.0f + Rand.RandomFloat(-0.6f, 0.6f);
                    float cy = 15.0f + Rand.RandomFloat(-2.3f, 2.0f);
                    var   bd = new b2BodyDef();
                    bd.position      = new b2Vec2(cx, cy);
                    bd.fixedRotation = true;
                    bd.type          = b2BodyType.b2_dynamicBody;
                    var b = m_world.CreateBody(bd);
                    b.CreateFixture(fd).UserData = LIQUID_INT;
                    var md = new b2MassData();
                    md.mass = massPerParticle;
                    md.I    = 1.0f;
                    b.SetMassData(md);
                    b.SetSleepingAllowed(false);
                    liquid[i] = b;
                }
            }

            if (bod.WorldCenter.y < -15.0f)
            {
                m_world.DestroyBody(bod);
                var polyDef = new b2PolygonShape();
                polyDef.SetAsBox(Rand.RandomFloat(0.3f, 0.7f), Rand.RandomFloat(0.3f, 0.7f));
                var bodyDef = new b2BodyDef();
                bodyDef.position = new b2Vec2(0.0f, 25.0f);
                bodyDef.type     = b2BodyType.b2_dynamicBody;
                bod = m_world.CreateBody(bodyDef);
                bod.CreateFixture(polyDef, 1f);
            }
        }
        public void createRectangleWithDictionary(PlistDictionary dict, b2Body body, CCNode node, LHScene scene, CCSize size)
        {
            _shapeID   = dict ["shapeID"].AsInt;
            _shapeName = dict ["name"].AsString;

            b2PolygonShape shape = new b2PolygonShape();

            shape.SetAsBox(size.Width * 0.5f, size.Height * 0.5f);

            b2FixtureDef fixture = new b2FixtureDef();

            LHSetupb2FixtureWithInfo(fixture, dict);

            fixture.userData = this;
            fixture.shape    = shape;

            body.CreateFixture(fixture);
        }
Пример #29
0
        public override void Step(Settings settings)
        {
            base.Step(settings);

            if (m_count < e_count)
            {
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.125f, 0.125f);
                body.CreateFixture(shape, 1.0f);

                ++m_count;
            }
        }
Пример #30
0
    protected b2Body createBox(float w, float h, float x, float y)
    {
        b2BodyDef bodyDef = new b2BodyDef();

        bodyDef.type = b2Body.b2_dynamicBody;
        bodyDef.position.Set(x / ptm_ratio, y / ptm_ratio);
        b2Body body = _world.CreateBody(bodyDef);

        b2PolygonShape s = new b2PolygonShape();

        s.SetAsBox(w / ptm_ratio * 0.5f, h / ptm_ratio * 0.5f);
        b2FixtureDef fixtrureDef = new b2FixtureDef();

        fixtrureDef.shape = s;
        body.CreateFixture(fixtrureDef);

        return(body);
    }
Пример #31
0
        void MakeBox(b2Vec2 pos, float w, float h)
        {
            // Define the dynamic body. We set its position and call the body factory.
            b2BodyDef bodyDef = new b2BodyDef();
            bodyDef.type = b2BodyType.b2_dynamicBody;
            bodyDef.position = pos;
            var body = world.CreateBody(bodyDef);

            // Define another box shape for our dynamic body.
            b2PolygonShape dynamicBox = new b2PolygonShape();
            dynamicBox.SetAsBox(w, h);

            // Define the dynamic body fixture.
            b2FixtureDef fixtureDef = new b2FixtureDef();
            fixtureDef.shape = dynamicBox;

            // Set the box density to be non-zero, so it will be dynamic.
            fixtureDef.density = 1.0f;

            // Override the default friction.
            fixtureDef.friction = 0.3f;

            // Add the shape to the body.
            body.CreateFixture(fixtureDef);
        }
Пример #32
0
        private void Form1_Load(object sender, EventArgs e)
        {
            sogc = new SimpleOpenGlControl();
            sogc.Dock = DockStyle.Fill;
            sogc.Paint += new PaintEventHandler(sogc_Paint);
            sogc.Resize += new EventHandler(sogc_Resize);
            Controls.Add(sogc);

            sogc.InitializeContexts();
            InitOpenGL(sogc.Size, 1, PointF.Empty);

            // Define the gravity vector.
            b2Vec2 gravity = new b2Vec2(0.0f, -10.0f);

            // Do we want to let bodies sleep?
            bool doSleep = true;

            // Construct a world object, which will hold and simulate the rigid bodies.
            world = new b2World(gravity, doSleep);
            //	world.SetWarmStarting(true);

            {
                b2BodyDef bd = new b2BodyDef();
                b2Body ground = world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsEdge(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                float a = 0.5f;
                b2CircleShape shape = new b2CircleShape();
                shape.m_radius = a;

                b2Vec2 x = new b2Vec2(-7.0f, 0.95f);
                b2Vec2 y;
                b2Vec2 deltaX = new b2Vec2(0, 1.25f);
                b2Vec2 deltaY = new b2Vec2(0, 1.25f);
                y= deltaY;

                for (int j = 0; j < 8; ++j)
                {
                    b2BodyDef bd = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position = y;
                    b2Body body = world.CreateBody(bd);
                    body.CreateFixture(shape, 5.0f);

                    y += deltaY;
                }
            }

            GDIDebugThing.instance.SetFlags(EDebugFlags.e_shapeBit);
            world.SetDebugDraw(GDIDebugThing.instance);

            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 85;
            timer.SynchronizingObject = this;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }