private void DeleteActor(PhysX.RigidActor oldActor, PhysicsShape oldShape, bool wasPhysical, DeleteActorFlags flags) { if (oldActor != null) { if (_touchCounts.IsValueCreated) { SendCollisionEndToContactedObjects(); _touchCounts.Value.Clear(); } _scene.ForEachCharacter((PhysxCharacter character) => { character.InvalidateControllerCacheIfContacting(this); } ); _scene.SceneImpl.RemoveActor(oldActor); oldActor.Dispose(); if ((flags & DeleteActorFlags.DestroyPrimaryMaterial) != 0) { _properties.PhysxMaterial.CheckedDispose(); } if ((flags & DeleteActorFlags.DestroyChildMaterials) != 0) { //also dispose child prim materials foreach (PhysxPrim prim in _childShapes.Keys) { prim._properties.PhysxMaterial.CheckedDispose(); } } //unref our old shape if (oldShape != null) { _scene.MeshingStageImpl.UnrefShape(oldShape, wasPhysical); if ((flags & DeleteActorFlags.UnrefChildShapes) != 0) { //unref child shapes foreach (RelatedShapes childShapes in _childShapes.Values) { _scene.MeshingStageImpl.UnrefShape(childShapes.ChildShape, wasPhysical); } } } //everyone watching us for delete should be informed Action<PhysxPrim> onDel = this.OnDeleted; if (onDel != null) { onDel(this); foreach (Action<PhysxPrim> act in onDel.GetInvocationList()) { onDel -= act; } } if (_primsBeingWatchedForDeletes.IsValueCreated) { foreach (var prim in _primsBeingWatchedForDeletes.Value) { prim.OnDeleted -= new Action<PhysxPrim>(other_OnDeleted); } _primsBeingWatchedForDeletes = new Lazy<List<PhysxPrim>>(); } //no matter if we're unreffing or not, the actual child shape list is now invalid becase //these shapes were part of our old physx actor _childShapes.Clear(); _shapeToPrimIndex.Clear(); _primaryShapes.Clear(); } }
private void AssignActor(PhysX.RigidActor myActor, PhysicsShape newShape, bool isPhysical, DeleteActorFlags flags) { PhysX.RigidActor oldActor = _actor; PhysicsShape oldShape = null; if (newShape != null) { oldShape = _myShape; _myShape = newShape; } _actor = myActor; if (_actor != null && _actor is PhysX.RigidDynamic) { _dynActor = (PhysX.RigidDynamic)_actor; CacheMassData(); } else { _dynActor = null; } bool wasPhysical = _isPhysical; _isPhysical = isPhysical; if (HasActor) _scene.SceneImpl.AddActor(_actor); //add to physx scene this.DeleteActor(oldActor, oldShape, wasPhysical, flags); if (_actor != null) { _actor.UserData = this; this.IndexAndConfigurePrimaryShapes(); } ClearForcesAndSendUpdate(); ChangeGravityIfNeeded(); }