Skip to content

Commit

Permalink
fixed bug that prevented the mod to work on ships with struts or fuel…
Browse files Browse the repository at this point in the history
… ducts (that use a different drag model, trying to apply drag cubes on these parts caused exceptions)
  • Loading branch information
neuoy committed Jul 20, 2015
1 parent ba6cff6 commit 8dc1d4b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Plugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.2.0")]
[assembly: AssemblyFileVersion("1.4.2.0")]
[assembly: AssemblyVersion("1.4.3.0")]
[assembly: AssemblyFileVersion("1.4.3.0")]
78 changes: 47 additions & 31 deletions Plugin/StockAeroUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,37 +147,54 @@ public static Vector3 SimAeroForce(Vessel _vessel, Vector3 v_wrld_vel, double al
}

// Get Drag
Vector3 sim_dragVectorDir = v_wrld_vel.normalized;
Vector3 sim_dragVectorDirLocal = -(p.transform.InverseTransformDirection(v_wrld_vel.normalized));

DragCubeList cubes = p.DragCubes;

DragCubeList.CubeData p_drag_data;

// negative local air velocity should go into AddSurfaceDragDirection
try
{
p_drag_data = cubes.AddSurfaceDragDirection(-sim_dragVectorDirLocal, (float)mach);
}
catch (Exception)
{
cubes.SetDrag(sim_dragVectorDirLocal, (float)mach);
cubes.ForceUpdate(true, true);
p_drag_data = cubes.AddSurfaceDragDirection(-sim_dragVectorDirLocal, (float)mach);
//Debug.Log(String.Format("Trajectories: Caught NRE on Drag Initialization. Should be fixed now. {0}", e));
Vector3 sim_dragVectorDir = v_wrld_vel.normalized;
Vector3 sim_dragVectorDirLocal = -(p.transform.InverseTransformDirection(sim_dragVectorDir));

Vector3 liftForce = new Vector3(0, 0, 0);

switch(p.dragModel)
{
case Part.DragModel.DEFAULT:
case Part.DragModel.CUBE:
DragCubeList cubes = p.DragCubes;

DragCubeList.CubeData p_drag_data;

try
{
p_drag_data = cubes.AddSurfaceDragDirection(-sim_dragVectorDirLocal, (float)mach);
}
catch (Exception)
{
cubes.SetDrag(sim_dragVectorDirLocal, (float)mach);
cubes.ForceUpdate(true, true);
p_drag_data = cubes.AddSurfaceDragDirection(-sim_dragVectorDirLocal, (float)mach);
//Debug.Log(String.Format("Trajectories: Caught NRE on Drag Initialization. Should be fixed now. {0}", e));
}

double sim_dragScalar = dyn_pressure * (double)p_drag_data.areaDrag * PhysicsGlobals.DragCubeMultiplier * PhysicsGlobals.DragMultiplier;
total_drag += -(Vector3d)sim_dragVectorDir * sim_dragScalar;

liftForce = p_drag_data.liftForce;

break;

case Part.DragModel.SPHERICAL:
total_drag += -(Vector3d)sim_dragVectorDir * (double)p.maximum_drag;
break;

case Part.DragModel.CYLINDRICAL:
total_drag += -(Vector3d)sim_dragVectorDir * (double)Mathf.Lerp(p.minimum_drag, p.maximum_drag, Mathf.Abs(Vector3.Dot(p.partTransform.TransformDirection(p.dragReferenceVector), sim_dragVectorDir)));
break;

case Part.DragModel.CONIC:
total_drag += -(Vector3d)sim_dragVectorDir * (double)Mathf.Lerp(p.minimum_drag, p.maximum_drag, Vector3.Angle(p.partTransform.TransformDirection(p.dragReferenceVector), sim_dragVectorDir) / 180f);
break;

default:
// no drag to apply
break;
}
// NRE occurs in AddSurfaceDragDirection call if SetDrag isn't run to initialize.
// ForceUpdate may not be necessary.
// Runs the risk of something else throwing an NRE, but what are you going to do?
// Logging disabled for performance - if someone is bug hunting turn it back on.

float areaDrag = p_drag_data.areaDrag;
float area = p_drag_data.area;
float dragCoeff = p_drag_data.dragCoeff;
Vector3 dragVector = p_drag_data.dragVector;
Vector3 liftForce = p_drag_data.liftForce;

double sim_dragScalar = dyn_pressure * (double)areaDrag * PhysicsGlobals.DragCubeMultiplier * PhysicsGlobals.DragMultiplier;

// If it isn't a wing or lifter, get body lift.
if (!p.hasLiftModule)
Expand All @@ -188,7 +205,6 @@ public static Vector3 SimAeroForce(Vessel _vessel, Vector3 v_wrld_vel, double al
bodyLift = Vector3.ProjectOnPlane(bodyLift, sim_dragVectorDir);
// Only accumulate forces for non-LiftModules
total_lift += bodyLift;
total_drag += -(Vector3d)sim_dragVectorDir * sim_dragScalar;
}

// Find ModuleLifingSurface for wings and liftforce.
Expand Down
2 changes: 1 addition & 1 deletion Trajectories.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"NAME":"Trajectories","URL":"http://ksp-avc.cybutek.net/version.php?id=63","DOWNLOAD":"https://kerbalstuff.com/mod/238/Trajectories","CHANGE_LOG_URL":"https://github.com/neuoy/KSPTrajectories/releases","VERSION":{"MAJOR":1,"MINOR":4,"PATCH":2,"BUILD":0},"KSP_VERSION":{"MAJOR":1,"MINOR":0,"PATCH":4},"KSP_VERSION_MIN":{"MAJOR":1,"MINOR":0,"PATCH":0},"KSP_VERSION_MAX":{"MAJOR":1,"MINOR":0,"PATCH":4}}
{"NAME":"Trajectories","URL":"http://ksp-avc.cybutek.net/version.php?id=63","DOWNLOAD":"https://kerbalstuff.com/mod/238/Trajectories","CHANGE_LOG_URL":"https://github.com/neuoy/KSPTrajectories/releases","VERSION":{"MAJOR":1,"MINOR":4,"PATCH":3,"BUILD":0},"KSP_VERSION":{"MAJOR":1,"MINOR":0,"PATCH":4},"KSP_VERSION_MIN":{"MAJOR":1,"MINOR":0,"PATCH":0},"KSP_VERSION_MAX":{"MAJOR":1,"MINOR":0,"PATCH":4}}

0 comments on commit 8dc1d4b

Please sign in to comment.