diff --git a/Assets/Boid.cs b/Assets/Boid.cs index b6b7b39..9742c30 100644 --- a/Assets/Boid.cs +++ b/Assets/Boid.cs @@ -1,71 +1,32 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using UnityEngine; using UnityEngine.Analytics; +using Random = UnityEngine.Random; // Boids are represented by a moving, rotating triangle. // Boids should communicate with sibling Boids public class Boid : MonoBehaviour { - public Vector2 position = Vector2.zero; + [NonSerialized] public Vector2 position = Vector2.zero; public Vector2 velocity; - private Renderer[] _renderers; - bool _isWrappingX = false; - bool _isWrappingY = false; - Camera _cam; - Vector3 _viewportPosition; + [NonSerialized] public Renderer[] renderers; + [NonSerialized] public bool IsWrappingX = false; + [NonSerialized] public bool IsWrappingY = false; void Start() { velocity = new Vector2(0.03f * Random.value < 0.5 ? 1 : -1, 0.03f * Random.value < 0.5 ? 1 : -1); - _renderers = GetComponents(); - _cam = Camera.main; - _viewportPosition = _cam.WorldToViewportPoint(transform.position); + renderers = GetComponents(); } void Update() { // Updates the rotation of the object based on the Velocity transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * -Mathf.Atan2(velocity.x, velocity.y)); - ScreenWrap(); } - - bool CheckRenderers() { - foreach (var renderer in _renderers) { - // If at least one render is visible, return true - if (renderer.isVisible) { - return true; - } - } - - // Otherwise, the object is invisible - return false; - } - - void ScreenWrap() { - var isVisible = CheckRenderers(); - - if (isVisible) { - _isWrappingX = false; - _isWrappingY = false; - return; - } - - if (_isWrappingX && _isWrappingY) { - return; - } - var newPosition = transform.position; - - if (!_isWrappingX && (_viewportPosition.x > 1 || _viewportPosition.x < 0)) { - newPosition.x = -newPosition.x; - _isWrappingX = true; - } - - if (!_isWrappingY && (_viewportPosition.y > 1 || _viewportPosition.y < 0)) { - newPosition.y = -newPosition.y; - _isWrappingY = true; - } - - transform.position = newPosition; - } - + public Vector2 NextPosition(List boids, float[] magnitudes) { + if (IsWrappingX || IsWrappingY) + return position + velocity; + // Acquires all List flock = GetFlock(boids, 20); diff --git a/Assets/BoidController.cs b/Assets/BoidController.cs index 275f68c..4fce3bf 100644 --- a/Assets/BoidController.cs +++ b/Assets/BoidController.cs @@ -1,49 +1,93 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using UnityEngine; +using Debug = System.Diagnostics.Debug; using Random = UnityEngine.Random; public class BoidController : MonoBehaviour { // Controller Attributes - public Rect space = new Rect(new Vector2(0, 0), new Vector2(24000, 14000)); + public Rect space; // Swarm Attributes public int boidCount = 50; public float boidGroupRange = 1.0f; + public float boidVelocity = 0.005f; // Bias changes how different rules influence individual Boids more or less public float separationBias = 1.0f; public float alignmentBias = 1.0f; public float cohesionBias = 1.0f; + // Boid Object Prefab public GameObject boidObject; - + // Boid Objects for Updates public List boids = new List(); + // Used for wrapping + Camera _cam; private void OnDrawGizmos() { Gizmos.DrawWireCube(space.center, space.size); } - void Start() { + private void Start() { + // Setup Camera + _cam = Camera.main; + + var size = new Vector2(142, 80); + space = new Rect((Vector2) transform.position - (size / 2), size); + + // Size the Rectangle + _cam.rect = space; + + // Add in Boid Objects / Spawn Boid Prefabs for (int i = 0; i < boidCount; i++) { var position = new Vector2(Random.Range(-15, 15), Random.Range(-15, 15)); - var boid = Instantiate(boidObject, position, Quaternion.identity); - + GameObject boid = Instantiate(boidObject, position, Quaternion.identity); + boid.transform.parent = transform; boids.Add(boid.GetComponent()); boids[boids.Count - 1].position = position; } } - void Update() { - // foreach (Boid boid in boids) { - // if (!space.Contains(boid.position)) - // boid.position = transform.position; - // } - + private void Update() { + // Wrapping Functionality + foreach (Boid boid in boids) { + print($"{boid.IsWrappingX} {boid.IsWrappingY}"); + + if (!space.Contains(boid.position)) { + // 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; + } + + boid.transform.position = newPosition; + boid.position = newPosition; + } + else { + // Within the rectangle again + boid.IsWrappingX = false; + boid.IsWrappingY = false; + } + } + float[] magnitudes = {cohesionBias, separationBias, alignmentBias}; // Update all Boid positions foreach (Boid boid in boids) { Vector2 next = boid.NextPosition(boids, magnitudes); + boid.position = next; boid.transform.position = new Vector3(next.x, next.y, 0); }