private static bool GetNonPenetratingTransformPosition(ref BoundingBox box, ref MatrixD transform) { Quaternion q = Quaternion.CreateFromRotationMatrix(transform); Vector3 halfExtents = box.HalfExtents; try { for (int i = 0; i < 11; ++i) { float offset = 0.3f * i; Vector3D translation = transform.Translation + Vector3D.UnitY * offset; m_tmpCollisions.Clear(); MyPhysics.GetPenetrationsBox(ref halfExtents, ref translation, ref q, m_tmpCollisions, MyPhysics.CollisionLayers.DefaultCollisionLayer); if (m_tmpCollisions.Count == 0) { transform.Translation = translation; return(true); } } return(false); } finally { m_tmpCollisions.Clear(); } }
private MyAttachableTopBlockBase FindMatchingTop() { Debug.Assert(CubeGrid != null, "\"Impossible\""); Debug.Assert(m_penetrations != null, "\"Impossible\""); Debug.Assert(CubeGrid.Physics != null, "\"Impossible\""); Quaternion orientation; Vector3D pos; Vector3 halfExtents; ComputeTopQueryBox(out pos, out halfExtents, out orientation); try { MyPhysics.GetPenetrationsBox(ref halfExtents, ref pos, ref orientation, m_penetrations, MyPhysics.CollisionLayers.DefaultCollisionLayer); foreach (var obj in m_penetrations) { var entity = obj.GetCollisionEntity(); if (entity == null || entity == CubeGrid) { continue; } MyAttachableTopBlockBase top = FindTopInGrid(entity, pos); if (top != null) { return(top); } MyPhysicsBody body = entity.Physics as MyPhysicsBody; if (body != null) { foreach (var child in body.WeldInfo.Children) { top = FindTopInGrid(child.Entity, pos); if (top != null) { return(top); } } } } } catch (Exception ex) { } finally { m_penetrations.Clear(); } return(null); }
private HkRigidBody FindBody(out Vector3D pivot) { Quaternion orientation; Vector3 halfExtents; foreach (var m in m_lockPositions) { GetBoxFromMatrix(m, out halfExtents, out pivot, out orientation); try { halfExtents *= new Vector3(2.0f, 1.0f, 2.0f); orientation.Normalize(); MyPhysics.GetPenetrationsBox(ref halfExtents, ref pivot, ref orientation, m_penetrations, MyPhysics.ObjectDetectionCollisionLayer); foreach (var obj in m_penetrations) { var entity = obj.GetEntity(); if (entity == null) { continue; } var grid = entity as MyCubeGrid; if (grid == null) { grid = entity.Parent as MyCubeGrid; } // Dont want to lock to fixed/keyframed object if (entity == null || entity is Sandbox.Game.Entities.Character.MyCharacter || (grid != null && !grid.IsStatic && obj.IsFixedOrKeyframed)) { continue; } if (CubeGrid.Physics != null && obj != CubeGrid.Physics.RigidBody && obj != CubeGrid.Physics.RigidBody2) { return(obj); } } } finally { m_penetrations.Clear(); } } pivot = Vector3D.Zero; return(null); }
private static bool GetNonPenetratingTransformPosition(ref BoundingBox box, ref MatrixD transform) { bool flag; Quaternion rotation = Quaternion.CreateFromRotationMatrix(transform); Vector3 halfExtents = box.HalfExtents; try { int num = 0; while (true) { if (num >= 11) { flag = false; } else { float num2 = 0.3f * num; Vector3D translation = transform.Translation + (Vector3D.UnitY * num2); m_tmpCollisions.Clear(); MyPhysics.GetPenetrationsBox(ref halfExtents, ref translation, ref rotation, m_tmpCollisions, 15); if (m_tmpCollisions.Count != 0) { num++; continue; } transform.Translation = translation; flag = true; } break; } } finally { m_tmpCollisions.Clear(); } return(flag); }
public override void UpdateBeforeSimulation() { return; base.UpdateBeforeSimulation(); if (m_particleSpawnCounter-- > 0) { return; } var entity = MySession.Static.ControlledEntity as MyEntity; if (entity == null) { return; } var controlledEntity = entity.GetTopMostParent(); if (controlledEntity == null) { return; } try { ProfilerShort.Begin("Grassland.UpdateBeforeSimulation"); m_particleSpawnCounter = (int)Math.Round(m_particleSpawnCounter + m_particleSpawnCounter * m_particleSpawnIntervalRandomness * (MyRandom.Instance.NextFloat() * 2.0f - 1.0f)); if (MyRandom.Instance.FloatNormal() <= m_particleDensity) { return; } var cameraPosition = MySector.MainCamera.Position; MyPlanet nearestPlanet = MyGravityProviderSystem.GetNearestPlanet(cameraPosition); Vector3D naturalGravity = nearestPlanet.GetWorldGravityGrid(MySector.MainCamera.Position); if (naturalGravity.Dot(MySector.DirectionToSunNormalized) > 0) // Only spawn during the day { return; } var velocity = (MySession.Static.GetCameraControllerEnum() != MyCameraControllerEnum.Entity && MySession.Static.GetCameraControllerEnum() != MyCameraControllerEnum.ThirdPersonSpectator ? Vector3.Zero : controlledEntity.Physics.LinearVelocity); var speed = velocity.Length(); var cameraPositionLocal = Vector3D.Transform(cameraPosition, MatrixD.Invert(nearestPlanet.WorldMatrix)); //Vector3D nearestSurfacePointLocal = nearestPlanet.GetClosestSurfacePointLocal(cameraPosition); // Vector3D nearestSurfacePointWorld = Vector3D.Transform(nearestSurfacePointLocal, nearestPlanet.WorldMatrix); // bool test = nearestPlanet.IsFloraAtPosition(nearestSurfacePointLocal); var currentCharacter = controlledEntity as MyCharacter; float characterFlyingMaxSpeed = (currentCharacter != null) ? currentCharacter.Physics.CharacterProxy.CharacterFlyingMaxLinearVelocity() : MyGridPhysics.ShipMaxLinearVelocity(); Vector3 halfExtents = Vector3.One * m_particleSpawnDistance; if (speed / characterFlyingMaxSpeed > 1.0f) { halfExtents += 10.0f * velocity / characterFlyingMaxSpeed; } var entityTranslation = cameraPosition; var searchPosition = entityTranslation + velocity; MyPhysics.GetPenetrationsBox(ref halfExtents, ref searchPosition, ref Quaternion.Identity, m_bodyCollisions, MyPhysics.CollisionLayers.NotCollideWithStaticLayer); var spawnPosition = default(Vector3D); bool spawnPositionFound = false; foreach (var foundEntity in m_bodyCollisions) { var environmentItems = foundEntity.Body.GetEntity(foundEntity.ShapeKey) as MyEnvironmentItems; if (environmentItems != null) { environmentItems.GetAllItemsInRadius(searchPosition, m_particleSpawnDistance, m_tmpItemInfos); } } if (m_tmpItemInfos.Count != 0) { int selectedTreeIndex = MyRandom.Instance.Next(0, m_tmpItemInfos.Count - 1); spawnPosition = m_tmpItemInfos[selectedTreeIndex].Transform.Position; spawnPositionFound = true; } if (!spawnPositionFound) { return; } var spawnedParticle = Spawn(spawnPosition); if (spawnedParticle == null) { return; } InitializePath(spawnedParticle); } finally { m_bodyCollisions.Clear(); ProfilerShort.End(); } }
public override void UpdateBeforeSimulation() { int rayCount = 0; if (m_rayCastQueue.Count > 0 && m_rayCastCounter % 20 == 0) { while (rayCount < 50 && m_rayCastQueue.Count > 0) { var rand = MyUtils.GetRandomInt(m_rayCastQueue.Count - 1); var entity = m_rayCastQueue[rand].Entity; var l = m_rayCastQueue[rand].Ray; var p = m_rayCastQueue[rand].Position; var particle = m_rayCastQueue[rand].Particle; if (entity is MyCubeGrid) { particle.Stop(); var grid = entity as MyCubeGrid; var invMat = grid.PositionComp.GetWorldMatrixNormalizedInv(); if (grid.BlocksDestructionEnabled) { grid.Physics.ApplyDeformation(6f, 3f, 3f, Vector3.Transform(p, invMat), Vector3.Normalize(Vector3.Transform(m_directionFromSunNormalized, invMat)), MyDamageType.Environment); } //MyPhysics.HavokWorld.CastRay(l.From, l.To, m_hitLst); //rayCount++; //MyEntity ent = null; //if (m_hitLst.Count != 0) // ent = m_hitLst[0].GetEntity(); //if (ent == grid) //{ // grid.Physics.ApplyDeformation(6f, 3f, 3f, Vector3.Transform(m_hitLst[0].Position, invMat), Vector3.Normalize(Vector3.Transform(m_directionFromSunNormalized, invMat)), Sandbox.Game.Weapons.MyDamageType.Environment); // //var block = grid.GetBlock(Vector3I.Floor(Vector3.Transform(m_hitLst[0].Position, invMat) / grid.GridSize)); // //if (block != null) // // grid.ApplyDestructionDeformation(block); // m_rayCastQueue.RemoveAt(0); // m_hitLst.Clear(); // break; //} m_rayCastQueue.RemoveAt(rand); m_hitLst.Clear(); break; } } } m_rayCastCounter++; // Update only if sun wind is active if (IsActive == false) { return; } //? float dT = ((float)MySandboxGame.TotalGamePlayTimeInMilliseconds - (float)m_timeLastUpdate) / 1000.0f; m_timeLastUpdate = MySandboxGame.TotalGamePlayTimeInMilliseconds; if (MySandboxGame.IsPaused) { return; } m_deltaTime += dT; float traveledDistance = m_speed * m_deltaTime; // If sun wind finished its way, we will turn it off if (traveledDistance >= MySunWindConstants.SUN_WIND_LENGTH_TOTAL) { IsActive = false; StopCue(); return; } Vector3D campos = MySession.LocalCharacter == null ? Vector3D.Zero : MySession.LocalCharacter.Entity.WorldMatrix.Translation; // This is plane that goes through sun wind, it's in its middle m_planeMiddle = new PlaneD(m_initialSunWindPosition + m_directionFromSunNormalized * traveledDistance, m_directionFromSunNormalized); m_distanceToSunWind = m_planeMiddle.DistanceToPoint(ref campos); // We make sure that sound moves always on line that goes through camera. So it's not in the middle of sun wind, more like middle where is camera. // Reason is that I want the sound always go through camera. m_positionOnCameraLine = /*MySession.Player.PlayerEntity.Entity.WorldMatrix.Translation*/ -m_directionFromSunNormalized * m_distanceToSunWind; Vector3D positionFront = m_positionOnCameraLine + m_directionFromSunNormalized * 2000; Vector3D positionBack = m_positionOnCameraLine + m_directionFromSunNormalized * -2000; m_planeFront = new PlaneD(positionFront, m_directionFromSunNormalized); m_planeBack = new PlaneD(positionBack, m_directionFromSunNormalized); var distanceToFrontPlane = m_planeFront.DistanceToPoint(ref campos); var distanceToBackPlane = m_planeBack.DistanceToPoint(ref campos); #region commented //Vector3 positionOfSound; //if ((distanceToFrontPlane <= 0) && (distanceToBackPlane >= 0)) //{ // positionOfSound = MySession.Player.PlayerEntity.Entity.WorldMatrix.Translation; //} //else if (distanceToFrontPlane > 0) //{ // positionOfSound = positionFront; //} //else //{ // positionOfSound = positionBack; //} // Update position of sound. It works like this: we hear coming sound, then we are in the sound and then we hear it coming out. //MyAudio.Static.UpdateCuePosition(m_burningCue, positionOfSound, m_directionFromSunNormalized, -m_downVector, m_directionFromSunNormalized * m_speed); //MySounds.UpdateCuePosition(m_burningCue, positionOfSound, m_directionFromSunNormalized, Vector3.Up, Vector3.Zero); //MyLogManager.WriteLine("positionOfSound: " + MyUtils.GetFormatedVector3(positionOfSound, 3)); //MyLogManager.WriteLine("m_directionFromSunNormalized: " + MyUtils.GetFormatedVector3(m_directionFromSunNormalized, 3)); //MyLogManager.WriteLine("m_downVector: " + MyUtils.GetFormatedVector3(m_downVector, 3)); //Position = positionOfSound; // Shake player's head //float distanceToSound; //Vector3.Distance(ref positionOfSound, ref campos, out distanceToSound); //float shake = 1 - MathHelper.Clamp(distanceToSound / 1000, 0, 1); /*if (MySession.Player.Controller.ControlledEntity != null) * { * MySession.PlayerShip.IncreaseHeadShake( * MathHelper.Lerp(MyHeadShakeConstants.HEAD_SHAKE_AMOUNT_DURING_SUN_WIND_MIN, * MyHeadShakeConstants.HEAD_SHAKE_AMOUNT_DURING_SUN_WIND_MAX, shake)); * }*/ #endregion for (int i = 0; i < m_sunwindEntities.Count;) { if (m_sunwindEntities[i].MarkedForClose) { m_sunwindEntities.RemoveAtFast(i); } else { i++; } } var q = VRageMath.Quaternion.CreateFromRotationMatrix(Matrix.CreateFromDir(m_directionFromSunNormalized, m_downVector)); var v = new Vector3(10000, 10000, 2000); MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(positionFront + m_directionFromSunNormalized * 2500, v, q), Color.Red.ToVector3(), 1, false, false); if (m_rayCastCounter == 120) { var pos = positionFront + m_directionFromSunNormalized * 2500; MyPhysics.GetPenetrationsBox(ref v, ref pos, ref q, m_intersectionLst, MyPhysics.DefaultCollisionLayer); foreach (var hit in m_intersectionLst) { var entity = hit.GetCollisionEntity(); if (entity is MyVoxelMap) { continue; } if (!m_sunwindEntities.Contains(entity)) { m_sunwindEntities.Add(entity); } } m_intersectionLst.Clear(); for (int i = 0; i < m_sunwindEntities.Count; i++) { var entity = m_sunwindEntities[i]; if (entity is MyCubeGrid) { var grid = entity as MyCubeGrid; var aabb = grid.PositionComp.WorldAABB; var halfDiagonal = (aabb.Center - aabb.Min).Length(); var rightMax = ((aabb.Center - aabb.Min) / m_rightVector).AbsMin(); var downMax = ((aabb.Center - aabb.Min) / m_downVector).AbsMin(); var size = (grid.Max - grid.Min); var max = Math.Max(size.X, Math.Max(size.Y, size.Z)); var invMat = grid.PositionComp.GetWorldMatrixNormalizedInv(); var start = aabb.Center - rightMax * m_rightVector - downMax * m_downVector; for (int x = 0; x < rightMax * 2; x += grid.GridSizeEnum == Common.ObjectBuilders.MyCubeSize.Large ? 25 : 10) { for (int y = 0; y < downMax * 2; y += grid.GridSizeEnum == Common.ObjectBuilders.MyCubeSize.Large ? 25 : 10) { var pivot = start + x * m_rightVector + y * m_downVector; pivot += (float)halfDiagonal * m_directionFromSunNormalized; var circle = MyUtils.GetRandomVector3CircleNormalized(); float rand = MyUtils.GetRandomFloat(0, grid.GridSizeEnum == Common.ObjectBuilders.MyCubeSize.Large ? 10 : 5); pivot += m_rightVector * circle.X * rand + m_downVector * circle.Z * rand; LineD l = new LineD(pivot - m_directionFromSunNormalized * (float)halfDiagonal, pivot); if (grid.RayCastBlocks(l.From, l.To).HasValue) { l.From = pivot - m_directionFromSunNormalized * 1000; MyPhysics.CastRay(l.From, l.To, m_hitLst); m_rayCastCounter++; if (m_hitLst.Count == 0 || m_hitLst[0].HkHitInfo.GetHitEntity() != grid.Components) { m_hitLst.Clear(); continue; } MyParticleEffect particle; if (MyParticlesManager.TryCreateParticleEffect((int)MyParticleEffectsIDEnum.Prefab_LeakingFire, out particle)) { particle.WorldMatrix = MatrixD.CreateWorld(m_hitLst[0].Position, Vector3D.Forward, Vector3D.Up); } m_rayCastQueue.Add(new MyEntityRayCastPair() { Entity = grid, Ray = l, Position = m_hitLst[0].Position, Particle = particle }); //grid.Physics.ApplyDeformation(0.2f, 4, 2, Vector3.Transform(m_hitLst[0].Position, invMat), Vector3.Transform(m_directionFromSunNormalized, invMat), Sandbox.Game.Weapons.MyDamageType.Environment); } } } m_sunwindEntities.Remove(grid); i--; } else { m_sunwindEntities.Remove(entity); i--; } } m_rayCastCounter = 0; } // Apply force to all objects that aren't static and are hit by sun wind (ignoring voxels and large ships) //MyEntities.ApplySunWindForce(m_sunwindEntities, ref m_planeFront, ref m_planeBack, DoNotIgnoreTheseTypes, ref m_directionFromSunNormalized); // Start small billboards if (m_distanceToSunWind <= MySunWindConstants.SWITCH_LARGE_AND_SMALL_BILLBOARD_DISTANCE) { Debug.Assert(m_computedMaxDistances == MySunWindConstants.SMALL_BILLBOARDS_SIZE.X * MySunWindConstants.SMALL_BILLBOARDS_SIZE.Y, "Not all small billboard MaxDistances are computed!"); m_smallBillboardsStarted = true; } ComputeMaxDistances(); base.UpdateBeforeSimulation(); }
private MyEntity FindBody(out Vector3D pivot) { pivot = Vector3D.Zero; if (CubeGrid.Physics == null) { return(null); } Quaternion orientation; Vector3 halfExtents; foreach (var m in m_lockPositions) { GetBoxFromMatrix(m, out halfExtents, out pivot, out orientation); HkBoxShape boxShape; try { halfExtents *= new Vector3(2.0f, 1.0f, 2.0f); orientation.Normalize(); MyPhysics.GetPenetrationsBox(ref halfExtents, ref pivot, ref orientation, m_penetrations, MyPhysics.CollisionLayers.DefaultCollisionLayer); boxShape = new HkBoxShape(halfExtents); Matrix tranform = Matrix.CreateFromQuaternion(orientation); //tranform.Translation = pivot; //MyOrientedBoundingBoxD obb = new MyOrientedBoundingBoxD(new BoundingBoxD(-halfExtents, halfExtents),tranform); //tranform.Translation = Vector3D.Zero; //MyRenderProxy.DebugDrawOBB(obb, Color.Red, 1, false, false); foreach (var obj in m_penetrations) { var entity = MyPhysicsExtensions.GetCollisionEntity(obj) as MyEntity; if (entity == null)// || entity.Parent != null) { continue; } if (entity.GetPhysicsBody().WeldInfo.Children.Count > 0) { Matrix t2; foreach (var child in entity.GetPhysicsBody().WeldInfo.Children) { var childEnt = child.Entity as MyEntity; t2 = childEnt.GetPhysicsBody().WeldInfo.Transform *entity.Physics.RigidBody.GetRigidBodyMatrix(); t2.Translation = entity.Physics.ClusterToWorld(t2.Translation); //obb = new MyOrientedBoundingBoxD((BoundingBoxD)childEnt.PositionComp.LocalAABB, t2); //MyRenderProxy.DebugDrawOBB(obb, Color.Green, 1, false, false); t2.Translation = t2.Translation - pivot; if (MyPhysics.IsPenetratingShapeShape(boxShape, ref tranform, child.WeldedRigidBody.GetShape(), ref t2)) { if ( CanAttachTo(obj, child.Entity as MyEntity)) { return(child.Entity as MyEntity); } } } t2 = entity.Physics.RigidBody.GetRigidBodyMatrix(); t2.Translation = entity.Physics.ClusterToWorld(t2.Translation) - pivot; if (MyPhysics.IsPenetratingShapeShape(boxShape, ref tranform, entity.GetPhysicsBody().GetShape(), ref t2) && CanAttachTo(obj, entity)) { return(entity); } } else if (CanAttachTo(obj, entity)) { return(entity); } } } finally { boxShape.Base.RemoveReference(); m_penetrations.Clear(); } } return(null); }
protected MyMotorRotor FindMatchingRotor() { Debug.Assert(CubeGrid != null); Debug.Assert(m_penetrations != null); Debug.Assert(CubeGrid.Physics != null); if (CubeGrid == null) { MySandboxGame.Log.WriteLine("MyMotorStator.FindMatchingRotor(): Cube grid == null!"); return(null); } if (m_penetrations == null) { MySandboxGame.Log.WriteLine("MyMotorStator.FindMatchingRotor(): penetrations cache == null!"); return(null); } if (CubeGrid.Physics == null) { MySandboxGame.Log.WriteLine("MyMotorStator.FindMatchingRotor(): Cube grid physics == null!"); return(null); } Quaternion orientation; Vector3D pos; Vector3 halfExtents; ComputeRotorQueryBox(out pos, out halfExtents, out orientation); try { MyPhysics.GetPenetrationsBox(ref halfExtents, ref pos, ref orientation, m_penetrations, MyPhysics.DefaultCollisionLayer); foreach (var obj in m_penetrations) { if (obj == null) { continue; } if (obj == CubeGrid.Physics.RigidBody || obj == CubeGrid.Physics.RigidBody2) { continue; } var entity = obj.GetEntity(); if (entity == null) { continue; } var grid = entity as MyCubeGrid; if (grid == null) { continue; } // Rotor should always be on position [0,0,0]; var pos2 = Vector3.Transform(DummyPosition, CubeGrid.WorldMatrix); var blockPos = grid.RayCastBlocks(pos2, pos2 + WorldMatrix.Up); if (blockPos.HasValue) { var slimBlock = grid.GetCubeBlock(blockPos.Value); if (slimBlock == null || slimBlock.FatBlock == null) { continue; } var rotor = slimBlock.FatBlock as MyMotorRotor; if (rotor != null) { return(rotor); } } } } finally { m_penetrations.Clear(); } return(null); }
public override void UpdateBeforeSimulation() { int num = 0; if ((m_rayCastQueue.Count > 0) && ((this.m_rayCastCounter % 20) == 0)) { while ((num < 50) && (m_rayCastQueue.Count > 0)) { int randomInt = MyUtils.GetRandomInt(m_rayCastQueue.Count - 1); MyEntity entity = m_rayCastQueue[randomInt].Entity; MyEntityRayCastPair local1 = m_rayCastQueue[randomInt]; Vector3D position = m_rayCastQueue[randomInt].Position; MyParticleEffect particle = m_rayCastQueue[randomInt].Particle; if (entity is MyCubeGrid) { particle.Stop(true); MyCubeGrid grid = entity as MyCubeGrid; MatrixD worldMatrixNormalizedInv = grid.PositionComp.WorldMatrixNormalizedInv; if (grid.BlocksDestructionEnabled) { grid.Physics.ApplyDeformation(6f, 3f, 3f, (Vector3)Vector3.Transform((Vector3)position, worldMatrixNormalizedInv), Vector3.Normalize(Vector3.Transform((Vector3)m_directionFromSunNormalized, worldMatrixNormalizedInv)), MyDamageType.Environment, 0f, 0f, 0L); } m_rayCastQueue.RemoveAt(randomInt); this.m_hitLst.Clear(); break; } } } this.m_rayCastCounter++; if (IsActive) { float num2 = (MySandboxGame.TotalGamePlayTimeInMilliseconds - m_timeLastUpdate) / 1000f; m_timeLastUpdate = MySandboxGame.TotalGamePlayTimeInMilliseconds; if (!MySandboxGame.IsPaused) { m_deltaTime += num2; float num3 = m_speed * m_deltaTime; if (num3 >= 60000f) { IsActive = false; StopCue(); } else { Vector3D translation; if (MySession.Static.LocalCharacter != null) { translation = MySession.Static.LocalCharacter.Entity.WorldMatrix.Translation; } else { translation = Vector3D.Zero; } Vector3D point = translation; m_planeMiddle = new PlaneD(m_initialSunWindPosition + (m_directionFromSunNormalized * num3), m_directionFromSunNormalized); m_distanceToSunWind = m_planeMiddle.DistanceToPoint(ref point); m_positionOnCameraLine = -m_directionFromSunNormalized * m_distanceToSunWind; Vector3D position = m_positionOnCameraLine + (m_directionFromSunNormalized * 2000.0); Vector3D vectord3 = m_positionOnCameraLine + (m_directionFromSunNormalized * -2000.0); m_planeFront = new PlaneD(position, m_directionFromSunNormalized); m_planeBack = new PlaneD(vectord3, m_directionFromSunNormalized); m_planeFront.DistanceToPoint(ref point); m_planeBack.DistanceToPoint(ref point); int index = 0; while (index < m_sunwindEntities.Count) { if (m_sunwindEntities[index].MarkedForClose) { m_sunwindEntities.RemoveAtFast <IMyEntity>(index); continue; } index++; } Quaternion orientation = Quaternion.CreateFromRotationMatrix(Matrix.CreateFromDir((Vector3)m_directionFromSunNormalized, (Vector3)m_downVector)); Vector3 halfExtents = new Vector3(10000f, 10000f, 2000f); MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(position + (m_directionFromSunNormalized * 2500.0), halfExtents, orientation), Color.Red.ToVector3(), 1f, false, false, false); if (this.m_rayCastCounter == 120) { Vector3D translation = position + (m_directionFromSunNormalized * 2500.0); MyPhysics.GetPenetrationsBox(ref halfExtents, ref translation, ref orientation, m_intersectionLst, 15); using (List <HkBodyCollision> .Enumerator enumerator = m_intersectionLst.GetEnumerator()) { while (enumerator.MoveNext()) { IMyEntity collisionEntity = enumerator.Current.GetCollisionEntity(); if (!(collisionEntity is MyVoxelMap) && !m_sunwindEntities.Contains(collisionEntity)) { m_sunwindEntities.Add(collisionEntity); } } } m_intersectionLst.Clear(); int num6 = 0; while (true) { if (num6 >= m_sunwindEntities.Count) { this.m_rayCastCounter = 0; break; } IMyEntity item = m_sunwindEntities[num6]; if (item is MyCubeGrid) { MyCubeGrid grid2 = item as MyCubeGrid; BoundingBoxD worldAABB = grid2.PositionComp.WorldAABB; double num7 = (worldAABB.Center - worldAABB.Min).Length(); double num8 = ((worldAABB.Center - worldAABB.Min) / m_rightVector).AbsMin(); double num9 = ((worldAABB.Center - worldAABB.Min) / m_downVector).AbsMin(); Vector3I vectori = grid2.Max - grid2.Min; Math.Max(vectori.X, Math.Max(vectori.Y, vectori.Z)); MatrixD worldMatrixNormalizedInv = grid2.PositionComp.WorldMatrixNormalizedInv; Vector3D vectord6 = (worldAABB.Center - (num8 * m_rightVector)) - (num9 * m_downVector); int num10 = 0; while (true) { if (num10 >= (num8 * 2.0)) { m_sunwindEntities.Remove(grid2); num6--; break; } int num11 = 0; while (true) { if (num11 >= (num9 * 2.0)) { num10 += (grid2.GridSizeEnum == MyCubeSize.Large) ? 0x19 : 10; break; } Vector3D to = ((vectord6 + (num10 * m_rightVector)) + (num11 * m_downVector)) + (((float)num7) * m_directionFromSunNormalized); Vector3 vector2 = MyUtils.GetRandomVector3CircleNormalized(); float randomFloat = MyUtils.GetRandomFloat(0f, (grid2.GridSizeEnum == MyCubeSize.Large) ? ((float)10) : ((float)5)); to += ((m_rightVector * vector2.X) * randomFloat) + ((m_downVector * vector2.Z) * randomFloat); LineD ed = new LineD(to - (m_directionFromSunNormalized * ((float)num7)), to); if (grid2.RayCastBlocks(ed.From, ed.To) != null) { ed.From = to - (m_directionFromSunNormalized * 1000.0); MyPhysics.CastRay(ed.From, ed.To, this.m_hitLst, 0); this.m_rayCastCounter++; if ((this.m_hitLst.Count == 0) || !ReferenceEquals(this.m_hitLst[0].HkHitInfo.GetHitEntity(), grid2.Components)) { this.m_hitLst.Clear(); } else { MyParticleEffect effect2; MyParticlesManager.TryCreateParticleEffect("Dummy", MatrixD.CreateWorld(this.m_hitLst[0].Position, Vector3D.Forward, Vector3D.Up), out effect2); MyEntityRayCastPair pair = new MyEntityRayCastPair { Entity = grid2, _Ray = ed, Position = this.m_hitLst[0].Position, Particle = effect2 }; m_rayCastQueue.Add(pair); } } num11 += (grid2.GridSizeEnum == MyCubeSize.Large) ? 0x19 : 10; } } } else { m_sunwindEntities.Remove(item); num6--; } num6++; } } if (m_distanceToSunWind <= 10000.0) { m_smallBillboardsStarted = true; } ComputeMaxDistances(); base.UpdateBeforeSimulation(); } } } }
public override void DebugDraw() { MatrixD worldMatrix; Vector3D vectord4; if (MyDebugDrawSettings.DEBUG_DRAW_GRID_AABB) { MatrixD worldMatrix = this.m_cubeGrid.PositionComp.WorldMatrix; MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(this.m_cubeGrid.PositionComp.LocalAABB, worldMatrix), Color.Yellow, 0.2f, false, true, false); MyRenderProxy.DebugDrawAxis(worldMatrix, 1f, false, false, false); } if (MyDebugDrawSettings.DEBUG_DRAW_FIXED_BLOCK_QUERIES) { foreach (MySlimBlock local1 in this.m_cubeGrid.GetBlocks()) { Vector3D vectord; Matrix matrix; BoundingBox geometryLocalBox = local1.FatBlock.GetGeometryLocalBox(); Vector3 halfExtents = geometryLocalBox.Size / 2f; local1.ComputeScaledCenter(out vectord); vectord = Vector3D.Transform(vectord + geometryLocalBox.Center, this.m_cubeGrid.WorldMatrix); local1.Orientation.GetMatrix(out matrix); worldMatrix = this.m_cubeGrid.WorldMatrix; Quaternion rotation = Quaternion.CreateFromRotationMatrix((MatrixD)(matrix * worldMatrix.GetOrientation())); MyPhysics.GetPenetrationsBox(ref halfExtents, ref vectord, ref rotation, this.m_penetrations, 14); bool flag = false; using (List <HkBodyCollision> .Enumerator enumerator2 = this.m_penetrations.GetEnumerator()) { while (enumerator2.MoveNext()) { IMyEntity collisionEntity = enumerator2.Current.GetCollisionEntity(); if ((collisionEntity != null) && (collisionEntity is MyVoxelMap)) { flag = true; break; } } } this.m_penetrations.Clear(); MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(vectord, halfExtents, rotation), flag ? Color.Green : Color.Red, 0.1f, false, false, false); } } if (MyDebugDrawSettings.DEBUG_DRAW_GRID_NAMES || MyDebugDrawSettings.DEBUG_DRAW_GRID_CONTROL) { string text = ""; Color white = Color.White; if (MyDebugDrawSettings.DEBUG_DRAW_GRID_NAMES) { text = text + this.m_cubeGrid.ToString() + " "; } if (MyDebugDrawSettings.DEBUG_DRAW_GRID_CONTROL) { MyPlayer controllingPlayer = Sync.Players.GetControllingPlayer(this.m_cubeGrid); if (controllingPlayer != null) { text = text + "Controlled by: " + controllingPlayer.DisplayName; white = Color.LightGreen; } } MyRenderProxy.DebugDrawText3D(this.m_cubeGrid.PositionComp.WorldAABB.Center, text, white, 0.7f, false, MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER, -1, false); } MyRenderComponentCubeGrid render = this.m_cubeGrid.Render; if (MyDebugDrawSettings.DEBUG_DRAW_BLOCK_GROUPS) { worldMatrix = this.m_cubeGrid.PositionComp.WorldMatrix; Vector3D translation = worldMatrix.Translation; foreach (MyBlockGroup group in this.m_cubeGrid.BlockGroups) { MyRenderProxy.DebugDrawText3D(translation, group.Name.ToString(), Color.Red, 1f, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, -1, false); worldMatrix = this.m_cubeGrid.PositionComp.WorldMatrix; translation += (worldMatrix.Right * group.Name.Length) * 0.10000000149011612; } } if (MyDebugDrawSettings.DEBUG_DRAW_GRID_DIRTY_BLOCKS) { foreach (KeyValuePair <Vector3I, MyTimeSpan> pair in this.m_dirtyBlocks) { Vector3 vector1; if (this.m_cubeGrid.GetCubeBlock(pair.Key) == null) { vector1 = Color.Yellow.ToVector3(); } else { vector1 = Color.Red.ToVector3(); } Vector3 color = vector1; MyRenderProxy.DebugDrawOBB((Matrix.CreateScale(this.m_cubeGrid.GridSize) * Matrix.CreateTranslation(pair.Key * this.m_cubeGrid.GridSize)) * this.m_cubeGrid.WorldMatrix, color, 0.15f, false, true, true, false); } } if (MyDebugDrawSettings.DEBUG_DRAW_DISPLACED_BONES) { Vector3D position = MySector.MainCamera.Position; foreach (MySlimBlock block in this.m_cubeGrid.CubeBlocks) { MyCube cube; if (this.m_cubeGrid.TryGetCube(block.Position, out cube)) { int num = 0; foreach (MyCubePart part in cube.Parts) { if ((part.Model.BoneMapping != null) && (num == MyPetaInputComponent.DEBUG_INDEX)) { for (int i = 0; i < Math.Min(part.Model.BoneMapping.Length, 9); i++) { Matrix orientation = part.InstanceData.LocalMatrix.GetOrientation(); Vector3I vectori = Vector3I.Round(Vector3.Transform((Vector3)(((part.Model.BoneMapping[i] * 1f) - Vector3.One) * 1f), orientation)); Vector3I bonePos = Vector3I.Round(Vector3.Transform((Vector3)(((part.Model.BoneMapping[i] * 1f) - Vector3.One) * 1f), orientation) + Vector3.One); Vector3 bone = this.m_cubeGrid.Skeleton.GetBone(block.Position, bonePos); Vector3D vectord3 = Vector3D.TransformNormal(bone, this.m_cubeGrid.PositionComp.WorldMatrix); Vector3D vectord6 = Vector3D.Transform((Vector3)(this.m_cubeGrid.GridSize * (block.Position + (vectori / 2f))), this.m_cubeGrid.PositionComp.WorldMatrix); MyRenderProxy.DebugDrawSphere(vectord6, 0.025f, Color.Green, 0.5f, false, true, true, false); MyRenderProxy.DebugDrawText3D(vectord6, i.ToString(), Color.Green, 0.5f, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, -1, false); Color?colorTo = null; MyRenderProxy.DebugDrawArrow3D(vectord6, vectord6 + vectord3, Color.Red, colorTo, false, 0.1, null, 0.5f, false); } } num++; } } } } if (MyDebugDrawSettings.DEBUG_DRAW_STRUCTURAL_INTEGRITY && (this.m_cubeGrid.StructuralIntegrity != null)) { this.m_cubeGrid.StructuralIntegrity.DebugDraw(); } if (MyDebugDrawSettings.DEBUG_DRAW_CUBES) { foreach (MySlimBlock local2 in this.m_cubeGrid.CubeBlocks) { Matrix matrix4; local2.GetLocalMatrix(out matrix4); MyRenderProxy.DebugDrawAxis(matrix4 * this.m_cubeGrid.WorldMatrix, 1f, false, false, false); MyCubeBlock fatBlock = local2.FatBlock; if (fatBlock != null) { fatBlock.DebugDraw(); } } } this.m_cubeGrid.GridSystems.DebugDraw(); bool flag1 = MyDebugDrawSettings.DEBUG_DRAW_GRID_TERMINAL_SYSTEMS; if (MyDebugDrawSettings.DEBUG_DRAW_GRID_ORIGINS) { MyRenderProxy.DebugDrawAxis(this.m_cubeGrid.PositionComp.WorldMatrix, 1f, false, false, false); } if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW && MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS_ALL) { foreach (MySlimBlock block3 in this.m_cubeGrid.GetBlocks()) { vectord4 = this.m_cubeGrid.GridIntegerToWorld(block3.Position) - MySector.MainCamera.Position; if (vectord4.LengthSquared() < 200.0) { this.DebugDrawMountPoints(block3); } } } if (MyDebugDrawSettings.DEBUG_DRAW_BLOCK_INTEGRITY && (MySector.MainCamera != null)) { vectord4 = MySector.MainCamera.Position - this.m_cubeGrid.PositionComp.WorldVolume.Center; if (vectord4.Length() < (16.0 + this.m_cubeGrid.PositionComp.WorldVolume.Radius)) { using (HashSet <MySlimBlock> .Enumerator enumerator = this.m_cubeGrid.CubeBlocks.GetEnumerator()) { MySlimBlock current; float num4; goto TR_0011; TR_0002: MyRenderProxy.DebugDrawText3D(this.m_cubeGrid.GridIntegerToWorld(current.Position), ((int)num4).ToString(), Color.White, (this.m_cubeGrid.GridSizeEnum == MyCubeSize.Large) ? 0.65f : 0.5f, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, -1, false); TR_0011: while (true) { if (!enumerator.MoveNext()) { break; } current = enumerator.Current; Vector3D vectord5 = this.m_cubeGrid.GridIntegerToWorld(current.Position); if (this.m_cubeGrid.GridSizeEnum != MyCubeSize.Large) { if (MySector.MainCamera == null) { continue; } if ((MySector.MainCamera.Position - vectord5).LengthSquared() >= 9.0) { continue; } } num4 = 0f; if (current.FatBlock is MyCompoundCubeBlock) { foreach (MySlimBlock block5 in (current.FatBlock as MyCompoundCubeBlock).GetBlocks()) { num4 += block5.Integrity * block5.BlockDefinition.MaxIntegrityRatio; } } else { num4 = current.Integrity * current.BlockDefinition.MaxIntegrityRatio; } goto TR_0002; } } } } base.DebugDraw(); }
protected MyAttachableTopBlockBase FindMatchingTop() { Debug.Assert(CubeGrid != null); Debug.Assert(m_penetrations != null); Debug.Assert(CubeGrid.Physics != null); if (CubeGrid == null) { MySandboxGame.Log.WriteLine("MyPistonBase.FindMatchingTop(): Cube grid == null!"); return(null); } if (m_penetrations == null) { MySandboxGame.Log.WriteLine("MyPistonBase.FindMatchingTop(): penetrations cache == null!"); return(null); } if (CubeGrid.Physics == null) { MySandboxGame.Log.WriteLine("MyPistonBase.FindMatchingTop(): Cube grid physics == null!"); return(null); } Quaternion orientation; Vector3D pos; Vector3 halfExtents; ComputeTopQueryBox(out pos, out halfExtents, out orientation); try { MyPhysics.GetPenetrationsBox(ref halfExtents, ref pos, ref orientation, m_penetrations, MyPhysics.CollisionLayers.DefaultCollisionLayer); foreach (var obj in m_penetrations) { var entity = obj.GetCollisionEntity(); if (entity == null || entity == CubeGrid) { continue; } MyAttachableTopBlockBase top = FindTopInGrid(entity, pos); if (top != null) { return(top); } MyPhysicsBody body = entity.Physics as MyPhysicsBody; if (body != null) { foreach (var child in body.WeldInfo.Children) { top = FindTopInGrid(child.Entity, pos); if (top != null) { return(top); } } } } } catch (Exception ex) { } finally { m_penetrations.Clear(); } return(null); }
protected MyMotorRotor FindMatchingRotor() { Debug.Assert(CubeGrid != null); Debug.Assert(m_penetrations != null); Debug.Assert(CubeGrid.Physics != null); if (CubeGrid == null) { MySandboxGame.Log.WriteLine("MyMotorStator.FindMatchingRotor(): Cube grid == null!"); return(null); } if (m_penetrations == null) { MySandboxGame.Log.WriteLine("MyMotorStator.FindMatchingRotor(): penetrations cache == null!"); return(null); } if (CubeGrid.Physics == null) { MySandboxGame.Log.WriteLine("MyMotorStator.FindMatchingRotor(): Cube grid physics == null!"); return(null); } Quaternion orientation; Vector3D pos; Vector3 halfExtents; ComputeRotorQueryBox(out pos, out halfExtents, out orientation); try { MyPhysics.GetPenetrationsBox(ref halfExtents, ref pos, ref orientation, m_penetrations, MyPhysics.CollisionLayers.DefaultCollisionLayer); foreach (var obj in m_penetrations) { var entity = obj.GetCollisionEntity(); if (entity == null || entity == CubeGrid) { continue; } MyMotorRotor rotor = FindRotorInGrid(entity); if (rotor != null) { return(rotor); } MyPhysicsBody body = entity.Physics as MyPhysicsBody; if (body != null) { foreach (var child in body.WeldInfo.Children) { rotor = FindRotorInGrid(child.Entity); if (rotor != null) { return(rotor); } } } } } finally { m_penetrations.Clear(); } return(null); }