mirror of
https://github.com/Xevion/Boids.git
synced 2026-01-31 02:23:36 -06:00
add separate draw (optional redraw) method, new drawArc and drawArcCenetered methods for FOV
This commit is contained in:
@@ -220,9 +220,7 @@ public class Boid : MonoBehaviour {
|
||||
var triangle = transform.GetComponent<Triangle>();
|
||||
triangle.meshRenderer.material.color = Color.red;
|
||||
|
||||
// Add a LineRenderer for Radius Drawing
|
||||
DrawCircle(_parent.boidSeparationRange, "Separation Range Circle");
|
||||
DrawCircle(_parent.boidGroupRange, "Group Range Circle");
|
||||
Draw();
|
||||
}
|
||||
|
||||
// Disable focusing, removing LineRenderers and resetting color
|
||||
@@ -238,6 +236,20 @@ public class Boid : MonoBehaviour {
|
||||
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) {
|
||||
// Create a new child GameObject to hold the LineRenderer Component
|
||||
var child = new GameObject(childName);
|
||||
@@ -245,8 +257,6 @@ public class Boid : MonoBehaviour {
|
||||
child.transform.position = transform.position;
|
||||
var line = child.AddComponent<LineRenderer>();
|
||||
|
||||
_parent.circleVertexCount = 360;
|
||||
|
||||
// Setup LineRenderer properties
|
||||
line.useWorldSpace = false;
|
||||
line.startWidth = _parent.circleWidth;
|
||||
@@ -264,4 +274,36 @@ public class Boid : MonoBehaviour {
|
||||
// Add points to LineRenderer
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,8 @@ public class BoidController : MonoBehaviour {
|
||||
[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 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 maxSteerForce = 10f;
|
||||
[SerializeField] public float boidFOV = 160;
|
||||
|
||||
Reference in New Issue
Block a user