Skip to content

Commit

Permalink
Use physicsMass instead of constantly calculating part plus resource …
Browse files Browse the repository at this point in the history
…mass (#14)
  • Loading branch information
siimav authored Feb 24, 2024
1 parent bc9710a commit 2a81c73
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
51 changes: 18 additions & 33 deletions KerbalJointReinforcement/KerbalJointReinforcement/KJRJointUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,11 @@ public static List<Part> GetDecouplerPartStiffeningList(Part p, bool childrenNot
{
if (childrenNotParent)
{
if (p.children != null)
foreach (Part q in p.children)
{
foreach (Part q in p.children)
if (q != null && q.parent == p)
{
if (q != null && q.parent == p)
{
newAdditions.AddRange(GetDecouplerPartStiffeningList(q, childrenNotParent, onlyAddLastPart));
}
newAdditions.AddRange(GetDecouplerPartStiffeningList(q, childrenNotParent, onlyAddLastPart));
}
}
}
Expand All @@ -122,16 +119,16 @@ public static List<Part> GetDecouplerPartStiffeningList(Part p, bool childrenNot
}
else
{
float thisPartMaxMass = MaximumPossiblePartMass(p);
if (childrenNotParent)
double thisPartMaxMass = p.physicsMass;
if (thisPartMaxMass > 0)
{
if (p.children != null)
if (childrenNotParent)
{
foreach (Part q in p.children)
{
if (q != null && q.parent == p)
{
float massRatio = MaximumPossiblePartMass(q) / thisPartMaxMass;
double massRatio = q.physicsMass / thisPartMaxMass;
//if (massRatio < 1)
// massRatio = 1 / massRatio;

Expand All @@ -144,18 +141,18 @@ public static List<Part> GetDecouplerPartStiffeningList(Part p, bool childrenNot
}
}
}
}
else if (p.parent)
{
float massRatio = MaximumPossiblePartMass(p.parent) / thisPartMaxMass;
//if (massRatio < 1)
// massRatio = 1 / massRatio;

if (massRatio > settings.stiffeningExtensionMassRatioThreshold)
else if (p.parent)
{
newAdditions.Add(p.parent);
if (settings.debug)
Debug.Log($"[KJR] Part {p.parent.partInfo.title} added to list due to mass ratio difference");
double massRatio = p.parent.physicsMass / thisPartMaxMass;
//if (massRatio < 1)
// massRatio = 1 / massRatio;

if (massRatio > settings.stiffeningExtensionMassRatioThreshold)
{
newAdditions.Add(p.parent);
if (settings.debug)
Debug.Log($"[KJR] Part {p.parent.partInfo.title} added to list due to mass ratio difference");
}
}
}
}
Expand Down Expand Up @@ -188,18 +185,6 @@ public static void ConnectLaunchClampToGround(Part clamp)
//newJoint.angularXMotion = newJoint.angularYMotion = newJoint.angularZMotion = ConfigurableJointMotion.Locked;
}

public static float MaximumPossiblePartMass(Part p)
{
float maxMass = p.mass;
foreach (PartResource r in p.Resources)
{
maxMass += (float)(r.info.density * r.maxAmount);
}
if (settings.debug)
Debug.Log($"[KJR] Maximum mass for part {p.partInfo.title} is {maxMass}");
return maxMass;
}

public static void AddLaunchClampReinforcementModule(Part p)
{
var pm = (KJRLaunchClampReinforcementModule)p.AddModule(nameof(KJRLaunchClampReinforcementModule));
Expand Down
23 changes: 10 additions & 13 deletions KerbalJointReinforcement/KerbalJointReinforcement/KJRManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private void RunVesselJointUpdateFunction(Vessel v)
{
p.breakingForce = Mathf.Infinity;
p.breakingTorque = Mathf.Infinity;
p.mass = Mathf.Max(p.mass, (p.parent.mass + p.parent.GetResourceMass()) * 0.01f); //We do this to make sure that there is a mass ratio of 100:1 between the clamp and what it's connected to. This helps counteract some of the wobbliness simply, but also allows some give and springiness to absorb the initial physics kick
p.mass = (float)Math.Max(p.physicsMass, p.parent.physicsMass * 0.01); //We do this to make sure that there is a mass ratio of 100:1 between the clamp and what it's connected to. This helps counteract some of the wobbliness simply, but also allows some give and springiness to absorb the initial physics kick
if (KJRJointUtils.settings.debug)
Debug.Log("[KJR] Launch Clamp Break Force / Torque increased");

Expand Down Expand Up @@ -413,7 +413,7 @@ private void UpdatePartJoint(Part p)
//addAdditionalJointToParent &= !(p.Modules.Contains("LaunchClamp") || (p.parent.Modules.Contains("ModuleDecouple") || p.parent.Modules.Contains("ModuleAnchoredDecoupler")));
addAdditionalJointToParent &= !p.Modules.Contains<CModuleStrut>();

float partMass = p.mass + p.GetResourceMass();
double partMass = p.physicsMass;
for (int i = 0; i < jointList.Count; i++)
{
ConfigurableJoint j = jointList[i];
Expand All @@ -424,7 +424,7 @@ private void UpdatePartJoint(Part p)
Rigidbody connectedBody = j.connectedBody;

Part connectedPart = connectedBody.GetComponent<Part>() ?? p.parent;
float parentMass = connectedPart.mass + connectedPart.GetResourceMass();
double parentMass = connectedPart.physicsMass;

if (partMass < KJRJointUtils.settings.massForAdjustment || parentMass < KJRJointUtils.settings.massForAdjustment)
{
Expand Down Expand Up @@ -677,7 +677,7 @@ private void UpdatePartJoint(Part p)
bool massRatioBelowThreshold = false;
int numPartsFurther = 0;

float partMaxMass = KJRJointUtils.MaximumPossiblePartMass(p);
double partMaxMass = p.physicsMass;
List<Part> partsCrossed = new List<Part>();
List<Part> possiblePartsCrossed = new List<Part>();

Expand All @@ -689,12 +689,12 @@ private void UpdatePartJoint(Part p)

do
{
float massRat1, massRat2;
massRat1 = partMaxMass / newConnectedPart.mass;
double massRat1, massRat2;
massRat1 = partMaxMass / newConnectedPart.physicsMass;
if (massRat1 < 1)
massRat1 = 1 / massRat1;

massRat2 = p.mass / KJRJointUtils.MaximumPossiblePartMass(newConnectedPart);
massRat2 = p.physicsMass / newConnectedPart.physicsMass;
if (massRat2 < 1)
massRat2 = 1 / massRat2;

Expand Down Expand Up @@ -965,13 +965,10 @@ public void MultiPartJointTreeChildren(Vessel v)
for (int i = 0; i < v.Parts.Count; ++i)
{
Part p = v.Parts[i];
if (p.children.Count == 0 && !p.Modules.Contains("LaunchClamp") &&
KJRJointUtils.MaximumPossiblePartMass(p) > KJRJointUtils.settings.massForAdjustment)
if (p.rb != null &&
p.children.Count == 0 && !p.Modules.Contains("LaunchClamp") &&
p.physicsMass > KJRJointUtils.settings.massForAdjustment)
{
if (p.rb == null && p.Rigidbody != null)
{
p = p.RigidBodyPart;
}
childPartsToConnect.Add(p);
}
}
Expand Down

0 comments on commit 2a81c73

Please sign in to comment.