Exemplo n.º 1
0
        private void CreateBody(Physics2D.World world)
        {
            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);

            if (tsMaterial == null)
            {
                tsMaterial = GetComponent <FPMaterial>();
            }

            Physics2D.Shape shape = Shape;
            if (shape != null)
            {
                CreateBodyFixture(body, shape);
            }
            else
            {
                Physics2D.Shape[] shapes = CreateShapes();
                for (int index = 0, length = shapes.Length; index < length; index++)
                {
                    CreateBodyFixture(body, shapes[index]);
                }
            }

            if (FPRigidBody == null)
            {
                body.BodyType = Physics2D.BodyType.Static;
            }
            else
            {
                if (FPRigidBody.isKinematic)
                {
                    body.BodyType = KBEngine.Physics2D.BodyType.Kinematic;
                }
                else
                {
                    body.BodyType      = Physics2D.BodyType.Dynamic;
                    body.IgnoreGravity = !FPRigidBody.useGravity;
                }

                if (FPRigidBody.mass <= 0)
                {
                    FPRigidBody.mass = 1;
                }

                body.FixedRotation = FPRigidBody.freezeZAxis;
                body.Mass          = FPRigidBody.mass;
                body.TSLinearDrag  = FPRigidBody.drag;
                body.TSAngularDrag = FPRigidBody.angularDrag;
            }

            body.IsSensor     = isTrigger;
            body.CollidesWith = Physics2D.Category.All;

            _body = body;
        }
        public void Clone(IWorld iWorld, bool doChecksum)
        {
            Physics2D.World world = (Physics2D.World)iWorld;

            Reset();
            //whitebai 2017.5.9
            //函数在动态库内,直接就干掉了,应该没啥用
            if (doChecksum)
            {
                //    checksum = ChecksumExtractor.GetEncodedChecksum();
            }

            clonedPhysics.Clear();
            for (index = 0, length = world.BodyList.Count; index < length; index++)
            {
                Physics2D.Body b = world.BodyList[index];

                BodyClone2D cloneB = poolRigidBodyClone.GetNew();
                cloneB.Clone(b);

                clonedPhysics.Add(b.BodyId, cloneB);
            }

            contactsClone.Clear();

            for (index = 0, length = world.ContactList.Count; index < length; index++)
            {
                Physics2D.Contact c = world.ContactList[index];

                ContactClone2D cClone = poolContactClone.GetNew();
                cClone.Clone(c);

                contactsClone.Add(cClone);
            }

            islandClone.Clone(world.Island);
            toiClone.Clone(world._input);
            dynamicTreeClone.Clone((Physics2D.DynamicTreeBroadPhase)world.ContactManager.BroadPhase);

            this._worldHasNewFixture = world._worldHasNewFixture;

            bodyCounter    = Physics2D.Body._bodyIdCounter;
            fixtureCounter = Physics2D.Fixture._fixtureIdCounter;
        }
Exemplo n.º 3
0
        // Use this for initialization
        public void Init()
        {
            ChecksumExtractor.Init(this);

            Settings.ContinuousPhysics = SpeculativeContacts;

            world = new SyncFrame.Physics2D.World(new TSVector2(Gravity.x, Gravity.y));
            ContactManager.physicsManager = this;

            world.BodyRemoved += OnRemovedRigidBody;
            world.ContactManager.BeginContact += CollisionEnter;
            world.ContactManager.StayContact  += CollisionStay;
            world.ContactManager.EndContact   += CollisionExit;

            gameObjectMap = new Dictionary <IBody, GameObject>();
            collisionInfo = new Dictionary <Physics2D.Body, Dictionary <Physics2D.Body, TSCollision2D> >();

            instance = this;
            AddRigidBodies();
        }
