예제 #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
        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();
            }
        }
예제 #3
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);
                    }
                }
            }
        }
예제 #4
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);
            }
        }