add separate draw (optional redraw) method, new drawArc and drawArcCenetered methods for FOV

This commit is contained in:
Xevion
2020-05-27 19:36:07 -05:00
parent 784d498a52
commit 30dd4a1100
2 changed files with 49 additions and 6 deletions
+47 -5
View File
@@ -220,9 +220,7 @@ public class Boid : MonoBehaviour {
var triangle = transform.GetComponent<Triangle>(); var triangle = transform.GetComponent<Triangle>();
triangle.meshRenderer.material.color = Color.red; triangle.meshRenderer.material.color = Color.red;
// Add a LineRenderer for Radius Drawing Draw();
DrawCircle(_parent.boidSeparationRange, "Separation Range Circle");
DrawCircle(_parent.boidGroupRange, "Group Range Circle");
} }
// Disable focusing, removing LineRenderers and resetting color // Disable focusing, removing LineRenderers and resetting color
@@ -238,6 +236,20 @@ public class Boid : MonoBehaviour {
Destroy(child.gameObject); Destroy(child.gameObject);
} }
private void Draw(bool redraw = false) {
if (redraw)
foreach (Transform child in transform)
Destroy(child.gameObject);
// Add a LineRenderer for Radius Drawing
DrawCircle(_parent.boidSeparationRange, "Separation Range Circle");
DrawCircle(_parent.boidGroupRange, "Group Range Circle");
// Draw FOV Line
DrawArcCentered((_parent.boidSeparationRange + _parent.boidGroupRange) / 2f, transform.eulerAngles.z,
_parent.boidFOV, "FOV Arc");
}
private void DrawCircle(float radius, string childName) { private void DrawCircle(float radius, string childName) {
// Create a new child GameObject to hold the LineRenderer Component // Create a new child GameObject to hold the LineRenderer Component
var child = new GameObject(childName); var child = new GameObject(childName);
@@ -245,8 +257,6 @@ public class Boid : MonoBehaviour {
child.transform.position = transform.position; child.transform.position = transform.position;
var line = child.AddComponent<LineRenderer>(); var line = child.AddComponent<LineRenderer>();
_parent.circleVertexCount = 360;
// Setup LineRenderer properties // Setup LineRenderer properties
line.useWorldSpace = false; line.useWorldSpace = false;
line.startWidth = _parent.circleWidth; line.startWidth = _parent.circleWidth;
@@ -264,4 +274,36 @@ public class Boid : MonoBehaviour {
// Add points to LineRenderer // Add points to LineRenderer
line.SetPositions(points); line.SetPositions(points);
} }
private void DrawArcCentered(float radius, float centerAngle, float angleWidth, string childName) {
DrawArc(radius, centerAngle - angleWidth / 2f, centerAngle + angleWidth / 2f, childName);
}
private void DrawArc(float radius, float from, float to, string childName) {
// Create a new child GameObject to hold the LineRenderer Component
var child = new GameObject(childName);
child.transform.SetParent(transform);
child.transform.position = transform.position;
var line = child.AddComponent<LineRenderer>();
int vertexCount = _parent.arcVertexCount != -1 ? _parent.arcVertexCount : (int) Mathf.Abs(from - to) * 2;
// Setup LineRenderer properties
line.useWorldSpace = false;
line.startWidth = _parent.circleWidth;
line.endWidth = _parent.circleWidth;
line.positionCount = vertexCount + 1;
// Calculate points for circle
var pointCount = vertexCount + 1;
var points = new Vector3[pointCount];
for (int i = 0; i < pointCount; i++) {
var rad = Mathf.Deg2Rad * Mathf.LerpAngle(from, to, i / (float) pointCount);
// var rad = Mathf.Deg2Rad * (i * 360f / _parent.circleVertexCount);
points[i] = new Vector3(Mathf.Sin(rad) * radius, Mathf.Cos(rad) * radius, 0);
}
// Add points to LineRenderer
line.SetPositions(points);
}
} }
+2 -1
View File
@@ -34,7 +34,8 @@ public class BoidController : MonoBehaviour {
[SerializeField] public float boundaryForce = 10f; // The force applied when a Boid hits the boundary [SerializeField] public float boundaryForce = 10f; // The force applied when a Boid hits the boundary
[SerializeField] public bool localFlocks = true; // Calculate Local 'Neighborhood' for flocks? [SerializeField] public bool localFlocks = true; // Calculate Local 'Neighborhood' for flocks?
[SerializeField] public bool edgeWrapping = true; // Enforce Edge Wrapping [SerializeField] public bool edgeWrapping = true; // Enforce Edge Wrapping
[SerializeField] public int circleVertexCount = 40; // The number of vertices for circles displayed [SerializeField] public int circleVertexCount = 360; // The number of vertices for circles displayed
[SerializeField] public int arcVertexCount = -1; // The number of vertices for arcs displayed, -1 for auto
[SerializeField] public float circleWidth = 0.1f; // Width of circle [SerializeField] public float circleWidth = 0.1f; // Width of circle
[SerializeField] public float maxSteerForce = 10f; [SerializeField] public float maxSteerForce = 10f;
[SerializeField] public float boidFOV = 160; [SerializeField] public float boidFOV = 160;