Skip to content

Commit

Permalink
Some bugfixes, better surface-related drag
Browse files Browse the repository at this point in the history
  • Loading branch information
ferram4 committed Jul 29, 2015
1 parent 61a1934 commit 910cb97
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
86 changes: 59 additions & 27 deletions BetterBuoyancy/BBModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public class BBModule : PartModule
double horizCrashTolFactor = 7;
double overrideVol = -1;
double overridedepthForMaxForce = -1;
double depth = 0;
double depth = -1;

double volume = 1;
double depthFactor = 0;

float splashDrag = 0;
int splashFrameDelay = 0;
Expand Down Expand Up @@ -70,10 +73,11 @@ private void FixedUpdate()
}

depth = CalculateDepth((Vector3d)part.transform.position);
CalcDepthFactor(depth);
UpdateWaterContact(depth);
if (depth >= 0)
{
ApplyBouyancyForce(BuoyancyForce(part.rb, depth));
ApplyBouyancyForce(BuoyancyForce(part.rb, depth)+ BuoyantDragForce(part.rb, depthFactor));
}
}

Expand All @@ -83,7 +87,7 @@ private void Update()
{
if (depth >= 0)
{
ApplySplashEffect(depth, part.rb);
ApplySplashEffect(depth, part.Rigidbody);
splashFrameDelay = rand.Next(1, 5);
}
}
Expand Down Expand Up @@ -111,18 +115,22 @@ private void UpdateWaterContact(double depth)
if (part.WaterContact)
{
part.WaterContact = false;
vessel.checkSplashed();
}
if(depthFactor < -1)
{
splashDrag = 0;
part.rb.drag = 0;
part.rb.angularDrag = 0;
vessel.checkSplashed();
}
return;
}

float tmp = 0.25f / part.Rigidbody.mass;
float tmp = 0.15f; //base drag

tmp /= part.Rigidbody.mass;
if (splashDrag > tmp)
splashDrag = splashDrag * 0.90f;
splashDrag = splashDrag * 0.98f;
else
splashDrag = tmp;

Expand All @@ -132,14 +140,14 @@ private void UpdateWaterContact(double depth)
vessel.checkSplashed();

float vertVec = Vector3.Dot(part.rb.velocity + Krakensbane.GetFrameVelocityV3f(), vessel.upAxis);
vertVec *= 0.04f;
vertVec *= 0.1f;
if (vertVec > 1)
vertVec = 1;

splashDrag = 10f * vertVec / part.rb.mass;
splashDrag = Math.Max(20f * vertVec / part.rb.mass, tmp);
}
part.rb.drag = part.rb.angularDrag = splashDrag;

part.rb.drag = splashDrag;
part.rb.angularDrag = splashDrag * 10;
}

private Vector3d BuoyancyForce(Rigidbody body, double depth)
Expand All @@ -148,34 +156,58 @@ private Vector3d BuoyancyForce(Rigidbody body, double depth)
if (CheckDieOnHighVelocity(body))
return Vector3.zero;

double vol;
double depthForMaxForce;
if(overrideVol > 0)


Vector3d gForce = -FlightGlobals.getGeeForceAtPosition(part.transform.position);

Vector3d buoyancyForce = gForce * depthFactor;
buoyancyForce *= volume * BBPlanetOceanDensity.EvaluateBodyOceanDensity(vessel.mainBody);

return buoyancyForce;
}

private Vector3d BuoyantDragForce(Rigidbody body, double depthFactor)
{
float tmp = 0;
if (depthFactor < 0.5)
tmp += (float)depthFactor * 2f; //drag from depth
else
tmp += (float)(1f - depthFactor) * 2f;


Vector3d dragForce = Vector3.Project(body.velocity, vessel.upAxis);
double vertVel = dragForce.magnitude;
dragForce *= vertVel;
dragForce *= tmp * BBPlanetOceanDensity.EvaluateBodyOceanDensity(vessel.mainBody);

if (dragForce.magnitude * TimeWarp.fixedDeltaTime / body.mass > vertVel)
dragForce = vessel.upAxis * vertVel;

return -dragForce;
}

private void CalcDepthFactor(double depth)
{
if (overrideVol > 0)
{
vol = overrideVol;
depthForMaxForce = overridedepthForMaxForce;
volume = overrideVol;
depthFactor = overridedepthForMaxForce;
}
else if (part.collider)
{
Vector3 size = part.collider.bounds.size;
vol = size.x * size.y * size.z; //This is highly approximate, but it works
depthForMaxForce = Math.Max(Math.Max(size.x, Math.Max(size.y, size.z)), 2); //This is a very, very rough model of partial immersion
volume = size.x * size.y * size.z; //This is highly approximate, but it works
depthFactor = Math.Max(Math.Max(size.x, Math.Max(size.y, size.z)), 2); //This is a very, very rough model of partial immersion
}
else
{
vol = 1;
depthForMaxForce = 2;
volume = 1;
depthFactor = 2;
}

double depthFactor = depth / depthForMaxForce;
depthFactor = Math.Min(depthFactor, 1);

Vector3d gForce = -FlightGlobals.getGeeForceAtPosition(part.transform.position);
depthFactor = depth / depthFactor;

Vector3d buoyancyForce = gForce * depthFactor;
buoyancyForce *= vol * BBPlanetOceanDensity.EvaluateBodyOceanDensity(vessel.mainBody);

return buoyancyForce;
depthFactor = Math.Min(depthFactor, 1);
}

private void ApplySplashEffect(double depth, Rigidbody body)
Expand Down
Binary file modified GameData/BetterBuoyancy/Plugins/BetterBuoyancy.dll
Binary file not shown.

0 comments on commit 910cb97

Please sign in to comment.