Beispiel #1
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();
            }
        }
Beispiel #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);
            }
        }