Skip to content

Commit

Permalink
Merge pull request #3 from Wully616/refactor-bottlenecklowestpoint
Browse files Browse the repository at this point in the history
Refactored BottleneckLowestPoint calculation
  • Loading branch information
Macoron authored Oct 31, 2019
2 parents b853857 + 4510783 commit fedca90
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions Assets/Unity Simple Liquid/Scripts/SplitController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,25 @@ private Vector3 GenerateBottleneckPos()
return pos;
}

private Vector3 GenerateBottleneckLowesPoint()
{
if (!liquidContainer)
return Vector3.zero;
private Vector3 GenerateBottleneckLowesPoint()
{
if (!liquidContainer)
return Vector3.zero;

// TODO: This code is not optimal and can be done much better
// Righ now it caluclates minimal point of the circle (in 3d) by brute force
var containerOrientation = liquidContainer.transform.rotation;
// Calculate the direction vector of the bottlenecks slope

// Points on bottleneck radius (local space)
var angleStep = 0.1f;
var localPoints = new List<Vector3>();
for (float a = 0; a < Mathf.PI * 2f; a += angleStep)
{
var x = BottleneckRadiusWorld * Mathf.Cos(a);
var z = BottleneckRadiusWorld * Mathf.Sin(a);
Vector3 bottleneckSlope = GetSlopeDirection(Vector3.up, bottleneckPlane.normal);

localPoints.Add(new Vector3(x, 0, z));
}
// Find a position along the slope the side of the bottleneck radius
Vector3 min = BottleneckPos + bottleneckSlope * BottleneckRadiusWorld;

// Transfer points from local to global
var worldPoints = new List<Vector3>();
foreach (var locPoint in localPoints)
{
var worldPoint = BottleneckPos + containerOrientation * locPoint;
worldPoints.Add(worldPoint);
}

// Find the lowest one
var min = worldPoints.OrderBy((pt) => pt.y).First();
return min;
return min;

}
#endregion
}
#endregion

#region Gizmos
private void OnDrawGizmosSelected()
#region Gizmos
private void OnDrawGizmosSelected()
{
// Draws bottleneck direction and radius
var bottleneckPlane = GenerateBottleneckPlane();
Expand Down

0 comments on commit fedca90

Please sign in to comment.