/// <summary> /// Recursively updates the engines in the spacecraft and updates propellant mass. /// </summary> private void UpdateEngines(double dt) { Thrust = 0; if (Engines.Length > 0 && PropellantMass > 0) { double altitude = GetRelativeAltitude(); double ispMultiplier = GravitationalParent.GetIspMultiplier(altitude); foreach (IEngine engine in Engines) { if (!engine.IsActive) { continue; } Thrust += engine.Thrust(ispMultiplier); PropellantMass = Math.Max(0, PropellantMass - engine.MassFlowRate() * dt); } } foreach (SpaceCraftBase child in Children) { child.UpdateEngines(dt); Thrust += child.Thrust; } }
/// <summary> /// Updates the spacecraft and it's children. /// </summary> public override void Update(double dt) { double altitude = GetRelativeAltitude(); IspMultiplier = GravitationalParent.GetIspMultiplier(altitude); if (Parent == null) { UpdateEngines(dt); if (Thrust > 0) { var thrustVector = new DVector2(Math.Cos(Pitch), Math.Sin(Pitch)); AccelerationN += (thrustVector * Thrust) / Mass; } if (_requiresStaging) { AccelerationN += _stagingForce; _requiresStaging = false; } // Integrate acceleration Velocity += (AccelerationG * dt); Velocity += (AccelerationD * dt); Velocity += (AccelerationN * dt); Velocity += (AccelerationL * dt); // Re-normalize FTL scenarios if (Velocity.LengthSquared() > Constants.SpeedLightSquared) { Velocity.Normalize(); Velocity *= Constants.SpeedOfLight; } // Integrate velocity Position += (Velocity * dt); MachNumber = GetRelativeVelocity().Length() * 0.0029411764; foreach (ISpaceCraft child in Children) { child.UpdateChildren(Position, Velocity); } _trailTimer += dt; // Somewhat arbitrary conditions for launch trails if (dt < 0.1666666666 && altitude > 50 && !InOrbit && _trailTimer > 1) { _launchTrail.AddPoint(Position, GravitationalParent, Throttle > 0); _trailTimer = 0; } } }
/// <summary> /// Updates the spacecraft and it's children. /// </summary> public override void Update(double dt) { Controller.Update(dt); double altitude = GetRelativeAltitude(); IspMultiplier = GravitationalParent.GetIspMultiplier(altitude); if (Parent == null) { UpdateEngines(dt); if (Thrust > 0) { var thrustVector = new DVector2(Math.Cos(Rotation), Math.Sin(Rotation)); AccelerationN += (thrustVector * Thrust) / Mass; } // Integrate acceleration Velocity += (AccelerationG * dt); Velocity += (AccelerationD * dt); Velocity += (AccelerationN * dt); // Re-normalize FTL scenarios if (Velocity.LengthSquared() > FlightGlobals.SPEED_LIGHT_SQUARED) { Velocity.Normalize(); Velocity *= FlightGlobals.SPEED_OF_LIGHT; } // Integrate velocity Position += (Velocity * dt); MachNumber = GetRelativeVelocity().Length() * 0.0029411764; foreach (ISpaceCraft child in Children) { child.UpdateChildren(Position, Velocity); } } }
private void UpdateEngines(double dt) { _thrust = 0; if (Engines.Length > 0 && PropellantMass > 0) { double ispMultiplier = GravitationalParent.GetIspMultiplier(Altitude); foreach (IEngine engine in Engines) { if (!engine.IsActive) { continue; } _thrust += engine.Thrust(ispMultiplier); PropellantMass = Math.Max(0, PropellantMass - engine.MassFlowRate(ispMultiplier) * dt); } } }
/// <summary> /// Updates the spacecraft and it's children. /// </summary> public override void Update(double dt) { ComputeCachedProperties(); double altitude = GetRelativeAltitude(); if (GravitationalParent == null) { IspMultiplier = 1; } else { IspMultiplier = GravitationalParent.GetIspMultiplier(altitude); } if (Parent == null) { UpdateEngines(dt); if (Thrust > 0 && _isReleased) { var thrustVector = new DVector2(Math.Cos(Pitch) * Math.Cos(Yaw), Math.Sin(Pitch) * Math.Cos(Yaw)); var yawVector = new DVector2(Math.Cos(Pitch) * Math.Sin(Yaw), Math.Sin(Pitch) * Math.Sin(Yaw)); AccelerationN += (thrustVector * Thrust) / Mass; AccelerationY += (yawVector * Thrust) / Mass; } if (_requiresStaging) { // Simulate simple staging mechanism double sAngle = StageOffset.Angle(); DVector2 stagingNormal = DVector2.FromAngle(Pitch + sAngle + Constants.PiOverTwo); AccelerationN += stagingNormal * StagingForce; _requiresStaging = false; } if (_isReleased) { // Integrate acceleration Velocity += AccelerationG * dt; Velocity += AccelerationD * dt; Velocity += AccelerationL * dt; Velocity += AccelerationN * dt; LateralVelocity += AccelerationY * dt; LateralPosition += LateralVelocity * dt; } // Re-normalize FTL scenarios if (Velocity.LengthSquared() > Constants.SpeedLightSquared) { Velocity.Normalize(); Velocity *= Constants.SpeedOfLight; } // If the craft is on the ground with high time warpd don't update the position as normal. // Instead just keep putting the ship at the correct spot on it's gravitational body based on rotation. if (OnGround && dt > 5) { DVector2 parentOffset = GravitationalParent.Position - Position; parentOffset.Normalize(); Position = GravitationalParent.Position + parentOffset * GravitationalParent.SurfaceRadius; } else { // Integrate velocity Position += Velocity * dt; } MachNumber = GetRelativeVelocity().Length() * 0.0029411764; foreach (ISpaceCraft child in Children) { child.UpdateChildren(Position, Velocity); } _trailTimer += dt; // Somewhat arbitrary conditions for launch trails if (dt < 0.1666666666 && !InOrbit && !OnGround && _cachedAltitude > 50 && _cachedAltitude < GravitationalParent.AtmosphereHeight * 2 && _trailTimer > 1) { string parentName = GravitationalParent.ToString(); if (!_launchTrails.ContainsKey(parentName)) { _launchTrails.Add(parentName, new LaunchTrail()); } _launchTrails[parentName].AddPoint(Position, GravitationalParent, Throttle > 0); _trailTimer = 0; } } }
/// <summary> /// Updates the spacecraft and it's children. /// </summary> public override void Update(double dt) { ComputeCachedProperties(); double altitude = GetRelativeAltitude(); IspMultiplier = GravitationalParent.GetIspMultiplier(altitude); if (Parent == null) { UpdateEngines(dt); if (Thrust > 0) { var thrustVector = new DVector2(Math.Cos(Pitch), Math.Sin(Pitch)); AccelerationN += (thrustVector * Thrust) / Mass; } if (_requiresStaging) { // Simulate simple staging mechanism double sAngle = StageOffset.Angle(); DVector2 stagingNormal = DVector2.FromAngle(Pitch + sAngle + Math.PI * 0.5); AccelerationN += stagingNormal * StagingForce; _requiresStaging = false; } // Integrate acceleration Velocity += (AccelerationG * dt); Velocity += (AccelerationD * dt); Velocity += (AccelerationN * dt); Velocity += (AccelerationL * dt); // Re-normalize FTL scenarios if (Velocity.LengthSquared() > Constants.SpeedLightSquared) { Velocity.Normalize(); Velocity *= Constants.SpeedOfLight; } // Integrate velocity Position += (Velocity * dt); MachNumber = GetRelativeVelocity().Length() * 0.0029411764; foreach (ISpaceCraft child in Children) { child.UpdateChildren(Position, Velocity); } _trailTimer += dt; // Somewhat arbitrary conditions for launch trails if (dt < 0.1666666666 && !InOrbit && _cachedAltitude > 50 && _cachedAltitude < GravitationalParent.AtmosphereHeight * 2 && _trailTimer > 1) { string parentName = GravitationalParent.ToString(); if (!_launchTrails.ContainsKey(parentName)) { _launchTrails.Add(parentName, new LaunchTrail()); } _launchTrails[parentName].AddPoint(Position, GravitationalParent, Throttle > 0); _trailTimer = 0; } } }