Skip to content

Commit

Permalink
Add API function to get/set a modifier function for part local forces…
Browse files Browse the repository at this point in the history
… before applying them
  • Loading branch information
dkavolis committed Aug 13, 2022
1 parent 0e6dc2e commit 476e99b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
24 changes: 24 additions & 0 deletions FerramAerospaceResearch/FARAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
http://forum.kerbalspaceprogram.com/threads/60863
*/

using System;
using ferram4;
using FerramAerospaceResearch.FARAeroComponents;
using FerramAerospaceResearch.FARGUI.FARFlightGUI;
Expand Down Expand Up @@ -419,5 +420,28 @@ public static bool VesselVoxelizationCompletedAndValid(Vessel vessel)

return !(vesselAeroModule is null) && vesselAeroModule.HasValidVoxelizationCurrently();
}

/// <summary>
/// Set a modifier function for part local forces in case FAR does not correctly handle them.
/// It will be called before applying the forces to the part/simulation.
/// </summary>
/// <param name="part">Part to apply modifier to</param>
/// <param name="modifier">Modifier function of part local forces.
/// Signature is (part, (drag, lift, torque)) -> (new_drag, new_lift, new_torque)</param>
public static void SetPartAeroForceModifier(Part part, Func<Part, Vector3, Vector3> modifier)
{
part.FindModuleImplementing<FARAeroPartModule>().AeroForceModifier = modifier;
}

/// <summary>
/// Get the current part local forces modifier function
/// </summary>
/// <param name="part"></param>
/// <returns>Modifier function of part local forces if set and the part is valid, otherwise null.
/// Signature is (part, (drag, lift, torque)) -> (new_drag, new_lift, new_torque)</returns>
public static Func<Part, Vector3, Vector3> GetPartAeroForceModifier(Part part)
{
return part.FindModuleImplementing<FARAeroPartModule>()?.AeroForceModifier;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public class FARAeroPartModule : PartModule, ILiftProvider
private bool updateVisualization;
public FARWingAerodynamicModel LegacyWingModel { get; private set; }

public Func<Part, Vector3, Vector3> AeroForceModifier { get; set; }

public ProjectedArea ProjectedAreas
{
get { return projectedArea; }
Expand Down Expand Up @@ -270,7 +272,7 @@ private static void IncrementAreas(ref ProjectedArea data, Vector3 vector, Matri

private void Start()
{
shield = new DummyAirstreamShield {part = part};
shield = new DummyAirstreamShield { part = part };

if (waterSlowDragNew < 0)
{
Expand Down Expand Up @@ -761,6 +763,7 @@ private void OnDestroy()

LegacyWingModel = null;
stockAeroSurfaceModule = null;
AeroForceModifier = null;
}

public struct ProjectedArea
Expand Down
19 changes: 19 additions & 0 deletions FerramAerospaceResearch/FARAeroComponents/FARAeroSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
using System.Collections.Generic;
using ferram4;
using FerramAerospaceResearch.FARPartGeometry;
using Unity.Mathematics;
using UnityEngine;

namespace FerramAerospaceResearch.FARAeroComponents
Expand Down Expand Up @@ -447,6 +448,24 @@ IForceContext forceContext
torqueVector -= dampingMoment * nonAxialAngLocalVel +
axialAngLocalVel * (rollDampingMoment * axialAngLocalVel.magnitude) / 0.001f;

if (aeroModule.AeroForceModifier is not null)
{
float velLocalForceMagnitude = Vector3.Dot(forceVector, velLocalNorm);
Vector3 velParallelForce = velLocalForceMagnitude * velLocalNorm; // anti-drag
Vector3 velNormalForce = forceVector - velParallelForce; // lift + side force
var localForce = new Vector3
{
x = -velLocalForceMagnitude,
y = velNormalForce.magnitude,
z = torqueVector.magnitude,
};
float3 newLocalForce = aeroModule.AeroForceModifier(aeroModule.part, localForce);
float3 multiplier = newLocalForce / localForce;

forceVector = velParallelForce * multiplier.x + velNormalForce * multiplier.y;
torqueVector *= multiplier.z;
}

forceVector *= data.dragFactor;
torqueVector *= data.dragFactor;

Expand Down

0 comments on commit 476e99b

Please sign in to comment.