diff --git a/Assets/Boid.cs b/Assets/Boid.cs index 9742c30..85d3f85 100644 --- a/Assets/Boid.cs +++ b/Assets/Boid.cs @@ -9,13 +9,14 @@ using Random = UnityEngine.Random; public class Boid : MonoBehaviour { [NonSerialized] public Vector2 position = Vector2.zero; public Vector2 velocity; - [NonSerialized] public Renderer[] renderers; [NonSerialized] public bool IsWrappingX = false; [NonSerialized] public bool IsWrappingY = false; + private BoidController parent; void Start() { - velocity = new Vector2(0.03f * Random.value < 0.5 ? 1 : -1, 0.03f * Random.value < 0.5 ? 1 : -1); - renderers = GetComponents(); + parent = transform.parent.GetComponent(); + // Acquire a Velocity Vector with a magnitude + velocity = GetRandomVelocity(parent.boidStartVelocity); } void Update() { @@ -24,29 +25,32 @@ public class Boid : MonoBehaviour { } public Vector2 NextPosition(List boids, float[] magnitudes) { + // Skip Flock Calculations if wrapping in progress if (IsWrappingX || IsWrappingY) return position + velocity; - // Acquires all - List flock = GetFlock(boids, 20); + // Acquires all Boids within the local flock + List flock = GetFlock(parent.boids, parent.boidGroupRange); if (flock.Count > 0) { // Calculate all offsets and multiple by magnitudes given - Vector2 r1 = Rule1(flock) * magnitudes[0]; - Vector2 r2 = Rule2(flock) * magnitudes[1]; - Vector2 r3 = Rule3(flock) * magnitudes[2]; + Vector2 r1 = Rule1(flock) * parent.cohesionBias; + Vector2 r2 = Rule2(flock) * parent.separationBias; + Vector2 r3 = Rule3(flock) * parent.alignmentBias; velocity += r1 + r2 + r3; } - LimitVelocity(); + // Limit the Velocity Vector to a certain Magnitude + if (velocity.magnitude > parent.boidVelocityLimit) { + velocity = (velocity / velocity.magnitude) * parent.boidVelocityLimit; + } return position + velocity; } - void LimitVelocity() { - if (velocity.magnitude > 2f) { - velocity = (velocity / velocity.magnitude) * 2f; - } + Vector2 GetRandomVelocity(float magnitude) { + Vector2 vector = new Vector2(magnitude, magnitude); + return Util.RotateBy(vector, Random.Range(0, 180)); } // Cohesion: Steer towards center of mass of flock diff --git a/Assets/BoidController.cs b/Assets/BoidController.cs index 4fce3bf..6b823aa 100644 --- a/Assets/BoidController.cs +++ b/Assets/BoidController.cs @@ -11,7 +11,8 @@ public class BoidController : MonoBehaviour { // Swarm Attributes public int boidCount = 50; public float boidGroupRange = 1.0f; - public float boidVelocity = 0.005f; + public float boidStartVelocity = 0.005f; + public float boidVelocityLimit = 2.0f; // Bias changes how different rules influence individual Boids more or less public float separationBias = 1.0f; @@ -59,16 +60,13 @@ public class BoidController : MonoBehaviour { // Activate Wrap, Move Vector2 newPosition = boid.transform.position; Vector3 viewportPosition = _cam.WorldToViewportPoint(newPosition); - print($"{boid.IsWrappingX} {boid.IsWrappingY} {viewportPosition}"); if (!boid.IsWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) { - print("WrappingX"); newPosition.x = -newPosition.x; boid.IsWrappingX = true; } if (!boid.IsWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) { - print("WrappingY"); newPosition.y = -newPosition.y; boid.IsWrappingY = true; }