private void UpdateThrusts() { Vector3 thrust; if (AutopilotEnabled) { thrust = ComputeAiThrust(AutoPilotThrust); } else { thrust = ComputeBaseThrust(ControlThrust); } UpdatePowerAndThrustStrength(thrust, true); thrust = ApplyThrustModifiers(thrust); Thrust = thrust; if (m_grid.GridSystems.ControlSystem.IsLocallyControlled || (!m_grid.GridSystems.ControlSystem.IsControlled && Sync.IsServer) || (false && Sync.IsServer)) { if (Thrust.LengthSquared() > 0.001f) { if (m_grid.Physics.Enabled) { m_grid.Physics.AddForce(MyPhysicsForceType.ADD_BODY_FORCE_AND_BODY_TORQUE, Thrust, null, null); } } const float stoppingVelocitySq = 0.001f * 0.001f; if (m_grid.Physics.Enabled) { if (m_grid.Physics.LinearVelocity != Vector3.Zero && m_grid.Physics.LinearVelocity.LengthSquared() < stoppingVelocitySq && m_grid.Physics.RigidBody.IsActive) { m_grid.Physics.LinearVelocity = Vector3.Zero; } } } }
private void UpdateThrusts() { Matrix invWorldRot = m_grid.PositionComp.GetWorldMatrixNormalizedInv().GetOrientation(); Vector3 localVelocity = Vector3.Transform(m_grid.Physics.LinearVelocity, ref invWorldRot); Vector3 positiveControl = Vector3.Clamp(ControlThrust, Vector3.Zero, Vector3.One); Vector3 negativeControl = Vector3.Clamp(ControlThrust, -Vector3.One, Vector3.Zero); Vector3 slowdownControl = Vector3.Zero; if (DampenersEnabled) { slowdownControl = Vector3.IsZeroVector(ControlThrust, 0.001f) * Vector3.IsZeroVector(m_totalThrustOverride); } Thrust = negativeControl * m_maxNegativeThrust + positiveControl * m_maxPositiveThrust; Thrust = Vector3.Clamp(Thrust, -m_maxNegativeThrust, m_maxPositiveThrust); const float STOPPING_TIME = 0.5f; var slowdownAcceleration = -localVelocity / STOPPING_TIME; var slowdownThrust = slowdownAcceleration * m_grid.Physics.Mass * slowdownControl; Thrust = Vector3.Clamp(Thrust + slowdownThrust, -m_maxNegativeThrust * MyFakes.SLOWDOWN_FACTOR_THRUST_MULTIPLIER, m_maxPositiveThrust * MyFakes.SLOWDOWN_FACTOR_THRUST_MULTIPLIER); // Calculate ratio of usage for different directions. Vector3 thrustPositive = Thrust / (m_maxPositiveThrust + 0.0000001f); Vector3 thrustNegative = -Thrust / (m_maxNegativeThrust + 0.0000001f); thrustPositive = Vector3.Clamp(thrustPositive, Vector3.Zero, Vector3.One * MyConstants.MAX_THRUST); thrustNegative = Vector3.Clamp(thrustNegative, Vector3.Zero, Vector3.One * MyConstants.MAX_THRUST); // When using joystick, there may be fractional values, not just 0 and 1. float requiredPower = 0; requiredPower += (thrustPositive.X > 0) ? thrustPositive.X * GetMaxRequirement(Vector3I.Left) : 0; requiredPower += (thrustPositive.Y > 0) ? thrustPositive.Y * GetMaxRequirement(Vector3I.Down) : 0; requiredPower += (thrustPositive.Z > 0) ? thrustPositive.Z * GetMaxRequirement(Vector3I.Forward) : 0; requiredPower += (thrustNegative.X > 0) ? thrustNegative.X * GetMaxRequirement(Vector3I.Right) : 0; requiredPower += (thrustNegative.Y > 0) ? thrustNegative.Y * GetMaxRequirement(Vector3I.Up) : 0; requiredPower += (thrustNegative.Z > 0) ? thrustNegative.Z * GetMaxRequirement(Vector3I.Backward) : 0; requiredPower += m_totalThrustOverridePower; if (requiredPower < m_minPowerInputTotal) { requiredPower = m_minPowerInputTotal; } // Setting this notifies power distributor who updates power input and thus changes SuppliedPowerRatio. RequiredPowerInput = requiredPower; PowerReceiver.Update(); Thrust += m_totalThrustOverride; Thrust *= PowerReceiver.SuppliedRatio; Thrust *= MyFakes.THRUST_FORCE_RATIO; if (m_grid.GridSystems.ControlSystem.IsLocallyControlled || (!m_grid.GridSystems.ControlSystem.IsControlled && Sync.IsServer) || (false && Sync.IsServer)) { if (Thrust.LengthSquared() > 0.001f) { if (m_grid.Physics.Enabled) { m_grid.Physics.AddForce(MyPhysicsForceType.ADD_BODY_FORCE_AND_BODY_TORQUE, Thrust, null, null); } } const float stoppingVelocitySq = 0.001f * 0.001f; if (m_grid.Physics.Enabled) { if (m_grid.Physics.LinearVelocity != Vector3.Zero && m_grid.Physics.LinearVelocity.LengthSquared() < stoppingVelocitySq && m_grid.Physics.RigidBody.IsActive) { m_grid.Physics.LinearVelocity = Vector3.Zero; } } } UpdateThrustStrength(m_thrustsByDirection[Vector3I.Left], thrustPositive.X, PowerReceiver.SuppliedRatio); UpdateThrustStrength(m_thrustsByDirection[Vector3I.Down], thrustPositive.Y, PowerReceiver.SuppliedRatio); UpdateThrustStrength(m_thrustsByDirection[Vector3I.Forward], thrustPositive.Z, PowerReceiver.SuppliedRatio); UpdateThrustStrength(m_thrustsByDirection[Vector3I.Right], thrustNegative.X, PowerReceiver.SuppliedRatio); UpdateThrustStrength(m_thrustsByDirection[Vector3I.Up], thrustNegative.Y, PowerReceiver.SuppliedRatio); UpdateThrustStrength(m_thrustsByDirection[Vector3I.Backward], thrustNegative.Z, PowerReceiver.SuppliedRatio); }