public void Init(IShipconstruct v, CelestialBody body) { FARHook.UpdateCurrentBody(body); int threads = Threading.ThreadPool.ThreadCount; simulators.Clear(); for (int i = 0; i < threads; i++) { simulators.Add(new InstantConditionSimulationWrapper()); //(InstantConditionSimulationWrapper.Borrow()); } List <Part> oParts = v.Parts; int count = oParts.Count; bool lgWarning = false; int stage = 0; for (int i = 0; i < count; i++) { if (!lgWarning) { ModuleWheels.ModuleWheelDeployment gear = oParts[i].FindModuleImplementing <ModuleWheels.ModuleWheelDeployment>(); bool forcedRetract = !oParts[i].ShieldedFromAirstream && gear != null && gear.Position > 0; if (forcedRetract) { lgWarning = true; } } totalMass += oParts[i].mass + oParts[i].GetResourceMass(); dryMass += oParts[i].mass; CoM += (oParts[i].mass + oParts[i].GetResourceMass()) * oParts[i].transform.TransformPoint(oParts[i].CoMOffset); CoM_dry += (oParts[i].mass) * oParts[i].transform.TransformPoint(oParts[i].CoMOffset); if (oParts[i].inverseStage > stage) { VesselCache.SimulatedEngine.Release(engines); engines.Clear(); stage = oParts[i].inverseStage; } if (oParts[i].inverseStage >= stage) { MultiModeEngine multiMode = oParts[i].FindModuleImplementing <MultiModeEngine>(); if (multiMode != null) { engines.Add(VesselCache.SimulatedEngine.Borrow(oParts[i].FindModulesImplementing <ModuleEngines>().Find(engine => engine.engineID == multiMode.mode), this)); } else { ModuleEngines engine = oParts[i].FindModulesImplementing <ModuleEngines>().FirstOrDefault(); if (engine != null) { engines.Add(VesselCache.SimulatedEngine.Borrow(engine, this)); } } } } }
internal LandingGear(Part part) { if (!Is(part)) { throw new ArgumentException("Part is not landing gear"); } Part = part; var internalPart = part.InternalPart; deployment = internalPart.Module <ModuleWheels.ModuleWheelDeployment> (); damage = internalPart.Module <ModuleWheels.ModuleWheelDamage> (); }
internal Leg(Part part) { if (!Is(part)) { throw new ArgumentException("Part is not a landing leg"); } Part = part; var internalPart = part.InternalPart; wheel = internalPart.Module <ModuleWheelBase> (); deployment = internalPart.Module <ModuleWheels.ModuleWheelDeployment> (); damage = internalPart.Module <ModuleWheels.ModuleWheelDamage> (); }
internal Wheel(Part part) { if (!Is(part)) { throw new ArgumentException("Part is not a wheel"); } Part = part; var internalPart = part.InternalPart; wheel = internalPart.Module <ModuleWheelBase>(); brakes = internalPart.Module <ModuleWheels.ModuleWheelBrakes>(); damage = internalPart.Module <ModuleWheels.ModuleWheelDamage>(); deployment = internalPart.Module <ModuleWheels.ModuleWheelDeployment>(); motor = internalPart.Module <ModuleWheels.ModuleWheelMotor> (); steering = internalPart.Module <ModuleWheels.ModuleWheelSteering>(); suspension = internalPart.Module <ModuleWheels.ModuleWheelSuspension>(); }
private void Init(IShipconstruct v) { totalMass = 0; dryMass = 0; CoM = Vector3.zero; CoM_dry = Vector3.zero; relativeWingArea = 0; stage = 0; List <Part> oParts = v.Parts; List <SimulatedPart> variableDragParts_ctrls = new List <SimulatedPart>(); count = oParts.Count; if (HighLogic.LoadedSceneIsEditor) { for (int i = 0; i < v.Parts.Count; i++) { Part p = v.Parts[i]; if (p.dragModel == Part.DragModel.CUBE && !p.DragCubes.None) { DragCubeList cubes = p.DragCubes; lock (cubes) { DragCubeList.CubeData p_drag_data = new DragCubeList.CubeData(); try { cubes.SetDragWeights(); cubes.SetPartOcclusion(); cubes.AddSurfaceDragDirection(-Vector3.forward, 0, ref p_drag_data); } catch (Exception) { cubes.SetDrag(Vector3.forward, 0); cubes.ForceUpdate(true, true); cubes.SetDragWeights(); cubes.SetPartOcclusion(); cubes.AddSurfaceDragDirection(-Vector3.forward, 0, ref p_drag_data); //Debug.Log(String.Format("Trajectories: Caught NRE on Drag Initialization. Should be fixed now. {0}", e)); } } } } } bool lgWarning = false; for (int i = 0; i < count; i++) { if (!lgWarning) { ModuleWheels.ModuleWheelDeployment gear = oParts[i].FindModuleImplementing <ModuleWheels.ModuleWheelDeployment>(); bool forcedRetract = !oParts[i].ShieldedFromAirstream && gear != null && gear.Position > 0; if (forcedRetract) { lgWarning = true; } } } // Recursively add all parts to collections // What a crazy change to make just to accomodate rotating parts! partCollection = PartCollection.Borrow(this, oParts[0]); CoM /= totalMass; CoM_dry /= dryMass; partCollection.origin = CoM; if (relativeWingArea == 0) { // I'm not sure what effect calling ScreenMessages from a background thread will be. // Fortunately, anyone using this mod probably has at least one wing. ScreenMessages.PostScreenMessage("No wings found, using a reference area of 1.", 5, ScreenMessageStyle.UPPER_CENTER); relativeWingArea = 1; } //if (lgWarning) //ScreenMessages.PostScreenMessage("Landing gear deployed, results may not be accurate.", 5, ScreenMessageStyle.UPPER_CENTER); }
protected void Init(Part p) { this.name = p.name; Rigidbody rigidbody = p.rb; //totalMass = rigidbody == null ? 0 : rigidbody.mass; // TODO : check if we need to use this or the one without the childMass totalMass = p.mass + p.GetResourceMass(); dryMass = p.mass; shieldedFromAirstream = p.ShieldedFromAirstream; noDrag = rigidbody == null && !PhysicsGlobals.ApplyDragToNonPhysicsParts; hasLiftModule = p.hasLiftModule; bodyLiftMultiplier = p.bodyLiftMultiplier; dragModel = p.dragModel; cubesNone = p.DragCubes.None; CoM = p.transform.TransformPoint(p.CoMOffset); CoP = p.transform.TransformPoint(p.CoPOffset); CoL = p.transform.TransformPoint(p.CoLOffset); switch (dragModel) { case Part.DragModel.CYLINDRICAL: case Part.DragModel.CONIC: maximum_drag = p.maximum_drag; minimum_drag = p.minimum_drag; dragReferenceVector = p.partTransform.TransformDirection(p.dragReferenceVector); break; case Part.DragModel.SPHERICAL: maximum_drag = p.maximum_drag; break; case Part.DragModel.CUBE: if (cubesNone) { maximum_drag = p.maximum_drag; } break; } simCurves = SimCurves.Borrow(null); //cubes = new DragCubeList(); ModuleWheels.ModuleWheelDeployment wheelDeployment = p.FindModuleImplementing <ModuleWheels.ModuleWheelDeployment>(); bool forcedRetract = !shieldedFromAirstream && wheelDeployment != null && wheelDeployment.Position > 0; float gearPosition = 0; if (forcedRetract) { gearPosition = wheelDeployment.Position; lock (wheelDeployment) { lock (p.DragCubes) { p.DragCubes.SetCubeWeight("Retracted", 1); p.DragCubes.SetCubeWeight("Deployed", 0); lock (this.cubes) CopyDragCubesList(p.DragCubes, cubes); p.DragCubes.SetCubeWeight("Retracted", 1 - gearPosition); p.DragCubes.SetCubeWeight("Deployed", gearPosition); } } } else { lock (this.cubes) lock (p.DragCubes) CopyDragCubesList(p.DragCubes, cubes); } // Rotation to convert the vessel space vesselVelocity to the part space vesselVelocity // QuaternionD.LookRotation is not working... //partToVessel = Quaternion.LookRotation(p.vessel.GetTransform().InverseTransformDirection(p.transform.forward), p.vessel.GetTransform().InverseTransformDirection(p.transform.up)); //vesselToPart = Quaternion.Inverse(partToVessel); partToVessel = p.transform.rotation; vesselToPart = Quaternion.Inverse(partToVessel); /*Debug.Log(p.name); * Debug.Log(p.transform.rotation); * Debug.Log(Quaternion.Inverse(p.transform.rotation)); * Debug.Log(Quaternion.LookRotation(p.transform.forward, p.transform.up)); * Debug.Log(p.transform.InverseTransformDirection(Vector3.forward) + " // " + Quaternion.Inverse(p.transform.rotation) * Vector3.forward + " // " + Quaternion.Inverse(Quaternion.LookRotation(p.transform.forward, p.transform.up)) * Vector3.forward); * Debug.Log(p.DragCubes.None + " " + p.dragModel); * Debug.Log("");*/ }
private void Init(IShipconstruct v, SimCurves _simCurves) { totalMass = 0; dryMass = 0; CoM = Vector3.zero; CoM_dry = Vector3.zero; List<Part> oParts = v.Parts; List<SimulatedPart> variableDragParts_ctrls = new List<SimulatedPart>(); count = oParts.Count; if (HighLogic.LoadedSceneIsEditor) { for (int i = 0; i < v.Parts.Count; i++) { Part p = v.Parts[i]; if (p.dragModel == Part.DragModel.CUBE && !p.DragCubes.None) { DragCubeList cubes = p.DragCubes; DragCubeList.CubeData p_drag_data = new DragCubeList.CubeData(); try { cubes.SetDragWeights(); cubes.SetPartOcclusion(); cubes.AddSurfaceDragDirection(-Vector3.forward, 0, ref p_drag_data); } catch (Exception) { cubes.SetDrag(Vector3.forward, 0); cubes.ForceUpdate(true, true); cubes.SetDragWeights(); cubes.SetPartOcclusion(); cubes.AddSurfaceDragDirection(-Vector3.forward, 0, ref p_drag_data); //Debug.Log(String.Format("Trajectories: Caught NRE on Drag Initialization. Should be fixed now. {0}", e)); } } } } simCurves = _simCurves; if (parts.Capacity < count) parts.Capacity = count; bool lgWarning = false; int stage = 0; for (int i = 0; i < count; i++) { if (!lgWarning) { ModuleWheels.ModuleWheelDeployment gear = oParts[i].FindModuleImplementing<ModuleWheels.ModuleWheelDeployment>(); bool forcedRetract = !oParts[i].ShieldedFromAirstream && gear != null && gear.Position > 0; if (forcedRetract) lgWarning = true; } SimulatedPart simulatedPart = SimulatedPart.Borrow(oParts[i], this); parts.Add(simulatedPart); totalMass += simulatedPart.totalMass; dryMass += simulatedPart.dryMass; CoM += simulatedPart.totalMass * simulatedPart.CoM; CoM_dry += simulatedPart.dryMass * simulatedPart.CoM; ModuleLiftingSurface liftingSurface = oParts[i].FindModuleImplementing<ModuleLiftingSurface>(); if (liftingSurface != null) { parts[i].hasLiftModule = true; if (liftingSurface is ModuleControlSurface ctrlSurface) { ctrls.Add(SimulatedControlSurface.Borrow(ctrlSurface, simulatedPart)); variableDragParts_ctrls.Add(simulatedPart); if (ctrlSurface.ctrlSurfaceArea < 1) surfaces.Add(SimulatedLiftingSurface.Borrow(ctrlSurface, simulatedPart)); } else surfaces.Add(SimulatedLiftingSurface.Borrow(liftingSurface, simulatedPart)); } List<ITorqueProvider> torqueProviders = oParts[i].FindModulesImplementing<ITorqueProvider>(); // TODO: Add them to a list. if(oParts[i].inverseStage > stage) { SimulatedEngine.Release(engines); engines.Clear(); stage = oParts[i].inverseStage; } if (oParts[i].inverseStage >= stage) { MultiModeEngine multiMode = oParts[i].FindModuleImplementing<MultiModeEngine>(); if (multiMode != null) { engines.Add(SimulatedEngine.Borrow(oParts[i].FindModulesImplementing<ModuleEngines>().Find(engine => engine.engineID == multiMode.mode), simulatedPart)); } else { ModuleEngines engine = oParts[i].FindModulesImplementing<ModuleEngines>().FirstOrDefault(); if (engine != null) engines.Add(SimulatedEngine.Borrow(engine, simulatedPart)); } } } CoM /= totalMass; CoM_dry /= dryMass; if (lgWarning) ScreenMessages.PostScreenMessage("Landing gear deployed, results may not be accurate.", 5, ScreenMessageStyle.UPPER_CENTER); /*for (int i = 0; i < count; i++) { parts[i].CoM -= this.CoM; parts[i].CoL -= this.CoM; parts[i].CoP -= this.CoM; }*/ parts.RemoveAll(part => variableDragParts_ctrls.Contains(part)); }