From 8dc1d4be72feed5a23386b4cc91cc3e75f9f9b51 Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Mon, 20 Jul 2015 20:13:50 +0200 Subject: [PATCH] fixed bug that prevented the mod to work on ships with struts or fuel ducts (that use a different drag model, trying to apply drag cubes on these parts caused exceptions) --- Plugin/Properties/AssemblyInfo.cs | 4 +- Plugin/StockAeroUtil.cs | 78 +++++++++++++++++++------------ Trajectories.version | 2 +- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/Plugin/Properties/AssemblyInfo.cs b/Plugin/Properties/AssemblyInfo.cs index d2f854be..433d9795 100644 --- a/Plugin/Properties/AssemblyInfo.cs +++ b/Plugin/Properties/AssemblyInfo.cs @@ -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")] diff --git a/Plugin/StockAeroUtil.cs b/Plugin/StockAeroUtil.cs index 26577177..4e8d4ab9 100644 --- a/Plugin/StockAeroUtil.cs +++ b/Plugin/StockAeroUtil.cs @@ -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) @@ -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. diff --git a/Trajectories.version b/Trajectories.version index 848f14ae..615ef40c 100644 --- a/Trajectories.version +++ b/Trajectories.version @@ -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}} \ No newline at end of file +{"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}} \ No newline at end of file