public bool RemoveSensor(MySensor sensor) { //if (sensor.Inserted == false) //{ // return false; //} //m_SensorsToRemove.Add(sensor); //return true; bool result = false; if (m_SensorsToAdd.ContainsKey(sensor.GUID)) { m_SensorsToAdd.Remove(sensor.GUID); result = true; } else { if (!m_SensorsToRemove.ContainsKey(sensor.GUID)) { m_SensorsToRemove.Add(sensor.GUID, sensor); result = true; } } return(result); }
public void HandleSensorChanges() { foreach (var sensorToRemoveKVP in m_SensorsToRemove) { MySensor sensorToRemove = sensorToRemoveKVP.Value; ClearSensor(sensorToRemove); m_Sensors.Remove(sensorToRemove); } m_SensorsToRemove.Clear(); foreach (var sensorToAddKVP in m_SensorsToAdd) { MySensor sensorToAdd = sensorToAddKVP.Value; // insert to sort into bp MySensorElement elem = sensorToAdd.GetElement(); elem.UpdateAABB(); MyPhysics.physicsSystem.GetRigidBodyModule().GetBroadphase().CreateVolume(elem); sensorToAdd.Inserted = true; m_Sensors.Add(sensorToAdd); } m_SensorsToAdd.Clear(); }
public bool RemoveSensor(MySensor sensor) { //if (sensor.Inserted == false) //{ // return false; //} //m_SensorsToRemove.Add(sensor); //return true; bool result = false; if (m_SensorsToAdd.ContainsKey(sensor.GUID)) { m_SensorsToAdd.Remove(sensor.GUID); result = true; } else { if (!m_SensorsToRemove.ContainsKey(sensor.GUID)) { m_SensorsToRemove.Add(sensor.GUID, sensor); result = true; } } return result; }
public bool AddSensor(MySensor sensor) { //if (sensor.Inserted) //{ // return false; //} //m_SensorsToAdd.Add(sensor); //return true; bool result = false; if (m_SensorsToRemove.ContainsKey(sensor.GUID)) { m_SensorsToRemove.Remove(sensor.GUID); sensor.Active = true; sensor.PrepareSensorInteractions(); result = true; } else { if (!m_SensorsToAdd.ContainsKey(sensor.GUID)) { m_SensorsToAdd.Add(sensor.GUID, sensor); result = true; } } return result; }
public bool AddSensor(MySensor sensor) { //if (sensor.Inserted) //{ // return false; //} //m_SensorsToAdd.Add(sensor); //return true; bool result = false; if (m_SensorsToRemove.ContainsKey(sensor.GUID)) { m_SensorsToRemove.Remove(sensor.GUID); sensor.Active = true; sensor.PrepareSensorInteractions(); result = true; } else { if (!m_SensorsToAdd.ContainsKey(sensor.GUID)) { m_SensorsToAdd.Add(sensor.GUID, sensor); result = true; } } return(result); }
public void DestroySensor(MySensor sensor) { if (sensor.Inserted) { MyPhysics.physicsSystem.GetSensorModule().RemoveSensor(sensor); } m_SensorsPool.Deallocate(sensor); }
/// <summary> /// When a sensor moves its needs to update its volume in broadphase /// </summary> public void MoveSensor(MySensor sensor, bool activateSensor) { if (sensor.Inserted) { MySensorElement el = sensor.GetElement(); MyPhysics.physicsSystem.GetRigidBodyModule().GetBroadphase().MoveVolume(el); sensor.Active = true; } }
/// <summary> /// Called when rigid body enters sensor. /// </summary> /// <param name="rbo">Rigid body that entered.</param> public void OnEnter(MySensor sensor, MyRigidBody rbo, MyRBElement rbElement) { //smallship var userData = rbo.m_UserData; var physicsBody = userData as MyPhysicsBody; if (physicsBody != null && physicsBody.Entity is MySmallShip) { m_counter++; if (m_counter > 0) m_owner.OrderToOpen(); } }
public MyEntityDetector(bool isSlowDetector = false) : base() { m_detectedEntities = new Dictionary<MyEntity, int>(); m_observableEntities = new List<MyEntity>(); m_sensor = new MySensor(); m_detectionCriterias = new List<IMyEntityDetectorCriterium>(); m_onEntityMarkForClose = new Action<MyEntity>(OnMarkForCloseHandler); m_onEntityClose = new Action<MyEntity>(OnCloseHandler); m_onEntityPositionChange = new EventHandler(OnDetectedEntityPositionChange); m_isSlowDetector = isSlowDetector; //InitCriterias(detectionCriterias); }
/// <summary> /// Called when rigid body leaves sensor. /// </summary> /// <param name="rbo">Rigid body that left.</param> public void OnLeave(MySensor sensor, MyRigidBody rbo, MyRBElement rbElement) { ////TODO: Temporary solution - there must not be rbo==null, fix the error and change to assert //if (rbo == null) // return; Debug.Assert(rbo != null); //smallship var userData = rbo.m_UserData; var physicsBody = userData as MyPhysicsBody; if (physicsBody != null && physicsBody.Entity is MySmallShip) { m_counter--; if (m_counter <= 0) m_owner.OrderToClose(); } }
public MySensor CreateSensor(MySensorDesc desc) { if (!desc.IsValid()) { // invalid desc MyCommonDebugUtils.AssertDebug(false); return(null); } MySensor sensor = m_SensorsPool.Allocate(); MyCommonDebugUtils.AssertDebug(sensor != null); sensor.LoadFromDesc(desc); return(sensor); }
private void ClearSensor(MySensor sensor) { MySensorElement elem = sensor.GetElement(); elem.UpdateAABB(); MyPhysics.physicsSystem.GetRigidBodyModule().GetBroadphase().DestroyVolume(elem); foreach (var siKvp in sensor.m_Interactions) { MyPhysics.physicsSystem.GetSensorInteractionModule().RemoveSensorInteraction(siKvp.Value); } sensor.m_Interactions.Clear(); sensor.Active = false; sensor.Inserted = false; if (sensor.IsMarkedForClose()) { sensor.Close(); } }
/// <summary> /// Checks for new interactions to raise enter event /// </summary> public void CheckInteractions(float dt) { m_checkInteractionsActive = true; PrepareSafetySensorInteractionIterator(); foreach (var si in m_SafetySensorInteractionIterator) { MySensor sensor = si.m_SensorElement.Sensor; if (!sensor.m_Interactions.ContainsKey(si.m_Guid)) { if (si.m_IsInside) { // new enter event sensor.m_Interactions.Add(si.m_Guid, si); sensor.GetHandler().OnEnter(si.m_SensorElement.Sensor, si.m_RBElement.GetRigidBody(), si.m_RBElement); } else { RemoveSensorInteraction(si); } } } m_checkInteractionsActive = false; }
public override void Close() { Debug.Assert(!m_isClosed); m_sensor.MarkForClose(); SetOff(false); //ClearDetectedEntities(false); //RemoveSensor(); //m_isOn = false; m_detectionCriterias.Clear(); m_sensor = null; OnEntityEnter = null; OnEntityLeave = null; OnEntityPositionChange = null; base.Close(); m_isClosed = true; }
public void OnLeave(MySensor sensor, MyRigidBody rbo, MyRBElement rbElement) { if (rbo == null) return; if (m_isOn && rbo.m_UserData != null) { MyEntity entity = (rbo.m_UserData as MyPhysicsBody).Entity; if (entity != null && (Parent == null || Parent != entity)) { RemoveEntityFromDetectedAndObservable(entity); } } }
public void OnEnter(MySensor sensor, MyRigidBody rbo, MyRBElement rbElement) { if(m_isOn && rbo.m_UserData != null) { MyEntity entity = (rbo.m_UserData as MyPhysicsBody).Entity; if(entity != null && (Parent == null || Parent != entity)) { int meetCriterias = 0; bool canRegisteForClose = false; if (IsEntityMeetCritarias(entity, ref meetCriterias)) { AddDetectedEntity(entity, meetCriterias); canRegisteForClose = true; } else { if (m_containsCriteriumToReCheck) { if (CanBeEntityObserved(entity)) { m_observableEntities.Add(entity); canRegisteForClose = true; } } } if (canRegisteForClose) { RegisterOnCloseHandlers(entity); } } } }
public void Close() { m_Sensor = null; }
public override void Close() { MyGuiScreenGamePlay.OnGameLoaded -= m_onGameLoaded; if (m_sensor != null) { //m_sensor.GetElement().ProxyData = MyElement.PROXY_UNASSIGNED; m_sensor.MarkForClose(); MyPhysics.physicsSystem.GetSensorModule().RemoveSensor(m_sensor); m_sensor = null; } foreach (var part in Parts) { if (part != null && part.EntityId.HasValue) { MyEntities.Remove(part); } } base.Close(); }
protected override void InitPrefab(string displayName, Vector3 relativePosition, Matrix localOrientation, MyMwcObjectBuilder_PrefabBase objectBuilder, MyPrefabConfiguration prefabConfig) { MyPrefabConfigurationKinematic prefabKinematicConfig = (MyPrefabConfigurationKinematic)prefabConfig; MyMwcObjectBuilder_PrefabKinematic kinematicBuilder = objectBuilder as MyMwcObjectBuilder_PrefabKinematic; MyModel model = MyModels.GetModelOnlyDummies(m_config.ModelLod0Enum); for (int i = 0; i < prefabKinematicConfig.KinematicParts.Count; i++) { MyPrefabConfigurationKinematicPart kinematicPart = prefabKinematicConfig.KinematicParts[i]; MyModelDummy open, close; if (model.Dummies.TryGetValue(kinematicPart.m_open, out open) && model.Dummies.TryGetValue(kinematicPart.m_close, out close)) { float? kinematicPartHealth = kinematicBuilder.KinematicPartsHealth[i]; float? kinematicPartMaxHealth = kinematicBuilder.KinematicPartsMaxHealth[i]; uint? kinematicPartEntityId = kinematicBuilder.KinematicPartsEntityId[i]; // if health is not set or not destroyed, then create part if (kinematicPartHealth == null || kinematicPartHealth != 0) { MyPrefabKinematicPart newPart = new MyPrefabKinematicPart(m_owner); if (kinematicPartEntityId.HasValue) { newPart.EntityId = new MyEntityIdentifier(kinematicPartEntityId.Value); } Parts[i] = newPart; newPart.Init(this, kinematicPart, prefabKinematicConfig.m_openTime, prefabKinematicConfig.m_closeTime, (MyModelsEnum)kinematicPart.m_modelMovingEnum, open.Matrix, close.Matrix, prefabKinematicConfig.MaterialType, prefabKinematicConfig.m_soundLooping, prefabKinematicConfig.m_soundOpening, prefabKinematicConfig.m_soundClosing/*, m_groupMask*/, kinematicPartHealth, kinematicPartMaxHealth, Activated); } } } //make handler m_sensorHandler = new MyPrefabKinematicSensor(this); MySphereSensorElement sensorEl = new MySphereSensorElement(); sensorEl.Radius = DETECT_RADIUS; sensorEl.LocalPosition = new Vector3(0, 0, 0); sensorEl.DetectRigidBodyTypes = MyConstants.RIGIDBODY_TYPE_SHIP; sensorEl.SpecialDetectingAngle = DETECTION_ANGLE; MySensorDesc senDesc = new MySensorDesc(); senDesc.m_Element = sensorEl; senDesc.m_Matrix = WorldMatrix; senDesc.m_SensorEventHandler = m_sensorHandler; m_sensor = new MySensor(); m_sensor.LoadFromDesc(senDesc); MyPhysics.physicsSystem.GetSensorModule().AddSensor(m_sensor); GetOwner().UpdateAABB(); UseProperties = new MyUseProperties(MyUseType.FromHUB | MyUseType.Solo, MyUseType.FromHUB); if (kinematicBuilder.UseProperties == null) { UseProperties.Init(MyUseType.FromHUB, MyUseType.FromHUB, 3, 4000, false); } else { UseProperties.Init(kinematicBuilder.UseProperties); } UpdateHudAndCloseStatus(); }
public void DestroySensor(MySensor sensor) { if(sensor.Inserted) { MyPhysics.physicsSystem.GetSensorModule().RemoveSensor(sensor); } m_SensorsPool.Deallocate(sensor); }
/// <summary> /// parses all active rigids, updates the aabbs and checks for possible collisions using the DAABB /// </summary> public override void DoWork() { MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("ClearInteractions"); ClearInteractions(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); MyRBInteractionModule module = MyPhysics.physicsSystem.GetRBInteractionModule(); HashSet <MyRigidBody> activeRigids = MyPhysics.physicsSystem.GetRigidBodyModule().GetActiveRigids(); float dt = MyPhysics.physicsSystem.GetRigidBodyModule().CurrentTimeStep; BoundingBox aabb; //Dictionary<string, int> typeStats = new Dictionary<string, int>(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("MoveProxy"); // A.B. this might be expensive, maybe update separate or move somewhere else like in the solve -> update positions !! foreach (MyRigidBody rbo in activeRigids) { /* * string ts = ((MinerWars.AppCode.Game.Physics.MyPhysicsBody)rbo.m_UserData).Entity.GetType().Name.ToString(); * if (!typeStats.ContainsKey(ts)) * typeStats.Add(ts, 0); * typeStats[ts]++; */ for (int j = 0; j < rbo.GetRBElementList().Count; j++) { MyRBElement el = rbo.GetRBElementList()[j]; el.UpdateAABB(); aabb = el.GetWorldSpaceAABB(); m_DAABBTree.MoveProxy(el.ProxyData, ref aabb, el.GetRigidBody().LinearVelocity *dt); } } MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("make the AABB test"); // make the AABB test MyRBElementInteraction interaction = null; #if RENDER_PROFILING && !MEMORY_PROFILING int[] heights = new int[activeRigids.Count]; int[] tests = new int[activeRigids.Count]; #endif int i = 0; foreach (MyRigidBody rbo in activeRigids) { for (int j = 0; j < rbo.GetRBElementList().Count; j++) { MyRBElement el = rbo.GetRBElementList()[j]; Vector3 globalPosition = Vector3.Transform(el.LocalPosition, rbo.Matrix); Vector3 deltaVelocity = rbo.LinearVelocity * dt; aabb = el.GetWorldSpaceAABB(); if (rbo.ReadFlag(RigidBodyFlag.RBF_COLDET_THROUGH_VOXEL_TRIANGLES) || el is MyRBSphereElement) //because sphere is interpolated for whole path { Vector3 v = globalPosition + rbo.LinearVelocity * dt; //Vector3 v = aabb.GetCenter()+rbo.LinearVelocity * dt; aabb = aabb.Include(ref v); } else { aabb.Max += deltaVelocity; aabb.Min += deltaVelocity; } //if (el is MyRBSphereElement) //{ //MyDebugDraw.AddDrawSphereWireframe(new BoundingSphere(aabb.GetCenter(), (aabb.GetCorners()[0] - aabb.GetCenter()).Length())); // MyDebugDraw.AddDrawSphereWireframe(new BoundingSphere(aabb.GetCenter()+rbo.LinearVelocity * dt, (aabb.GetCorners()[0] - aabb.GetCenter()).Length())); //} MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("m_DAABBTree.OverlapAllBoundingBox"); #if RENDER_PROFILING && !MEMORY_PROFILING m_DAABBTree.OverlapAllBoundingBox(ref aabb, m_overlapElementList, 0); #else m_DAABBTree.OverlapAllBoundingBox(ref aabb, m_overlapElementList, 0); #endif MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("Interactions"); foreach (var lEl in m_overlapElementList) { if (el == lEl)//optimization? { continue; } if ((lEl.Flags & MyElementFlag.EF_SENSOR_ELEMENT) > 0) { MySensorElement sensorElement = lEl as MySensorElement; MyRBElement rbElement = el as MyRBElement; MyPhysics.physicsSystem.GetSensorInteractionModule().AddSensorInteraction(sensorElement, rbElement); continue; } if ((lEl.Flags & MyElementFlag.EF_RB_ELEMENT) > 0) { MyRBElement testEl = (MyRBElement)lEl; if (el.GetRigidBody().IsStatic() && testEl.GetRigidBody().IsStatic()) { continue; } if (el.GetRigidBody().IsKinematic() && testEl.GetRigidBody().IsKinematic()) { continue; } if (el.GetRigidBody().IsKinematic() && testEl.GetRigidBody().IsStatic()) { continue; } if (el.GetRigidBody().IsStatic() && testEl.GetRigidBody().IsKinematic()) { continue; } if (el.GetRigidBody() == testEl.GetRigidBody()) { continue; } if (!MyFiltering.AcceptCollision(el, testEl)) { continue; } interaction = module.FindRBElementInteraction(el, testEl); if (interaction == null) { interaction = module.AddRBElementInteraction(el, testEl); } if (interaction != null) { bool iinserted = false; for (int t = 0; t < m_InteractionList.Count; t++) { if (m_InteractionList[t] == interaction) { iinserted = true; break; } } if (!iinserted) { m_InteractionList.Add(interaction); } } } } MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); } i++; } MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().ProfileCustomValue("Active rigids", activeRigids.Count); #if RENDER_PROFILING && !MEMORY_PROFILING float averageHeight = 0; float averageTest = 0; int maxHeight = 0; int maxTest = 0; for (int j = 0; j < activeRigids.Count; j++) { averageHeight += heights[j]; averageTest += tests[j]; if (maxHeight < heights[j]) { maxHeight = heights[j]; } if (maxTest < tests[j]) { maxTest = tests[j]; } } averageHeight /= activeRigids.Count; averageTest /= activeRigids.Count; MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().ProfileCustomValue("Average height", averageHeight); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().ProfileCustomValue("Average test", averageTest); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().ProfileCustomValue("Max height", maxHeight); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().ProfileCustomValue("Max test", maxTest); #endif MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("handle active sensors"); List <MySensor> activeSensors = MyPhysics.physicsSystem.GetSensorModule().ActiveSensors; if (activeSensors.Count > 0) { if (m_activeSensorIndex >= activeSensors.Count) { m_activeSensorIndex = 0; } MySensor activeSensor = activeSensors[m_activeSensorIndex]; activeSensor.PrepareSensorInteractions(); MySensorElement sensorElement = activeSensor.GetElement(); BoundingBox sensorElAABB = sensorElement.GetWorldSpaceAABB(); m_sensorInteractonList.Clear(); m_DAABBTree.OverlapAllBoundingBox(ref sensorElAABB, m_sensorInteractonList, (uint)MyElementFlag.EF_RB_ELEMENT); foreach (MyRBElement rbElement in m_sensorInteractonList) { MyPhysics.physicsSystem.GetSensorInteractionModule().AddSensorInteraction(sensorElement, rbElement); } activeSensor.Active = false; m_activeSensorIndex++; } //List<MySensor> activeSensors = MyPhysics.physicsSystem.GetSensorModule().ActiveSensors; //for (int i = activeSensors.Count - 1; i >= 0; i--) //{ // MySensorElement sensorElement = activeSensors[i].GetElement(); // BoundingBox sensorElAABB = sensorElement.GetWorldSpaceAABB(); // m_sensorInteractonList.Clear(); // m_DAABBTree.OverlapRBAllBoundingBox(ref sensorElAABB, m_sensorInteractonList); // foreach (MyRBElement rbElement in m_sensorInteractonList) // { // MyPhysics.physicsSystem.GetSensorInteractionModule().AddSensorInteraction(sensorElement, rbElement); // } // activeSensors[i].IsActive = false; // activeSensors.RemoveAt(i); //} MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); }