Exemplo n.º 4
0
        private static object OverlapGeneric(Physics2D.Shape shape, FPVector2 position, Physics2D.BodySpecialSensor sensorType, int layerMask)
        {
            Physics2D.World world = (Physics2D.World)Physics2DWorldManager.instance.GetWorld();

            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
            body.CreateFixture(shape);

            body.BodyType     = Physics2D.BodyType.Static;
            body.IsSensor     = true;
            body.CollidesWith = Physics2D.Category.All;

            body.SpecialSensor     = sensorType;
            body.SpecialSensorMask = layerMask;
            body.Position          = position;

            world.RemoveBody(body);
            world.ProcessRemovedBodies();

            if (body._specialSensorResults.Count > 0)
            {
                if (sensorType == Physics2D.BodySpecialSensor.ActiveOnce)
                {
                    return(Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[0]).GetComponent <FPCollider2D>());
                }
                else
                {
                    FPCollider2D[] result = new FPCollider2D[body._specialSensorResults.Count];
                    for (int i = 0; i < body._specialSensorResults.Count; i++)
                    {
                        result[i] = Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[i]).GetComponent <FPCollider2D>();
                    }

                    return(result);
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public void Restore(IWorld iWorld)
        {
            Physics2D.World world = (Physics2D.World)iWorld;

            bodiesToRemove.Clear();
            for (index = 0, length = world.BodyList.Count; index < length; index++)
            {
                Physics2D.Body rb = world.BodyList[index];

                if (!clonedPhysics.ContainsKey(rb.BodyId))
                {
                    bodiesToRemove.Add(rb);
                }
            }

            for (index = 0, length = bodiesToRemove.Count; index < length; index++)
            {
                Physics2D.Body rb = bodiesToRemove[index];

                world.RemoveBody(rb);
            }

            world.ProcessRemovedBodies();

            for (index = 0, length = world.BodyList.Count; index < length; index++)
            {
                Physics2D.Body rb = world.BodyList[index];

                if (clonedPhysics.ContainsKey(rb.BodyId))
                {
                    BodyClone2D rbClone = clonedPhysics[rb.BodyId];
                    rbClone.Restore(rb);
                }
            }

            for (index = 0, length = world.ContactList.Count; index < length; index++)
            {
                Physics2D.Contact c = world.ContactList[index];

                world._contactPool.Enqueue(c);
            }

            world.ContactList.Clear();

            contactDic.Clear();
            for (index = 0, length = contactsClone.Count; index < length; index++)
            {
                ContactClone2D cClone = contactsClone[index];

                Physics2D.Contact c = null;
                if (world._contactPool.Count > 0)
                {
                    c = world._contactPool.Dequeue();
                }
                else
                {
                    c = new Physics2D.Contact();
                }

                cClone.Restore(c);

                contactDic.Add(c.Key, c);

                world.ContactList.Add(c);
            }

            contactEdgeDic.Clear();
            for (index = 0, length = contactsClone.Count; index < length; index++)
            {
                ContactClone2D cClone = contactsClone[index];

                contactDic[cClone.Key]._nodeA = cClone._nodeA.Restore(false, contactDic, contactEdgeDic);
                contactDic[cClone.Key]._nodeB = cClone._nodeB.Restore(false, contactDic, contactEdgeDic);
            }

            for (index = 0, length = contactsClone.Count; index < length; index++)
            {
                ContactClone2D cClone = contactsClone[index];

                contactDic[cClone.Key]._nodeA = cClone._nodeA.Restore(true, contactDic, contactEdgeDic);
                contactDic[cClone.Key]._nodeB = cClone._nodeB.Restore(true, contactDic, contactEdgeDic);
            }

            for (index = 0, length = world.BodyList.Count; index < length; index++)
            {
                Physics2D.Body rb = world.BodyList[index];

                if (clonedPhysics.ContainsKey(rb.BodyId))
                {
                    BodyClone2D rbClone = clonedPhysics[rb.BodyId];

                    if (rbClone.contactEdgeClone != null)
                    {
                        rbClone.contactEdgeClone.Restore(false, contactDic, contactEdgeDic);
                    }
                    else
                    {
                        rb.ContactList = null;
                    }
                }
            }

            for (index = 0, length = world.BodyList.Count; index < length; index++)
            {
                Physics2D.Body rb = world.BodyList[index];

                if (clonedPhysics.ContainsKey(rb.BodyId))
                {
                    BodyClone2D rbClone = clonedPhysics[rb.BodyId];

                    if (rbClone.contactEdgeClone != null)
                    {
                        rb.ContactList = rbClone.contactEdgeClone.Restore(true, contactDic, contactEdgeDic);
                    }
                }
            }

            islandClone.Restore(world.Island, contactDic);
            toiClone.Restore(world._input);


            TreeNode <FixtureProxy>[] treeNodes = ((DynamicTreeBroadPhase)world.ContactManager.BroadPhase)._tree._nodes;
            for (index = 0, length = treeNodes.Length; index < length; index++)
            {
                TreeNode <FixtureProxy> tn = treeNodes[index];

                poolTreeFixtureProxy.GiveBack(tn);
            }

            dynamicTreeClone.Restore((DynamicTreeBroadPhase)world.ContactManager.BroadPhase);

            world._worldHasNewFixture = this._worldHasNewFixture;

            Body._bodyIdCounter       = bodyCounter;
            Fixture._fixtureIdCounter = fixtureCounter;
        }
Exemplo n.º 6
0
 /**
  *  @brief Initializes Shape and RigidBody and sets initial values to position and orientation based on Unity's transform.
  **/
 public void Initialize(Physics2D.World world)
 {
     CreateBody(world);
 }