Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not call C++ with positions, velocities or angular velocities that are NaNs. #2532

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions geometry/r3x3_matrix_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ R3Element<Quotient<RScalar, Scalar>> R3x3Matrix<Scalar>::Solve(
max = Abs(A(i, k));
}
}
CHECK_LE(0, r);
CHECK_GT(3, r);
CHECK_LE(0, r) << *this << " cannot pivot";
CHECK_GT(3, r) << *this << " cannot pivot";
std::swap(A.rows_[k], A.rows_[r]);
std::swap(L.rows_[k], L.rows_[r]);
std::swap(b[k], b[r]);
CHECK_NE(Scalar{}, A(k, k));
CHECK_NE(Scalar{}, A(k, k)) << *this << " is singular";

for (int j = k; j < 3; ++j) {
Scalar U_kj = A(k, j);
Expand Down
4 changes: 2 additions & 2 deletions journal/player_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ TEST_F(PlayerTest, DISABLED_SECULAR_Debug) {
// An example of how journaling may be used for debugging. You must set
// |path| and fill the |method_in| and |method_out_return| protocol buffers.
std::string path =
R"(P:\Public Mockingbird\Principia\Crashes\2507\JOURNAL.20200328-214524)"; // NOLINT
R"(P:\Public Mockingbird\Principia\Crashes\2530\JOURNAL.20200416-093807)"; // NOLINT
Player player(path);
int count = 0;
while (player.Play(count)) {
Expand All @@ -109,7 +109,7 @@ TEST_F(PlayerTest, DISABLED_SECULAR_Debug) {
auto* extension = method_in.MutableExtension(
serialization::CatchUpLaggingVessels::extension);
auto* in = extension->mutable_in();
in->set_plugin(2014156823824);
in->set_plugin(2899402431696);
}
serialization::Method method_out_return;
{
Expand Down
2 changes: 2 additions & 0 deletions ksp_plugin/part.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Part final {

PartId part_id() const;

// When a part is not truthful, all its properties except for name and part_id
// are lies and should not be propagated to the game.
bool truthful() const;
void make_truthful();

Expand Down
37 changes: 24 additions & 13 deletions ksp_plugin_adapter/ksp_plugin_adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,24 @@ private static bool JustAboveTheGround(Vessel vessel,
return height + vertical_speed * Δt < 0;
}

private static bool IsNaN(Vector3d v) {
return double.IsNaN(v.x + v.y + v.z);
}

private static bool IsNaN(XYZ xyz) {
return double.IsNaN(xyz.x + xyz.y + xyz.z);
}

// It seems that parts sometimes have NaN position, velocity or angular
// velocity, presumably because they are being destroyed. Just skip these
// unfaithful parts as if they had no rigid body.
private static bool PartIsFaithful(Part part) {
return part.rb != null &&
!IsNaN(part.rb.position) &&
!IsNaN(part.rb.velocity) &&
!IsNaN(part.rb.angularVelocity);
}

private void OverrideRSASTarget(FlightCtrlState state) {
if (override_rsas_target_ && FlightGlobals.ActiveVessel.Autopilot.Enabled) {
FlightGlobals.ActiveVessel.Autopilot.SAS.SetTargetOrientation(
Expand Down Expand Up @@ -1045,7 +1063,7 @@ private System.Collections.IEnumerator WaitedForFixedUpdate() {
!vessel.packed,
out bool inserted);
if (!vessel.packed) {
foreach (Part part in vessel.parts.Where((part) => part.rb != null)) {
foreach (Part part in vessel.parts.Where(PartIsFaithful)) {
QP degrees_of_freedom;
if (part_id_to_degrees_of_freedom_.ContainsKey(part.flightID)) {
degrees_of_freedom = part_id_to_degrees_of_freedom_[part.flightID];
Expand Down Expand Up @@ -1224,10 +1242,7 @@ private System.Collections.IEnumerator WaitedForFixedUpdate() {
if (!plugin_.HasVessel(vessel.id.ToString())) {
continue;
}
foreach (Part part in vessel.parts) {
if (part.rb == null) {
continue;
}
foreach (Part part in vessel.parts.Where(PartIsFaithful)) {
if (main_body_change_countdown_ == 0 &&
last_main_body_ == FlightGlobals.ActiveVessel?.mainBody) {
plugin_.PartSetApparentRigidMotion(
Expand Down Expand Up @@ -1270,10 +1285,7 @@ private System.Collections.IEnumerator WaitedForFixedUpdate() {
if (!plugin_.HasVessel(vessel.id.ToString())) {
continue;
}
foreach (Part part in vessel.parts) {
if (part.rb == null) {
continue;
}
foreach (Part part in vessel.parts.Where(PartIsFaithful)) {
QPRW part_actual_motion =
plugin_.PartGetActualDegreesOfFreedom(
part.flightID,
Expand Down Expand Up @@ -1463,7 +1475,7 @@ private void FashionablyLate() {
foreach (Vessel vessel in
FlightGlobals.Vessels.Where(v => is_manageable(v) &&
!v.packed)) {
foreach (Part part in vessel.parts.Where((part) => part.rb != null)) {
foreach (Part part in vessel.parts.Where(PartIsFaithful)) {
if (part.torque != Vector3d.zero) {
part_id_to_intrinsic_torque_.Add(part.flightID, part.torque);
}
Expand Down Expand Up @@ -1555,7 +1567,7 @@ private void BetterLateThanNever() {
foreach (Vessel vessel in
FlightGlobals.Vessels.Where(v => is_manageable(v) &&
!v.packed)) {
foreach (Part part in vessel.parts.Where((part) => part.rb != null)) {
foreach (Part part in vessel.parts.Where(PartIsFaithful)) {
// TODO(egg): use the centre of mass.
part_id_to_degrees_of_freedom_.Add(
part.flightID,
Expand Down Expand Up @@ -1651,8 +1663,7 @@ private void RenderGuidance(Vessel active_vessel) {
Burn burn = plugin_.FlightPlanGetManoeuvre(
vessel_guid,
first_future_manœuvre_index.Value).burn;
if (flight_planner_.show_guidance &&
!double.IsNaN(guidance.x + guidance.y + guidance.z)) {
if (flight_planner_.show_guidance && !IsNaN(guidance)) {
// The user wants to show the guidance node, and that node was
// properly computed by the C++ code.
PatchedConicSolver solver = active_vessel.patchedConicSolver;
Expand Down