Skip to content

Commit

Permalink
A bunch of FlightIntegrator and VesselPrecalculate optimizations, mai…
Browse files Browse the repository at this point in the history
…nly relevant in large part count situations. More can be done : I didn't touch yet stuff specific to aero situations (drag, conduction occlusion...) and there is probably something to be done about Integrate()
  • Loading branch information
gotmachine committed Jun 3, 2024
1 parent deef8b0 commit 1f7acfd
Show file tree
Hide file tree
Showing 4 changed files with 901 additions and 1 deletion.
4 changes: 4 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ KSP_COMMUNITY_FIXES
// state synchronization and caching solar panels scaled space raycasts results.
OptimizedModuleRaycasts = true
// General micro-optimization of FlightIntegrator and VesselPrecalculate, significantely increase
// framerate in large part count situations.
FlightPerf = true
// ##########################
// Modding
// ##########################
Expand Down
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<Compile Include="Performance\DisableMapUpdateInFlight.cs" />
<Compile Include="Performance\DragCubeGeneration.cs" />
<Compile Include="Performance\FastLoader.cs" />
<Compile Include="Performance\FlightPerf.cs" />
<Compile Include="Performance\IMGUIOptimization.cs" />
<Compile Include="Performance\LocalizerPerf.cs" />
<Compile Include="Performance\LowerMinPhysicsDTPerFrame.cs" />
Expand Down
54 changes: 53 additions & 1 deletion KSPCommunityFixes/Library/Numerics.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System;
using System.Runtime.CompilerServices;
using UnityEngine;

namespace KSPCommunityFixes.Library
Expand All @@ -16,6 +17,48 @@ public static void Add(ref this Vector3d v, Vector3d other)
v.z += other.z;
}

/// <summary>
/// Return a QuaternionD representing a rotation from a Vector3d to another
/// </summary>
public static QuaternionD FromToRotation(Vector3d from, Vector3d to)
{
double d = Vector3d.Dot(from, to);
double qw = Math.Sqrt(from.sqrMagnitude * to.sqrMagnitude) + d;
double x, y, z, sqrMag;
if (qw < 1e-12)
{
// vectors are 180 degrees apart
x = from.x;
y = from.y;
z = -from.z;
sqrMag = x * x + y * y + z * z;
if (sqrMag != 1.0)
{
double invNorm = 1.0 / Math.Sqrt(sqrMag);
x *= invNorm;
y *= invNorm;
z *= invNorm;
}
return new QuaternionD(x, y, z, 0.0);
}

Vector3d axis = Vector3d.Cross(from, to);
x = axis.x;
y = axis.y;
z = axis.z;
sqrMag = x * x + y * y + z * z + qw * qw;
if (sqrMag != 1.0)
{
double invNorm = 1.0 / Math.Sqrt(sqrMag);
x *= invNorm;
y *= invNorm;
z *= invNorm;
qw *= invNorm;
}

return new QuaternionD(x, y, z, qw);
}

/// <summary>
/// Cast a Matrix4x4 to a Matrix4x4D
/// </summary>
Expand Down Expand Up @@ -50,6 +93,15 @@ public static Vector3d MultiplyVector(ref this Matrix4x4D m, Vector3d vector)
m.m20 * vector.x + m.m21 * vector.y + m.m22 * vector.z);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3d MultiplyPoint3x4(ref this Matrix4x4D m, double x, double y, double z)
{
return new Vector3d(
m.m00 * x + m.m01 * y + m.m02 * z + m.m03,
m.m10 * x + m.m11 * y + m.m12 * z + m.m13,
m.m20 * x + m.m21 * y + m.m22 * z + m.m23);
}

/// <summary>
/// Evaluate whether a given integral value is a power of 2.
/// </summary>
Expand Down
Loading

0 comments on commit 1f7acfd

Please sign in to comment.