Пример #1
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);
     }
 }
Пример #2
0
        public CircleProp(
            b2World b2world,

            double[] size,
            double[] position

            )
        {
            /*
             * static rectangle shaped prop
             *
             *  pars:
             *  size - array [width, height]
             *  position - array [x, y], in world meters, of center
             */
            this.size = size;

            //initialize body
            var bdef = new b2BodyDef();

            bdef.position      = new b2Vec2(position[0], position[1]);
            bdef.angle         = 0;
            bdef.fixedRotation = true;
            this.body          = b2world.CreateBody(bdef);

            //initialize shape

            var fixdef = new b2FixtureDef();

            var shape = new b2CircleShape();

            fixdef.shape = shape;

            shape.SetRadius(this.size[0] / 2);

            fixdef.restitution = 0.4; //positively bouncy!
            this.body.CreateFixture(fixdef);
        }
Пример #3
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);
            }
        }