public static bool GetTarget(Vector3D from, Vector3D to, ref Vector3D target) { ProfilerShort.Begin("GetTarget"); ProfilerShort.Begin("CastRay"); Vector3 normal; var body = MyPhysics.CastRay(from, to, out target, out normal); ProfilerShort.End(); //Profiler.Begin("Iterate"); //int index = 0; //while (index < hits.Count && // (hits[index].Body == null // || hits[index].Body.GetEntity() is Sandbox.Game.Weapons.MyAmmoBase // || hits[index].Body.GetEntity() is Sandbox.Game.Entities.Debris.MyDebrisBase)) //{ // index++; //} //Profiler.End(); // Vector3 targetPoint = to; ProfilerShort.End(); return(body != null); }
private void SpawnCrater() { Vector3 myPosition = MySession.Static.LocalCharacter.WorldMatrix.Translation; Vector3 myDirection = MySession.Static.LocalCharacter.WorldMatrix.Forward; MyPhysics.CastRay(myPosition, myPosition + myDirection * 100); }
private MyStringHash RayCastGround() { MyStringHash walkSurfaceMaterial = new MyStringHash(); float maxDistValue = MyConstants.DEFAULT_GROUND_SEARCH_DISTANCE; var from = m_character.PositionComp.GetPosition() + m_character.PositionComp.WorldMatrix.Up * 0.5; //(needs some small distance from the bottom or the following call to HavokWorld.CastRay will find no hits) var to = from + m_character.PositionComp.WorldMatrix.Down * maxDistValue; MyPhysics.CastRay(from, to, m_hits, MyPhysics.CollisionLayers.CharacterCollisionLayer); // Skips invalid hits (null body, self character) int index = 0; while ((index < m_hits.Count) && ((m_hits[index].HkHitInfo.Body == null) || (m_hits[index].HkHitInfo.GetHitEntity() == Entity.Components))) { index++; } if (m_hits.Count == 0) { m_standingOnGrid = null; } if (index < m_hits.Count) { // We must take only closest hit (others are hidden behind) var h = m_hits[index]; var entity = h.HkHitInfo.GetHitEntity(); var sqDist = Vector3D.DistanceSquared((Vector3D)h.Position, from); if (sqDist < maxDistValue * maxDistValue) { var cubeGrid = entity as MyCubeGrid; var voxelBase = entity as MyVoxelBase; m_standingOnGrid = cubeGrid; if (cubeGrid != null) { walkSurfaceMaterial = cubeGrid.Physics.GetMaterialAt(h.Position); } else if (voxelBase != null && voxelBase.Storage != null && voxelBase.Storage.DataProvider != null) { var materialDefinition = voxelBase.GetMaterialAt(ref h.Position); if (materialDefinition != null) { walkSurfaceMaterial = MyStringHash.GetOrCompute(materialDefinition.MaterialTypeName); } } if (walkSurfaceMaterial.ToString().Length == 0) { walkSurfaceMaterial = MyMaterialType.ROCK; } } } m_hits.Clear(); return(walkSurfaceMaterial); }
private void ChangeTarget() { Vector3D position; Vector3D forward; if ((MySession.Static.GetCameraControllerEnum() != MyCameraControllerEnum.ThirdPersonSpectator) && (MySession.Static.GetCameraControllerEnum() != MyCameraControllerEnum.Entity)) { position = MySector.MainCamera.Position; forward = MySector.MainCamera.WorldMatrix.Forward; } else { MatrixD xd = MySession.Static.ControlledEntity.GetHeadMatrix(true, true, false, false); position = xd.Translation; forward = xd.Forward; } m_tmpHitInfos.Clear(); MyPhysics.CastRay(position, position + (forward * 20.0), m_tmpHitInfos, 0x18); if (m_tmpHitInfos.Count != 0) { using (List <MyPhysics.HitInfo> .Enumerator enumerator = m_tmpHitInfos.GetEnumerator()) { while (enumerator.MoveNext()) { MyAgentBot bot; MyCharacter hitEntity = enumerator.Current.HkHitInfo.GetHitEntity() as MyCharacter; if ((hitEntity != null) && (this.TryGetBotForCharacter(hitEntity, out bot) && bot.BotDefinition.Commandable)) { this.ChangeBotBehavior(bot); } } } } }
private void HandleEntitySelect() { if (MyInput.Static.IsAnyShiftKeyPressed() && MyInput.Static.IsNewLeftMousePressed()) { if (SelectedEntity != null) { ((MyEntity)SelectedEntity).ClearDebugRenderComponents(); SelectedEntity = null; } else { if (MySector.MainCamera != null) { List <MyPhysics.HitInfo> lst = new List <MyPhysics.HitInfo>(); MyPhysics.CastRay(MySector.MainCamera.Position, MySector.MainCamera.Position + MySector.MainCamera.ForwardVector * 100, lst); foreach (var hit in lst) { var body = hit.HkHitInfo.Body; if (body == null || body.Layer == MyPhysics.CollisionLayers.NoCollisionLayer) { continue; } SelectedEntity = hit.HkHitInfo.GetHitEntity(); } } } } }
private void ChangeTarget() { Vector3D cameraPos, cameraDir; if (MySession.GetCameraControllerEnum() == MyCameraControllerEnum.ThirdPersonSpectator || MySession.GetCameraControllerEnum() == MyCameraControllerEnum.Entity) { var headMatrix = MySession.ControlledEntity.GetHeadMatrix(true, true); cameraPos = headMatrix.Translation; cameraDir = headMatrix.Forward; } else { cameraPos = MySector.MainCamera.Position; cameraDir = MySector.MainCamera.WorldMatrix.Forward; } m_tmpHitInfos.Clear(); MyPhysics.CastRay(cameraPos, cameraPos + cameraDir * 20, m_tmpHitInfos, MyPhysics.ObjectDetectionCollisionLayer); if (m_tmpHitInfos.Count == 0) { return; } foreach (var hitInfo in m_tmpHitInfos) { var ent = hitInfo.HkHitInfo.GetHitEntity() as MyCharacter; if (ent != null) { MyAgentBot bot; if (TryGetBotForCharacter(ent, out bot) && bot.BotDefinition.Commandable) { ChangeBotBehavior(bot); } } } }
private MyEntity GetTargetEntity(ref MatrixD ownerWorldHeadMatrix, out Vector3D hitPosition) { hitPosition = Vector3D.MaxValue; if (Owner == null) { return(null); } Vector3D ownerPosition = ownerWorldHeadMatrix.Translation; Vector3 ownerForward = ownerWorldHeadMatrix.Forward; Vector3D rayStart = ownerPosition; Vector3D rayEnd = rayStart + m_manipulationDistance * ownerForward; m_tmpHits.Clear(); MyPhysics.CastRay(rayStart, rayEnd, m_tmpHits, MyPhysics.ObjectDetectionCollisionLayer); foreach (var hitInfo in m_tmpHits) { if (hitInfo.HkHitInfo.GetHitEntity() == Owner) { continue; } else { hitPosition = hitInfo.Position; return(hitInfo.HkHitInfo.GetHitEntity() as MyEntity); } } return(null); }
private void UpdateVelocity() { if (MyInput.Static.IsAnyShiftKeyPressed()) { if (MyInput.Static.IsMousePressed(MyMouseButtonsEnum.Middle)) { IMyEntity entity; MyCamera mainCamera = MySector.MainCamera; List <MyPhysics.HitInfo> toList = new List <MyPhysics.HitInfo>(); MyPhysics.CastRay(base.Position, base.Position + (base.Orientation.Forward * 1000.0), toList, 0); if (toList.Count > 0) { entity = toList[0].HkHitInfo.Body.GetEntity(toList[0].HkHitInfo.GetShapeKey(0)); } else { entity = null; } this.m_velocity = (entity == null) ? Vector3D.Zero : entity.Physics.LinearVelocity; } if (MyInput.Static.IsMousePressed(MyMouseButtonsEnum.Right)) { this.m_velocity = Vector3D.Zero; } if (MyInput.Static.PreviousMouseScrollWheelValue() < MyInput.Static.MouseScrollWheelValue()) { this.m_velocity *= 1.1000000238418579; } else if (MyInput.Static.PreviousMouseScrollWheelValue() > MyInput.Static.MouseScrollWheelValue()) { this.m_velocity /= 1.1000000238418579; } } }
private bool IsThePlayerInSight(MyPlayer player) { if (player.Character == null) { return(false); } if (MySession.Static.LocalCharacter == null) { return(false); } var playerPosition = player.Character.PositionComp.GetPosition(); var localPlayerPosition = MySession.Static.LocalCharacter.PositionComp.GetPosition(); // Is just about 2k meters if (Vector3D.DistanceSquared(playerPosition, localPlayerPosition) > 4000000) { return(false); } m_hitInfoResults.Clear(); MyPhysics.CastRay(playerPosition, localPlayerPosition, m_hitInfoResults); foreach (var hitInfo in m_hitInfoResults) { if (hitInfo.HkHitInfo.GetHitEntity() is MyCharacter) { continue; } return(false); } return(true); }
public static bool GetTarget(Vector3D from, Vector3D to, ref Vector3D target) { ProfilerShort.Begin("GetTarget"); ProfilerShort.Begin("CastRay"); var info = MyPhysics.CastRay(from, to); if (info.HasValue) { target = info.Value.Position; } ProfilerShort.End(); //Profiler.Begin("Iterate"); //int index = 0; //while (index < hits.Count && // (hits[index].Body == null // || hits[index].GetEntity() is Sandbox.Game.Weapons.MyAmmoBase // || hits[index].GetEntity() is Sandbox.Game.Entities.Debris.MyDebrisBase)) //{ // index++; //} //Profiler.End(); // Vector3 targetPoint = to; ProfilerShort.End(); return(info.HasValue); }
private void GetHitEntityAndPosition(LineD line, out IMyEntity entity, out Vector3D hitPosition, out Vector3 hitNormal) { entity = null; hitPosition = hitNormal = Vector3.Zero; VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("MyEntities.GetIntersectionWithLine()"); m_intersection = MyEntities.GetIntersectionWithLine(ref line, m_ignoreEntity, m_weapon, false, false, true, IntersectionFlags.ALL_TRIANGLES, MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS * CHECK_INTERSECTION_INTERVAL); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); if (m_intersection != null) { entity = m_intersection.Value.Entity; hitPosition = m_intersection.Value.IntersectionPointInWorldSpace; hitNormal = m_intersection.Value.NormalInWorldSpace; } if (entity == null) { var hitRigidBody = MyPhysics.CastRay(line.From, line.To, out hitPosition, out hitNormal); entity = hitRigidBody.GetEntity() as MyEntity; } if (entity == null) { return; } if (!(entity is MyCharacter)) { entity = entity.GetTopMostParent(); } }
/// <summary> /// Finds closest object (grid or voxel map) for placement of blocks . /// </summary> public bool FindClosestPlacementObject(out MyCubeGrid closestGrid, out MyVoxelBase closestVoxelMap) { closestGrid = null; closestVoxelMap = null; m_hitInfo = null; if (MySession.Static.ControlledEntity == null) { return(false); } if (PlacementProvider != null) { closestGrid = PlacementProvider.ClosestGrid; closestVoxelMap = PlacementProvider.ClosestVoxelMap; m_hitInfo = PlacementProvider.HitInfo; } else { LineD line = new LineD(IntersectionStart, IntersectionStart + IntersectionDirection * IntersectionDistance); MyPhysics.CastRay(line.From, line.To, m_tmpHitList, MyPhysics.CollisionLayers.ObjectDetectionCollisionLayer); // Remove character hits. m_tmpHitList.RemoveAll(delegate(MyPhysics.HitInfo hitInfo) { return(hitInfo.HkHitInfo.GetHitEntity() == MySession.Static.ControlledEntity.Entity); }); if (m_tmpHitList.Count == 0) { return(false); } var hit = m_tmpHitList[0]; closestGrid = hit.HkHitInfo.GetHitEntity() as MyCubeGrid; if (closestGrid != null) { m_hitInfo = hit; } //if (MyFakes.ENABLE_BLOCK_PLACEMENT_ON_VOXEL) // TODO: check this MyFake to remove or what? //{ closestVoxelMap = hit.HkHitInfo.GetHitEntity() as MyVoxelBase; if (closestVoxelMap != null) { m_hitInfo = hit; } //} } return(closestGrid != null || closestVoxelMap != null); }
private void ComputeSunAngle() { m_angleToSun = Vector3.Dot(Vector3.Transform(m_panelOrientation, m_solarBlock.WorldMatrix.GetOrientation()), MySector.DirectionToSunNormalized); // If the sun is on the backside of the panel, and we're not two-sided, OR the block not functional, just stop processing here if ((m_angleToSun < 0 && !m_isTwoSided) || !m_solarBlock.IsFunctional) { return; } m_currentPivot %= 8; MatrixD rot = m_solarBlock.WorldMatrix.GetOrientation(); float scale = (float)m_solarBlock.WorldMatrix.Forward.Dot(Vector3.Transform(m_panelOrientation, rot)); float unit = m_solarBlock.BlockDefinition.CubeSize == MyCubeSize.Large ? 2.5f : 0.5f; Vector3D pivot = m_solarBlock.WorldMatrix.Translation; pivot += ((m_currentPivot % 4 - 1.5f) * unit * scale * (m_solarBlock.BlockDefinition.Size.X / 4f)) * m_solarBlock.WorldMatrix.Left; pivot += ((m_currentPivot / 4 - 0.5f) * unit * scale * (m_solarBlock.BlockDefinition.Size.Y / 2f)) * m_solarBlock.WorldMatrix.Up; pivot += unit * scale * (m_solarBlock.BlockDefinition.Size.Z / 2f) * Vector3.Transform(m_panelOrientation, rot) * m_panelOffset; LineD l = new LineD(pivot + MySector.DirectionToSunNormalized * 100, pivot + MySector.DirectionToSunNormalized * m_solarBlock.CubeGrid.GridSize / 4); //shadows are drawn only 1000m MyPhysics.CastRay(l.From, l.To, m_hitList, MyPhysics.CollisionLayers.DefaultCollisionLayer); m_isPivotInSun[m_currentPivot] = true; foreach (var hit in m_hitList) { var ent = hit.HkHitInfo.GetHitEntity(); if (ent != m_solarBlock.CubeGrid) { m_isPivotInSun[m_currentPivot] = false; break; } else { var grid = ent as MyCubeGrid; var pos = grid.RayCastBlocks(l.From, l.To); if (pos.HasValue && grid.GetCubeBlock(pos.Value) != m_solarBlock.SlimBlock) { m_isPivotInSun[m_currentPivot] = false; break; } } } m_pivotsInSun = 0; foreach (bool p in m_isPivotInSun) { if (p) { m_pivotsInSun++; } } }
private void TrySpawnBot() { Vector3D position; if ((MySession.Static.GetCameraControllerEnum() != MyCameraControllerEnum.ThirdPersonSpectator) && (MySession.Static.GetCameraControllerEnum() != MyCameraControllerEnum.Entity)) { position = MySector.MainCamera.Position; Vector3D forward = MySector.MainCamera.WorldMatrix.Forward; } else { MatrixD xd = MySession.Static.ControlledEntity.GetHeadMatrix(true, true, false, false); position = xd.Translation; Vector3D forward = xd.Forward; } List <MyPhysics.HitInfo> toList = new List <MyPhysics.HitInfo>(); LineD ed = new LineD(MySector.MainCamera.Position, MySector.MainCamera.Position + (MySector.MainCamera.ForwardVector * 1000f)); MyPhysics.CastRay(ed.From, ed.To, toList, 15); if (toList.Count == 0) { Static.SpawnNewBot(this.BotToSpawn, position, true); } else { MyPhysics.HitInfo?nullable = null; foreach (MyPhysics.HitInfo info in toList) { IMyEntity hitEntity = info.HkHitInfo.GetHitEntity(); if (hitEntity is MyCubeGrid) { nullable = new MyPhysics.HitInfo?(info); } else if (hitEntity is MyVoxelBase) { nullable = new MyPhysics.HitInfo?(info); } else { if (!(hitEntity is MyVoxelPhysics)) { continue; } nullable = new MyPhysics.HitInfo?(info); } break; } Vector3D position = (nullable == null) ? MySector.MainCamera.Position : nullable.Value.Position; Static.SpawnNewBot(this.BotToSpawn, position, true); } }
bool IMyPrefabManager.IsPathClear(Vector3D from, Vector3D to, double halfSize) { Vector3D other = new Vector3D(); other.X = 1; Vector3D forward = to - from; forward.Normalize(); if (Vector3D.Dot(forward, other) > 0.9f || Vector3D.Dot(forward, other) < -0.9f) { other.X = 0; other.Y = 1; } other = Vector3D.Cross(forward, other); other.Normalize(); other = other * halfSize; //first MyPhysics.CastRay(from + other, to + other, m_raycastHits, MyPhysics.ObjectDetectionCollisionLayer); if (m_raycastHits.Count() > 0) { m_raycastHits.Clear(); return(false); } //second other *= -1; MyPhysics.CastRay(from + other, to + other, m_raycastHits, MyPhysics.ObjectDetectionCollisionLayer); if (m_raycastHits.Count() > 0) { m_raycastHits.Clear(); return(false); } //third other = Vector3D.Cross(forward, other); MyPhysics.CastRay(from + other, to + other, m_raycastHits, MyPhysics.ObjectDetectionCollisionLayer); if (m_raycastHits.Count() > 0) { m_raycastHits.Clear(); return(false); } //fourth other *= -1; MyPhysics.CastRay(from + other, to + other, m_raycastHits, MyPhysics.ObjectDetectionCollisionLayer); if (m_raycastHits.Count() > 0) { m_raycastHits.Clear(); return(false); } return(true); }
public static CastHit?GetClosestFootSupportPosition(MyEntity characterEntity, MyEntity characterTool, Vector3 from, Vector3 up, Vector3 footDimension, Matrix WorldMatrix, float castDownLimit, float castUpLimit, uint raycastFilterLayer = 0) { bool flag = false; CastHit hit = new CastHit(); MatrixD matrix = WorldMatrix; Vector3 zero = Vector3.Zero; matrix.Translation = Vector3.Zero; zero = (Vector3)Vector3.Transform(zero, matrix); matrix.Translation = (from + (up * castUpLimit)) + zero; Vector3 vector1 = new Vector3(0f, footDimension.Y / 2f, 0f); Vector3 vector4 = new Vector3(0f, footDimension.Y / 2f, -footDimension.Z); Vector3 worldCoord = from + (up * castUpLimit); Vector3 pointTo = from - (up * castDownLimit); if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW && MyDebugDrawSettings.DEBUG_DRAW_CHARACTER_IK_RAYCASTLINE) { MyRenderProxy.DebugDrawText3D(worldCoord + zero, "Cast line", Color.White, 1f, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, -1, false); MyRenderProxy.DebugDrawLine3D(worldCoord + zero, pointTo + zero, Color.White, Color.White, false, false); } if (MyFakes.ENABLE_FOOT_IK_USE_HAVOK_RAYCAST) { MyPhysics.HitInfo info; if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW && MyDebugDrawSettings.DEBUG_DRAW_CHARACTER_IK_RAYCASTLINE) { MyRenderProxy.DebugDrawText3D(worldCoord, "Raycast line", Color.Green, 1f, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, -1, false); MyRenderProxy.DebugDrawLine3D(worldCoord, pointTo, Color.Green, Color.Green, false, false); } if (MyPhysics.CastRay(worldCoord, pointTo, out info, raycastFilterLayer, true)) { flag = true; if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW && MyDebugDrawSettings.DEBUG_DRAW_CHARACTER_IK_RAYCASTHITS) { MyRenderProxy.DebugDrawSphere(info.Position, 0.02f, Color.Green, 1f, false, false, true, false); MyRenderProxy.DebugDrawText3D(info.Position, "RayCast hit", Color.Green, 1f, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, -1, false); } if (Vector3.Dot((Vector3)info.Position, up) > Vector3.Dot(hit.Position, up)) { hit.Position = (Vector3)info.Position; hit.Normal = info.HkHitInfo.Normal; } } } if (flag) { return(new CastHit?(hit)); } return(null); }
private static void StartWave() { if (!m_currentTarget.HasValue) { return; } var sunDir = GetCorrectedDirection(MySector.DirectionToSunNormalized); SetupDirVectors(sunDir); var waveMeteorCount = MyUtils.GetRandomFloat(Math.Min(2, m_meteorcount - 3), m_meteorcount + 3); var randCircle = MyUtils.GetRandomVector3CircleNormalized(); var rand = MyUtils.GetRandomFloat(0, 1); Vector3D randomCircleInPlain = randCircle.X * m_rightVector + randCircle.Z * m_downVector; var hitPosition = m_currentTarget.Value.Center + Math.Pow(rand, 0.7f) * m_currentTarget.Value.Radius * randomCircleInPlain * METEOR_BLUR_KOEF; //Cast Ray for sure var antigravityDir = -Vector3D.Normalize(MyGravityProviderSystem.CalculateNaturalGravityInPoint(hitPosition)); if (antigravityDir != Vector3D.Zero) { var hi = MyPhysics.CastRay(hitPosition + antigravityDir * (3000), hitPosition, MyPhysics.CollisionLayers.DefaultCollisionLayer); if (hi != null) { hitPosition = hi.Value.Position; } } m_meteorHitPos = hitPosition; for (int i = 0; i < waveMeteorCount; i++) { // hit randCircle = MyUtils.GetRandomVector3CircleNormalized(); rand = MyUtils.GetRandomFloat(0, 1); randomCircleInPlain = randCircle.X * m_rightVector + randCircle.Z * m_downVector; hitPosition = hitPosition + Math.Pow(rand, 0.7f) * m_currentTarget.Value.Radius * randomCircleInPlain; // start var toSun = sunDir * (2000 + 100 * i); randCircle = MyUtils.GetRandomVector3CircleNormalized(); randomCircleInPlain = randCircle.X * m_rightVector + randCircle.Z * m_downVector; var realPosition = hitPosition + toSun + (float)Math.Tan(MyUtils.GetRandomFloat(0, (float)Math.PI / 18)) * randomCircleInPlain; m_meteorList.Add(MyMeteor.SpawnRandom(realPosition, Vector3.Normalize(hitPosition - realPosition))); } m_rightVector = Vector3.Zero; }
private static double?GetCurrentRayIntersection() { Vector3D position; Vector3 normal; HkRigidBody intersectedBody = MyPhysics.CastRay(MyCubeBuilder.IntersectionStart, MyCubeBuilder.IntersectionStart + 2000 * MyCubeBuilder.IntersectionDirection, out position, out normal, MyPhysics.CollisionLayerWithoutCharacter); if (intersectedBody != null) { Vector3D p = position - MyCubeBuilder.IntersectionStart; double dist = p.Length(); return(dist); } return(null); }
public void UpdateSoundOcclusion() { if ((MyFakes.ENABLE_SOUND_OCCLUSION && !this.m_playing2D) && (MySector.MainCamera != null)) { Vector3 position = (Vector3)MySector.MainCamera.Position; LineD ed = new LineD(this.m_entity.PositionComp.WorldAABB.Center, position); if (MyPhysics.CastRay(ed.From, ed.To, 30) == null) { this.VolumeMultiplier = 1f; } else if (this.VolumeMultiplier > 0.2f) { this.VolumeMultiplier = 0.2f; } } }
/// <summary> /// Used to calculate damage for entities that are not grids /// Can be called even if ComputeDamagedBlocks was not called before, but it doesn't do any caching /// </summary> /// <param name="worldPosition">World position from where the cast starts (usually the entity position)</param> /// <returns></returns> public MyRaycastDamageInfo ComputeDamageForEntity(Vector3D worldPosition) { return(new MyRaycastDamageInfo(m_explosionDamage, (float)(worldPosition - m_explosion.Center).Length())); var hitInfo = MyPhysics.CastRay(m_explosion.Center, worldPosition, MyPhysics.CollisionLayers.ExplosionRaycastLayer); if (hitInfo.HasValue) { return(new MyRaycastDamageInfo(m_explosionDamage, (float)(hitInfo.Value.Position - m_explosion.Center).Length())); } return(new MyRaycastDamageInfo(m_explosionDamage, (float)(worldPosition - m_explosion.Center).Length())); m_castBlocks.Clear(); stackOverflowGuard = 0; return(CastPhysicsRay(worldPosition)); }
private bool IsWalkingOnMoon(MyCharacter character) { float maxDistValue = MyConstants.DEFAULT_GROUND_SEARCH_DISTANCE; var from = character.PositionComp.GetPosition() + character.PositionComp.WorldMatrix.Up * 0.5; //(needs some small distance from the bottom or the following call to HavokWorld.CastRay will find no hits) var to = from + character.PositionComp.WorldMatrix.Down * maxDistValue; MyPhysics.CastRay(from, to, m_hits, MyPhysics.CollisionLayers.CharacterCollisionLayer); // Skips invalid hits (null body, self character) int index = 0; while ((index < m_hits.Count) && ((m_hits[index].HkHitInfo.Body == null) || (m_hits[index].HkHitInfo.GetHitEntity() == character.Components))) { index++; } if (m_hits.Count == 0) { return(false); } if (index < m_hits.Count) { // We must take only closest hit (others are hidden behind) var h = m_hits[index]; var entity = h.HkHitInfo.GetHitEntity(); var sqDist = Vector3D.DistanceSquared((Vector3D)h.Position, from); if (sqDist < maxDistValue * maxDistValue) { var voxelBase = entity as MyVoxelBase; if (voxelBase != null && voxelBase.Storage != null && voxelBase.Storage.DataProvider != null && voxelBase.Storage.DataProvider is Sandbox.Engine.Voxels.MyPlanetStorageProvider) { var planetProvider = voxelBase.Storage.DataProvider as Sandbox.Engine.Voxels.MyPlanetStorageProvider; if (planetProvider.Generator != null && planetProvider.Generator.FolderName == "Moon") { m_hits.Clear(); return(true); } } } } m_hits.Clear(); return(false); }
private void DamageGrid(FlameInfo flameInfo, LineD l, MyCubeGrid grid) { HkSphereShape sph = new HkSphereShape(flameInfo.Radius * BlockDefinition.FlameDamageLengthScale); var transform = MatrixD.CreateWorld(l.From, Vector3.Forward, Vector3.Up); var hit = MyPhysics.CastShapeReturnPoint(l.To, sph, ref transform, (int)MyPhysics.CollisionLayers.DefaultCollisionLayer, 0.05f); sph.Base.RemoveReference(); if (hit.HasValue) { //MyRenderProxy.DebugDrawSphere(hit.Value, 0.1f, Color.Green.ToVector3(), 1, true); MyPhysics.CastRay(hit.Value - l.Direction * 0.1f, hit.Value + l.Direction * 0.1f, m_gridRayCastLst, MyPhysics.CollisionLayers.ObjectDetectionCollisionLayer); if (m_gridRayCastLst.Count == 0 || m_gridRayCastLst[0].HkHitInfo.GetHitEntity() != grid) //If you found something other than the targeted grid do nothing { m_gridRayCastLst.Clear(); return; } Vector3D offsetHit = hit.Value + l.Direction * 0.1; m_gridRayCastLst.Clear(); var block = grid.GetCubeBlock(grid.WorldToGridInteger(offsetHit)); //if (block != this.SlimBlock) { //MyRenderProxy.DebugDrawSphere(hit.Value, 0.1f, Color.Green.ToVector3(), 1, true); var invWorld = grid.PositionComp.WorldMatrixNormalizedInv; var gridPos = Vector3D.Transform(offsetHit, invWorld); var gridDir = Vector3D.TransformNormal(l.Direction, invWorld); if (block != null) { //We dont want to damage thruster itself //We dont want smallship thruster to damage heavy armors because of landing if (block.FatBlock != this && (CubeGrid.GridSizeEnum == MyCubeSize.Large || block.BlockDefinition.DeformationRatio > 0.25)) { block.DoDamage(30 * BlockDefinition.FlameDamage, MyDamageType.Environment, attackerId: EntityId); } } if (block == null || block.FatBlock != this) { var areaPlanar = 0.5f * flameInfo.Radius * CubeGrid.GridSize; var areaVertical = 0.5f * CubeGrid.GridSize; grid.Physics.ApplyDeformation(BlockDefinition.FlameDamage, areaPlanar, areaVertical, gridPos, gridDir, MyDamageType.Environment, CubeGrid.GridSizeEnum == MyCubeSize.Small ? 0.1f : 0, attackerId: EntityId); } } } }
public void UpdatePlacement() { m_lastUpdate = MySession.Static.GameplayFrameCounter; m_hitInfo = null; m_closestGrid = null; m_closestVoxelMap = null; LineD line = new LineD(RayStart, RayStart + RayDirection * IntersectionDistance); MyPhysics.CastRay(line.From, line.To, m_tmpHitList, MyPhysics.CollisionLayers.ObjectDetectionCollisionLayer); // Remove character hits. if (MySession.Static.ControlledEntity != null) { m_tmpHitList.RemoveAll(delegate(MyPhysics.HitInfo hitInfo) { return(hitInfo.HkHitInfo.GetHitEntity() == MySession.Static.ControlledEntity.Entity); }); } if (m_tmpHitList.Count == 0) { return; } var hit = m_tmpHitList[0]; m_closestGrid = hit.HkHitInfo.GetHitEntity() as MyCubeGrid; if (m_closestGrid != null) { //always assign otherwise the block will be completely inside/behind the grid m_hitInfo = hit; if (!ClosestGrid.Editable) { m_closestGrid = null; } return; } //if (MyFakes.ENABLE_BLOCK_PLACEMENT_ON_VOXEL) // TODO: check this MyFake to remove or what? //{ m_closestVoxelMap = hit.HkHitInfo.GetHitEntity() as MyVoxelBase; if (m_closestVoxelMap != null) { m_hitInfo = hit; } //} }
public override void AccumulateCorrection(ref Vector3 correction, ref float weight) { m_hitLeft = false; m_hitRight = false; var pos = Parent.PositionAndOrientation; var fwd = Parent.ForwardVector; var left = Vector3.Cross(pos.Up, fwd); List <MyPhysics.HitInfo> list = new List <MyPhysics.HitInfo>(); MyPhysics.CastRay(pos.Translation + pos.Up, pos.Translation + pos.Up + fwd * 0.1f + left * 1.3f, list); if (list.Count > 0) { m_hitLeft = true; m_hitLeftFraction = list[0].HkHitInfo.HitFraction; } list.Clear(); MyPhysics.CastRay(pos.Translation + pos.Up, pos.Translation + pos.Up + fwd * 0.1f - left * 1.3f, list); if (list.Count > 0) { m_hitRight = true; m_hitRightFraction = list[0].HkHitInfo.HitFraction; } list.Clear(); float wtLeft = Weight * 0.01f * (1.0f - m_hitLeftFraction); float wtRight = Weight * 0.01f * (1.0f - m_hitRightFraction); if (m_hitLeft) { correction -= left * wtLeft; weight += wtLeft; } if (m_hitRight) { correction += left * wtRight; weight += wtRight; } if (m_hitLeft && m_hitRight) { correction -= left; weight += wtLeft; } }
private void TrySpawnBot() { Vector3D cameraPos, cameraDir; if (MySession.GetCameraControllerEnum() == Common.ObjectBuilders.MyCameraControllerEnum.ThirdPersonSpectator || MySession.GetCameraControllerEnum() == Common.ObjectBuilders.MyCameraControllerEnum.Entity) { var headMatrix = MySession.ControlledEntity.GetHeadMatrix(true, true); cameraPos = headMatrix.Translation; cameraDir = headMatrix.Forward; } else { cameraPos = MySector.MainCamera.Position; cameraDir = MySector.MainCamera.WorldMatrix.Forward; } List <MyPhysics.HitInfo> hitInfos = new List <MyPhysics.HitInfo>(); MyPhysics.CastRay(cameraPos, cameraPos + cameraDir * 100, hitInfos, MyPhysics.ObjectDetectionCollisionLayer); if (hitInfos.Count == 0) { return; } MyPhysics.HitInfo?closestValidHit = null; foreach (var hitInfo in hitInfos) { var ent = hitInfo.HkHitInfo.Body.GetEntity(); if (ent is MyCubeGrid) { closestValidHit = hitInfo; break; } else if (ent is MyVoxelMap) { closestValidHit = hitInfo; break; } } if (closestValidHit.HasValue) { Vector3D position = closestValidHit.Value.Position; MyAIComponent.Static.SpawnNewBot(BotToSpawn, position); } }
private void UpdateHitEntity() { Debug.Assert(m_raycastCollisionResults.Count == 0); MatrixD pasteMatrix = GetPasteMatrix(); MyPhysics.CastRay(pasteMatrix.Translation, pasteMatrix.Translation + pasteMatrix.Forward * m_dragDistance, m_raycastCollisionResults); m_closestHitDistSq = float.MaxValue; m_hitPos = new Vector3(0.0f, 0.0f, 0.0f); m_hitNormal = new Vector3(1.0f, 0.0f, 0.0f); m_hitEntity = null; foreach (var hit in m_raycastCollisionResults) { if (hit.HkHitInfo.Body == null) { continue; } MyPhysicsBody body = (MyPhysicsBody)hit.HkHitInfo.Body.UserObject; if (body == null) { continue; } Sandbox.ModAPI.IMyEntity entity = body.Entity; if ((entity is MyVoxelMap) || (entity is MyCubeGrid && entity.EntityId != PreviewGrids[0].EntityId)) { if (PreviewGrids[0].GridSizeEnum == MyCubeSize.Large && (entity is MyCubeGrid) && (entity as MyCubeGrid).GridSizeEnum == MyCubeSize.Small) { continue; } float distSq = (float)(hit.Position - pasteMatrix.Translation).LengthSquared(); if (distSq < m_closestHitDistSq) { m_closestHitDistSq = distSq; m_hitPos = hit.Position; m_hitNormal = hit.HkHitInfo.Normal; m_hitEntity = entity; } } } m_raycastCollisionResults.Clear(); }
public void Update() { if (Initialized && m_entity != null) { Vector3 entityPosition = (Vector3)m_entity.PositionComp.WorldAABB.Center; Vector3 targetPos = entityPosition + m_directions[m_currentDirectionIndex] * RAYCAST_LENGTH; LineD line = new LineD(entityPosition, targetPos); MyPhysics.HitInfo?hitInfo = MyPhysics.CastRay(line.From, line.To, MyPhysics.CollisionLayers.CollisionLayerWithoutCharacter); IMyEntity entity = null; Vector3D hitPosition = Vector3D.Zero; Vector3 hitNormal = Vector3.Zero; if (hitInfo.HasValue) { entity = hitInfo.Value.HkHitInfo.GetHitEntity() as MyEntity; hitPosition = hitInfo.Value.Position; hitNormal = hitInfo.Value.HkHitInfo.Normal; } if (entity != null) { float dist = Vector3.Distance(entityPosition, hitPosition); m_detectedLengths[m_currentDirectionIndex] = dist; m_detectedObjects[m_currentDirectionIndex] = (entity is MyCubeGrid || entity is MyCubeBlock) ? ReverbDetectedType.Grid : ReverbDetectedType.Voxel; } else { m_detectedLengths[m_currentDirectionIndex] = -1; m_detectedObjects[m_currentDirectionIndex] = ReverbDetectedType.None; } m_currentDirectionIndex++; if (m_currentDirectionIndex >= m_directions.Length) { m_currentDirectionIndex = 0; if (m_sendInformationToAudio) { float average = GetDetectedAverage(); int grids = GetDetectedNumberOfObjects(ReverbDetectedType.Grid); int voxels = GetDetectedNumberOfObjects(ReverbDetectedType.Voxel); SetReverb(average, grids, voxels); } } } }
public bool TryNamingAGrid() { var worldMatrix = MyAPIGateway.Session.Camera.WorldMatrix; List <MyPhysics.HitInfo> hits = new List <MyPhysics.HitInfo>(); MyPhysics.CastRay(worldMatrix.Translation, worldMatrix.Translation + worldMatrix.Forward * 5, hits, 15); foreach (var hitInfo in hits) { var entity = (MyEntity)hitInfo.HkHitInfo.GetHitEntity(); if (entity is MyCubeGrid) { NameDialog(entity); return(true); } } return(false); }
private bool SpawnBot() { if (MySession.LocalCharacter != null && m_selectedDefinition.HasValue) { var headMatrix = MySession.LocalCharacter.GetHeadMatrix(true); var position = headMatrix.Translation; var definition = MyDefinitionManager.Static.GetBotDefinition(new MyDefinitionId(typeof(MyObjectBuilder_BotDefinition), "BarbarianTest")); var result = MyPhysics.CastRay(position, position + headMatrix.Forward * 30); if (result.HasValue) { var botDef = m_agentDefinitions[m_selectedDefinition.Value]; MyAIComponent.Static.SpawnNewBot(botDef, result.Value.Position); } } return(true); }
/// <summary> /// Finds closest object (grid or voxel map) for placement of blocks . /// </summary> public bool FindClosestPlacementObject(out MyCubeGrid closestGrid, out MyVoxelMap closestVoxelMap) { closestGrid = null; closestVoxelMap = null; if (MySession.ControlledEntity == null) { return(false); } m_hitInfo = null; LineD line = new LineD(IntersectionStart, IntersectionStart + IntersectionDirection * IntersectionDistance); MyPhysics.CastRay(line.From, line.To, m_tmpHitList, MyPhysics.ObjectDetectionCollisionLayer); // Remove character hits. m_tmpHitList.RemoveAll(delegate(MyPhysics.HitInfo hit) { return(hit.HkHitInfo.Body.GetEntity() == MySession.ControlledEntity.Entity); }); if (m_tmpHitList.Count == 0) { return(false); } closestGrid = m_tmpHitList[0].HkHitInfo.Body.GetEntity() as MyCubeGrid; if (closestGrid != null) { m_hitInfo = m_tmpHitList[0]; } if (MyFakes.ENABLE_BLOCK_PLACEMENT_ON_VOXEL) { closestVoxelMap = m_tmpHitList[0].HkHitInfo.Body.GetEntity() as MyVoxelMap; if (closestVoxelMap != null) { m_hitInfo = m_tmpHitList[0]; } } return(closestGrid != null || closestVoxelMap != null); }