public override void Dispose()
 {
     base.Dispose();
     lock (instanceMutationLock) {
         foreach (PhysicsShapeHandle shapeHandle in activePhysicsShapes.Values.Distinct())
         {
             if (!WorldModelCache.CacheContainsHandle(shapeHandle))
             {
                 shapeHandle.Dispose();
             }
         }
         activePhysicsShapes.Clear();
         currentGameObjects.Values.ForEach(e => e.Dispose());
         currentGameObjects.Clear();
     }
 }
        public override void ResetEntityPhysics(bool fullReset)
        {
            LosgapSystem.InvokeOnMasterAsync(() => {             // Because creating geometryEntity shapes invoke on master and it can create locking deadlocks
                Vector3 physicsShapeOffset;
                lock (instanceMutationLock) {
                    if (fullReset)
                    {
                        foreach (PhysicsShapeHandle shapeHandle in activePhysicsShapes.Values.Distinct())
                        {
                            if (!WorldModelCache.CacheContainsHandle(shapeHandle))
                            {
                                shapeHandle.Dispose();
                            }
                        }
                        activePhysicsShapes.Clear();
                    }
                    List <PhysicsShapeHandle> unusedHandles = activePhysicsShapes.Values.Distinct().ToList();
                    foreach (KeyValuePair <LevelGeometryEntity, GeometryEntity> kvp in currentGeometryEntities)
                    {
                        PhysicsShapeHandle physicsShape = PhysicsShapeHandle.NULL;
                        if (kvp.Key.Geometry is LevelGeometry_Model)
                        {
                            LevelGeometry_Model geomAsModel = (LevelGeometry_Model)kvp.Key.Geometry;
                            var matchingEntity = activePhysicsShapes.Keys.FirstOrDefault(e => e.Geometry is LevelGeometry_Model && ((LevelGeometry_Model)e.Geometry).HasIdenticalPhysicsShape(geomAsModel));
                            if (matchingEntity != null)
                            {
                                physicsShape = activePhysicsShapes[matchingEntity];
                            }
                        }
                        else
                        {
                            if (activePhysicsShapes.ContainsKey(kvp.Key))
                            {
                                physicsShape = activePhysicsShapes[kvp.Key];
                            }
                        }
                        if (physicsShape == PhysicsShapeHandle.NULL)
                        {
                            physicsShape = kvp.Key.Geometry.CreatePhysicsShape(out physicsShapeOffset);
                        }
                        if (physicsShape == PhysicsShapeHandle.NULL)
                        {
                            continue;                                                                  // Some geometry is non-collidable (i.e. planes)
                        }
                        kvp.Value.SetPhysicsShape(physicsShape, Vector3.ZERO, LevelGeometry.GEOMETRY_MASS, forceIntransigence: true);
                        activePhysicsShapes[kvp.Key] = physicsShape;
                        if (!fullReset && unusedHandles.Contains(physicsShape))
                        {
                            unusedHandles.Remove(physicsShape);
                        }
                    }

                    if (!fullReset)
                    {
                        foreach (var unusedHandle in unusedHandles)
                        {
                            activePhysicsShapes.RemoveWhere(kvp => kvp.Value == unusedHandle);
                            if (!WorldModelCache.CacheContainsHandle(unusedHandle))
                            {
                                unusedHandle.Dispose();
                            }
                        }
                    }
                }
            });
        }