public void Clone(IWorld iWorld, bool doChecksum) { World world = (World)iWorld; Reset(); if (doChecksum) { checksum = ChecksumExtractor.GetEncodedChecksum(); } clonedPhysics.Clear(); foreach (RigidBody rb in world.RigidBodies) { RigidBodyClone rigidBodyClone = poolRigidBodyClone.GetNew(); rigidBodyClone.Clone(rb); clonedPhysics.Add(rb.GetInstance(), rigidBodyClone); } clonedArbiters.Clear(); foreach (Arbiter arb in world.ArbiterMap.Arbiters) { ArbiterClone arbiterClone = poolArbiterClone.GetNew(); arbiterClone.Clone(arb); clonedArbiters.Add(arbiterClone); } clonedArbitersTrigger.Clear(); foreach (Arbiter arb in world.ArbiterTriggerMap.Arbiters) { ArbiterClone arbiterClone = poolArbiterClone.GetNew(); arbiterClone.Clone(arb); clonedArbitersTrigger.Add(arbiterClone); } collisionIslands.Clear(); for (index = 0, length = world.islands.Count; index < length; index++) { CollisionIsland ci = world.islands[index]; CollisionIslandClone collisionIslandClone = poolCollisionIslandClone.GetNew(); collisionIslandClone.Clone(ci); collisionIslands.Add(collisionIslandClone); } cloneCollision.Clone((CollisionSystemPersistentSAP)world.CollisionSystem); clonedInitialCollisions.Clear(); clonedInitialCollisions.AddRange(world.initialCollisions); clonedInitialTriggers.Clear(); clonedInitialTriggers.AddRange(world.initialTriggers); rigidBodyInstanceCount = RigidBody.instanceCount; constraintInstanceCount = Constraint.instanceCount; }
public void Restore(IWorld iWorld) { World world = (World)iWorld; List <RigidBody> bodiesToRemove = new List <RigidBody>(); foreach (RigidBody rb in world.RigidBodies) { if (!clonedPhysics.ContainsKey(rb.GetInstance())) { bodiesToRemove.Add(rb); } } for (index = 0, length = bodiesToRemove.Count; index < length; index++) { RigidBody rb = bodiesToRemove[index]; world.RemoveBody(rb); } foreach (RigidBody rb in world.RigidBodies) { if (clonedPhysics.ContainsKey(rb.GetInstance())) { RigidBodyClone rbClone = clonedPhysics[rb.GetInstance()]; rbClone.Restore(world, rb); rb.island = null; rb.arbiters.Clear(); rb.arbitersTrigger.Clear(); } } foreach (Arbiter arb in world.ArbiterMap.Arbiters) { for (index = 0, length = arb.contactList.Count; index < length; index++) { Contact c = arb.contactList[index]; contactsToGiveBack.Add(c); } arbiterToGiveBack.Add(arb); } world.ArbiterMap.Clear(); foreach (Arbiter arb in world.ArbiterTriggerMap.Arbiters) { foreach (Contact c in arb.contactList) { contactsToGiveBack.Add(c); } arbiterToGiveBack.Add(arb); } world.ArbiterTriggerMap.Clear(); for (index = 0, length = world.islands.islands.Count; index < length; index++) { CollisionIsland ci = world.islands.islands[index]; collisionIslandToGiveBack.Add(ci); } for (index = 0, length = clonedArbiters.Count; index < length; index++) { ArbiterClone arbC = clonedArbiters[index]; Arbiter arbiter = Arbiter.Pool.GetNew(); arbC.Restore(arbiter); arbiter.body1.arbiters.Add(arbiter); arbiter.body2.arbiters.Add(arbiter); world.ArbiterMap.lookUpKey.body1 = arbiter.body1; world.ArbiterMap.lookUpKey.body2 = arbiter.body2; world.ArbiterMap.Add(world.ArbiterMap.lookUpKey.GetHashCode(), arbiter); } for (index = 0, length = clonedArbitersTrigger.Count; index < length; index++) { ArbiterClone arbC = clonedArbitersTrigger[index]; Arbiter arbiter = Arbiter.Pool.GetNew(); arbC.Restore(arbiter); arbiter.body1.arbitersTrigger.Add(arbiter); arbiter.body2.arbitersTrigger.Add(arbiter); world.ArbiterTriggerMap.lookUpKey.body1 = arbiter.body1; world.ArbiterTriggerMap.lookUpKey.body2 = arbiter.body2; world.ArbiterTriggerMap.Add(world.ArbiterTriggerMap.lookUpKey.GetHashCode(), arbiter); } world.islands.islands.Clear(); for (index = 0, length = collisionIslands.Count; index < length; index++) { CollisionIslandClone ci = collisionIslands[index]; CollisionIsland collisionIsland = IslandManager.Pool.GetNew(); ci.Restore(collisionIsland, world); world.islands.islands.Add(collisionIsland); } cloneCollision.Restore((CollisionSystemPersistentSAP)world.CollisionSystem); world.initialCollisions.Clear(); world.initialCollisions.AddRange(clonedInitialCollisions); world.initialTriggers.Clear(); world.initialTriggers.AddRange(clonedInitialTriggers); RigidBody.instanceCount = rigidBodyInstanceCount; Constraint.instanceCount = constraintInstanceCount; for (index = 0, length = contactsToGiveBack.Count; index < length; index++) { Contact obj = contactsToGiveBack[index]; Contact.Pool.GiveBack(obj); } contactsToGiveBack.Clear(); for (index = 0, length = arbiterToGiveBack.Count; index < length; index++) { Arbiter obj = arbiterToGiveBack[index]; Arbiter.Pool.GiveBack(obj); } arbiterToGiveBack.Clear(); for (index = 0, length = collisionIslandToGiveBack.Count; index < length; index++) { CollisionIsland obj = collisionIslandToGiveBack[index]; IslandManager.Pool.GiveBack(obj); } collisionIslandToGiveBack.Clear(); }