コード例 #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
        /**在编辑器更改碰撞器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);
                    }
                }
            }
        }