From 45107833938b7952a0ab5098445e13dcf181ba14 Mon Sep 17 00:00:00 2001 From: William Robb Date: Mon, 28 Oct 2019 20:49:31 +0000 Subject: [PATCH] Refactored BottleneckLowestPoint calculation by reusing the get slope direction function --- .../Scripts/SplitController.cs | 44 ++++++------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/Assets/Unity Simple Liquid/Scripts/SplitController.cs b/Assets/Unity Simple Liquid/Scripts/SplitController.cs index b0de93b..4f56d60 100644 --- a/Assets/Unity Simple Liquid/Scripts/SplitController.cs +++ b/Assets/Unity Simple Liquid/Scripts/SplitController.cs @@ -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(); - 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(); - 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();