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

Fix a couple of NaNs and nullrefs in VesselModuleRotationRO #2940

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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
46 changes: 36 additions & 10 deletions Source/VesselModuleRotationRO.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Runtime.CompilerServices;


// Modified version of the VesselModule of the same name in MandatoryRCS by gotmachine
Expand Down Expand Up @@ -132,6 +133,11 @@ private void SetRot()

public void SetPosRot(Quaternion rotation, Vector3 position)
{
if (rotation.x == float.NaN)
{
Debug.LogError("[RealismOverhaul] VesselModuleRotationRO is setting rotation to NaN!");
}

if (!vessel.loaded)
{
vessel.vesselTransform.SetPositionAndRotation(position, rotation);
Expand Down Expand Up @@ -538,13 +544,13 @@ private Vector3 AutopilotTargetDirection()
if (autopilotMode == 1 || autopilotMode == 2)
{
if (autopilotContext == 0) // Orbit prograde
target = vessel.obt_velocity;
target = GetVesselObtVelocity();
else if (autopilotContext == 1) // Surface prograde
target = vessel.srf_velocity;
target = GetVesselSrfVelocity();
else if (autopilotContext == 2) // Target prograde
{
if (_lastTarget != null)
target = -(_lastTarget.GetObtVelocity() - vessel.obt_velocity);
target = -(_lastTarget.GetObtVelocity() - GetVesselObtVelocity());
else
return vessel.transform.TransformDirection(referenceUpLocal);
}
Expand All @@ -559,14 +565,14 @@ private Vector3 AutopilotTargetDirection()
else if (autopilotMode == 3 || autopilotMode == 4 || autopilotMode == 5 || autopilotMode == 6)
{
// Get body up vector
Vector3 planetUp = (vessel.rootPart.transform.position - vessel.mainBody.position).normalized;
Vector3 planetUp = (vessel.transform.position - vessel.mainBody.position).normalized;

// Get normal vector
Vector3 normal = new Vector3();
if (autopilotContext == 0) // Orbit
normal = Vector3.Cross(vessel.obt_velocity, planetUp).normalized;
normal = Vector3.Cross(GetVesselObtVelocity(), planetUp).normalized;
else if (autopilotContext == 1 || autopilotContext == 2) // Surface/Target (seems to be the same for normal/radial)
normal = Vector3.Cross(vessel.srf_velocity, planetUp).normalized;
normal = Vector3.Cross(GetVesselSrfVelocity(), planetUp).normalized;

// Return normal/antinormal or calculate radial
if (autopilotMode == 3) // Normal
Expand All @@ -578,9 +584,9 @@ private Vector3 AutopilotTargetDirection()
// Get RadialIn vector
Vector3 radial = new Vector3();
if (autopilotContext == 0) // Orbit
radial = Vector3.Cross(vessel.obt_velocity, normal).normalized;
radial = Vector3.Cross(GetVesselObtVelocity(), normal).normalized;
else if (autopilotContext == 1 || autopilotContext == 2) // Surface/Target (seems to be the same for normal/radial)
radial = Vector3.Cross(vessel.srf_velocity, normal).normalized;
radial = Vector3.Cross(GetVesselSrfVelocity(), normal).normalized;

// Return radial vector
if (autopilotMode == 5) // Radial In
Expand Down Expand Up @@ -635,8 +641,15 @@ private Vector3 AutopilotTargetDirection()
return target.normalized;
}

// Copypasted from PersistentRotation main.cs
private Quaternion FromToRotation(Vector3d fromv, Vector3d tov) //Stock FromToRotation() doesn't work correctly
/// <summary>
/// Returns NaN when either input vector is (0,0,0)
/// Copypasted from PersistentRotation main.cs
/// Stock FromToRotation() doesn't work correctly
/// </summary>
/// <param name="fromv"></param>
/// <param name="tov"></param>
/// <returns></returns>
private Quaternion FromToRotation(Vector3d fromv, Vector3d tov)
{
Vector3d cross = Vector3d.Cross(fromv, tov);
double dot = Vector3d.Dot(fromv, tov);
Expand All @@ -645,6 +658,19 @@ private Quaternion FromToRotation(Vector3d fromv, Vector3d tov) //Stock FromToRo
return new QuaternionD(cross.x * norm, cross.y * norm, cross.z * norm, wval * norm);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector3d GetVesselObtVelocity()
{
return vessel.obt_velocity.IsZero() ? vessel.orbit.GetRelativeVel() : vessel.obt_velocity;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector3d GetVesselSrfVelocity()
{
return vessel.srf_velocity.IsZero() ? vessel.orbit.GetVel() - vessel.mainBody.getRFrmVelOrbit(vessel.orbit) :
vessel.srf_velocity;
}

public static VesselModuleRotationRO GetModule(Vessel v)
{
int c = v.vesselModules.Count;
Expand Down
Loading