示例#1
0
        public void Start()
        {
            if (this.body != null)
            {
                return;
            }

            var rigid = this.GetComponent <Rigidbody2D>();

            if (rigid != null)
            {
                if (rigid.bodyType == RigidbodyType2D.Static)
                {
                    this._bodyDef.BodyType = BodyType.StaticBody;
                }
                if (rigid.bodyType == RigidbodyType2D.Kinematic)
                {
                    this._bodyDef.BodyType = BodyType.KinematicBody;
                }
                if (rigid.bodyType == RigidbodyType2D.Dynamic)
                {
                    this._bodyDef.BodyType = BodyType.DynamicBody;
                }
                this.useAutoMass      = rigid.useAutoMass;
                this.inertia          = rigid.inertia;
                this.mass             = rigid.mass;
                this.GravityScale     = rigid.gravityScale;
                this.drag             = rigid.drag;
                this.angularDrag      = rigid.angularDrag;
                this._massData.Center = rigid.centerOfMass.ToVector2();



                Object.Destroy(rigid);
            }

            World world = PhysicsFight.instance.World;

            //body = BodyFactory.CreateRectangle(FSWorldComponent.PhysicsWorld, 1f, 1f, Density);
            body = world.CreateBody(this._bodyDef);

            var colliders = this.GetComponents <UnityEngine.Collider2D>();

            for (int i = 0; i < colliders.Length; i++)
            {
                if (colliders[i] is BoxCollider2D)
                {
                    var     shape = FSHelper.GetBoxShape(colliders[i] as BoxCollider2D);
                    Fixture f     = body.CreateFixture(shape, colliders[i].density);
                    this.UpdateFixture(f, colliders[i]);
                    f.Friction    = colliders[i].friction;
                    f.Restitution = colliders[i].bounciness;
                    f.IsSensor    = colliders[i].isTrigger;
                }
                else if (colliders[i] is CircleCollider2D)
                {
                    var     shape = FSHelper.GetCircleShape(colliders[i] as CircleCollider2D);
                    Fixture f     = body.CreateFixture(shape, colliders[i].density);
                    this.UpdateFixture(f, colliders[i]);
                    f.Friction    = colliders[i].friction;
                    f.Restitution = colliders[i].bounciness;
                    f.IsSensor    = colliders[i].isTrigger;
                }
                else if (colliders[i] is PolygonCollider2D)
                {
                    var shapes = FSHelper.GetPolygonShape(colliders[i] as PolygonCollider2D);
                    for (int j = 0; j < shapes.Length; j++)
                    {
                        Fixture f = body.CreateFixture(shapes[j], colliders[i].density);
                        this.UpdateFixture(f, colliders[i]);
                        f.Friction    = colliders[i].friction;
                        f.Restitution = colliders[i].bounciness;
                        f.IsSensor    = colliders[i].isTrigger;
                    }
                }
            }



            body.IgnoreGravity = !this._useGravity;

            body.CollisionCategories = this._collisionCategories;
            body.CollidesWith        = this._collidesWith;
            //body.IsBullet = true;

            body.SetTransform(new System.Numerics.Vector2(transform.position.x, transform.position.y), transform.rotation.eulerAngles.z * Mathf.Deg2Rad);
            body.Scale = transform.lossyScale.ToVector2();

            body.UseAutoMass = this._useAutoMass;
            body.SetMassData(_massData);
            body.LinearVelocity  = this._velocity;
            body.AngularVelocity = this._angularVelocity;
            body.IsEnabled       = true;
            body.IsBullet        = false;


            PhysicsFight.instance.AddBody(body, this.gameObject);
        }