diff --git a/impulse/Assets/Scripts/Road/RoadGenerator.cs b/impulse/Assets/Scripts/Road/RoadGenerator.cs index 49c82d64e0b904b5c1760aeb1877f7bde00884ab..497ba68fef1d1cec39ddf337b8c8a346c9828b15 100644 --- a/impulse/Assets/Scripts/Road/RoadGenerator.cs +++ b/impulse/Assets/Scripts/Road/RoadGenerator.cs @@ -210,46 +210,10 @@ public class RoadGenerator : MonoBehaviour /// </summary> /// <param name="roadPart">The road part to check</param> /// <returns>True if the road part collide with any other road part</returns> - //private bool IsColliding(GameObject roadPart) - //{ - // bool isColliding = false; - // Physics.SyncTransforms(); - - // // List all colliders that overlap this roadpart's collider - // BoxCollider roadPartCollider = roadPart.GetComponent<BoxCollider>(); - - // Vector3 center = roadPartCollider.transform.TransformPoint(roadPartCollider.center); - // Vector3 halfExtents = Vector3.Scale(roadPartCollider.size, roadPartCollider.transform.lossyScale) / 2f; - // Quaternion rotation = roadPartCollider.transform.rotation; - - // OverlapBoxVisualizer.AddBox(roadPart, center, halfExtents, rotation, 9999f, Color.red); - - // Collider[] colliders = Physics.OverlapBox(center, halfExtents, rotation); - - // StringBuilder stringBuilder = new StringBuilder(); - // foreach (Collider collider in colliders) - // { - // // There is collision only if it's not own road part collider and the object is a road part - // if (collider != roadPartCollider && collider.gameObject.tag == "RoadPart") - // { - // Debug.Log($"{roadPart.name} collides with {collider.gameObject.name}"); - // stringBuilder.AppendLine($"[{Time.time}] OverlapBox hit the following colliders:"); - // foreach (var col in colliders) - // { - // stringBuilder.AppendLine($"[{Time.time}] - {col.gameObject.name}"); - // } - // print(stringBuilder.ToString()); - // isColliding = true; - // } - // } - - // return isColliding; - //} - private bool IsColliding(GameObject roadPart) { - bool isColliding = false; Physics.SyncTransforms(); + bool isColliding = false; Collider[] partColliders = roadPart.GetComponentsInChildren<BoxCollider>(); @@ -260,40 +224,51 @@ public class RoadGenerator : MonoBehaviour Quaternion rotation = partCollider.transform.rotation; Collider[] overlaps = Physics.OverlapBox(worldCenter, halfExtents, rotation); - - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.AppendLine($"[{Time.time}] OverlapBox of {partCollider.gameObject.name} hit the following colliders:"); + //StringBuilder log = new StringBuilder(); + //log.AppendLine($"[{Time.time}] OverlapBox of {partCollider.gameObject.name} hit the following colliders:"); + foreach (var col in overlaps) { - if (col.CompareTag("RoadPart")) OverlapBoxVisualizer.AddBox(roadPart, worldCenter, halfExtents, rotation, 9999f, Color.red); - // Use for the special case of the ramp Up and Down with 2 colliders - if (col.gameObject.name.StartsWith("Up") || col.gameObject.name.StartsWith("Down")) - { - if (col.transform.parent.gameObject.CompareTag("RoadPart") && col.transform.parent.gameObject != roadPart) - { - isColliding = true; - Debug.Log($"{roadPart.name} collides with {col.gameObject.name}"); - stringBuilder.AppendLine($"[{Time.time}] - {col.gameObject.name}"); - break; - } + // Debug in editor purpose only + if (col.CompareTag("RoadPart")) + { + OverlapBoxVisualizer.AddBox(roadPart, worldCenter, halfExtents, rotation, 9999f, Color.red); } - if ((col.gameObject != roadPart && col.CompareTag("RoadPart"))) + + // Special case with Up and Down road part containing 2 colliders instead of 1 + if ((col.gameObject.name.StartsWith("Up") || col.gameObject.name.StartsWith("Down")) && + col.transform.parent != null && + col.transform.parent.gameObject.CompareTag("RoadPart") && + col.transform.parent.gameObject != roadPart) { isColliding = true; - Debug.Log($"{roadPart.name} collides with {col.gameObject.name}"); - stringBuilder.AppendLine($"[{Time.time}] - {col.gameObject.name}"); + //log.AppendLine($"[{Time.time}] - {col.gameObject.name} (special case)"); + Debug.Log($"[SC] {roadPart.name} collides with {col.gameObject.name}"); + break; + } + + // Normal case + if (col.gameObject != roadPart && col.CompareTag("RoadPart")) + { + isColliding = true; + //log.AppendLine($"[{Time.time}] - {col.gameObject.name}"); + Debug.Log($"[NC] {roadPart.name} collides with {col.gameObject.name}"); break; } } - print(stringBuilder.ToString()); - if (isColliding) break; + if (isColliding) + { + //Debug.Log(log.ToString()); + break; + } } return isColliding; } + /// <summary> /// Fix the road by replacing the bad road part with another one from the pending list.<br/> /// </summary> @@ -330,7 +305,7 @@ public class RoadGenerator : MonoBehaviour if (!IsColliding(newRoadPart)) { Debug.Log($"[{Time.time}] Step {step} fixed with {newRoadPart.name}"); - print($"[{Time.time}] {newRoadPart.name} is colliding ? {IsColliding(newRoadPart)}"); + //print($"[{Time.time}] {newRoadPart.name} is colliding ? {IsColliding(newRoadPart)}"); break; } @@ -394,7 +369,7 @@ public class RoadGenerator : MonoBehaviour return -1f; } float segmentDistance = Vector3.Distance(startPoint.position, endPoint.position); - //print($"[{current.name}] distance from start {startPoint.position} to end {endPoint.position} = {segmentDistance}"); + print($"[{current.name}] distance from start {startPoint.position} to end {endPoint.position} = {segmentDistance}"); totalDistance += segmentDistance; }