Exemplo n.º 1
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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /**在编辑器更改碰撞器的形状Shape时*/
        private void onEditShape(object[] args)
        {
            Collider2D collider = (Collider2D)args [0];

            b2Fixture[] fixtures = _fixtureDict [collider];
            if (collider is BoxCollider2D)
            {
                b2Fixture      fixture = fixtures[0];
                b2Shape        s       = fixture.GetShape();
                float          sizeX   = (float)args [1];
                float          sizeY   = (float)args [2];
                BoxCollider2D  boxColl = collider as BoxCollider2D;
                b2PolygonShape polygon = s as b2PolygonShape;
                polygon.SetAsOrientedBox(sizeX * 0.5f, sizeY * 0.5f, new b2Vec2(boxColl.offset.x, boxColl.offset.y), 0);
                scaleShape(polygon);
                _body.SetAwake(true);
            }
            else if (collider is PolygonCollider2D)
            {
                Vector2[] points   = (Vector2[])args[1];
                int       len      = points.Length;
                b2Vec2[]  vertices = new b2Vec2[len];
                for (int i = 0; i < len; i++)
                {
                    vertices[i] = new b2Vec2(points[i].x, points[i].y);
                }

                b2FixtureDef fixtureDef = new b2FixtureDef();
                fixtureDef.density     = fixtures[0].GetDensity();
                fixtureDef.friction    = fixtures[0].GetFriction();
                fixtureDef.isSensor    = fixtures[0].IsSensor();
                fixtureDef.restitution = fixtures[0].GetRestitution();

                int j = fixtures.Length;
                while (--j >= 0)
                {
                    _body.DestroyFixture(fixtures[j]);
                }

                b2Separator sep = new b2Separator();
                _fixtureDict [collider] = sep.Separate(_body, fixtureDef, vertices, 1);

                fixtures = _fixtureDict [collider];
                for (j = 0; j < fixtures.Length; j++)
                {
                    scaleShape(fixtures[j].GetShape());
                }

                _body.SetAwake(true);
            }
            else if (collider is CircleCollider2D)
            {
                b2Fixture fixture = fixtures[0];
                float     radius  = (float)args[1];
                float     cx      = (float)args[2];
                float     cy      = (float)args[3];
                b2Shape   s       = fixture.GetShape();

                b2CircleShape circle = s as b2CircleShape;
                circle.SetRadius(radius);
                circle.SetLocalPosition(new b2Vec2(cx, cy));
                scaleShape(circle);
                _body.SetAwake(true);
            }
        }