Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored BottleneckLowestPoint calculation #3

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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