diff --git a/.gitignore b/.gitignore index f5f7cc7..681361a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Repository .idea/** -/procedural-placement/.idea/** +/Boids/.idea/** # This .gitignore file should be placed at the root of your Unity project directory # diff --git a/Boids/Assets/Resources/BoidController.preset b/Boids/Assets/Resources/BoidController.preset index 07b41c9..2958b28 100644 --- a/Boids/Assets/Resources/BoidController.preset +++ b/Boids/Assets/Resources/BoidController.preset @@ -42,8 +42,8 @@ Preset: value: 0.3 objectReference: {fileID: 0} - target: {fileID: 0} - propertyPath: separationRange - value: 2.82 + propertyPath: globalBias + value: 1 objectReference: {fileID: 0} - target: {fileID: 0} propertyPath: separationBias @@ -57,268 +57,56 @@ Preset: propertyPath: cohesionBias value: 0.05 objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: boundaryBias + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: enableSeparation + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: enableAlignment + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: enableCohesion + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: enableBoundary + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: boidSeparationRange + value: 2.3 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: boundaryForce + value: 1 + objectReference: {fileID: 0} - target: {fileID: 0} propertyPath: localFlocks value: 1 objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: edgeWrapping + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: circleVertexCount + value: 40 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: circleWidth + value: 0.1 + objectReference: {fileID: 0} + - target: {fileID: 0} + propertyPath: focusedBoid + value: + objectReference: {fileID: 0} - target: {fileID: 0} propertyPath: boidObject value: objectReference: {fileID: 1737515784064720040, guid: 23e1eaaf69d4ef342ac3ef9590f6c642, type: 3} - - target: {fileID: 0} - propertyPath: boids.Array.size - value: 62 - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[0] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[1] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[2] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[3] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[4] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[5] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[6] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[7] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[8] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[9] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[10] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[11] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[12] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[13] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[14] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[15] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[16] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[17] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[18] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[19] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[20] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[21] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[22] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[23] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[24] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[25] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[26] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[27] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[28] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[29] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[30] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[31] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[32] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[33] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[34] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[35] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[36] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[37] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[38] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[39] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[40] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[41] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[42] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[43] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[44] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[45] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[46] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[47] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[48] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[49] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[50] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[51] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[52] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[53] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[54] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[55] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[56] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[57] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[58] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[59] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[60] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: boids.Array.data[61] - value: - objectReference: {fileID: 0} - - target: {fileID: 0} - propertyPath: cam - value: - objectReference: {fileID: 0} diff --git a/Boids/Assets/Scripts/Boid.cs b/Boids/Assets/Scripts/Boid.cs index b34f601..63a3c22 100644 --- a/Boids/Assets/Scripts/Boid.cs +++ b/Boids/Assets/Scripts/Boid.cs @@ -18,21 +18,16 @@ public class Boid : MonoBehaviour { [NonSerialized] private BoidController _parent; [NonSerialized] public bool isFocused = false; - void Start() { + private void Start() { _parent = transform.parent .GetComponent(); // Parent used to perform physics math without caching _renderers = transform.GetComponents(); // Acquire Renderer(s) to check for Boid visibility - _velocity = GetRandomVelocity(_parent.boidStartVelocity); // Acquire a Velocity Vector with a magnitude + _velocity = Util.GetRandomVelocity(_parent.boidStartVelocity); // Acquire a Velocity Vector with a magnitude _position = transform.position; // Track 2D position separately transform.name = $"Boid {transform.GetSiblingIndex()}"; // Name the Game Object so Boids can be tracked somewhat } - // void OnDrawGizmos() { - // var transform_ = transform; - // Handles.Label(transform_.position, $"{transform_.name} {_latestNeighborhoodCount}"); - // } - - void Update() { + private 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)); @@ -56,7 +51,7 @@ public class Boid : MonoBehaviour { _velocity += Rule3(flock) * _parent.alignmentBias; } - if(_parent.enableBoundary && _parent.Boundary.Contains(_position)) + if (_parent.enableBoundary && _parent.Boundary.Contains(_position)) _velocity += RuleBound() * _parent.boundaryBias; // Limit the Velocity Vector to a certain Magnitude @@ -105,12 +100,6 @@ public class Boid : MonoBehaviour { _centeringVelocity = Util.MaxVelocity(_parent.Space.center - _position, _parent.boidVelocityLimit / 2.0f); } - // Returns a velocity (Vector2) at a random angle with a specific overall magnitude - private 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 private Vector2 Rule1(List flock) { Vector2 center = Vector2.zero; @@ -133,7 +122,7 @@ public class Boid : MonoBehaviour { } // Alignment: Steer to align with the average heading of the flock - public Vector2 Rule3(List flock) { + private Vector2 Rule3(List flock) { if (flock.Count == 0) return Vector2.zero; @@ -163,6 +152,7 @@ public class Boid : MonoBehaviour { else if (_position.y > _parent.Boundary.yMax) vector.y = -_parent.boundaryForce * Mathf.InverseLerp(_parent.Boundary.yMax, _parent.Space.yMax, _position.y); + return vector; } @@ -172,7 +162,7 @@ public class Boid : MonoBehaviour { } // Sets up a Boid to be 'Focused', adds Circles around object and changes color - public void enableFocusing() { + public void EnableFocusing() { if (isFocused) { Debug.LogWarning("enableFocusing called on previously focused Boid"); return; diff --git a/Boids/Assets/Scripts/BoidController.cs b/Boids/Assets/Scripts/BoidController.cs index f086a89..482d4d9 100644 --- a/Boids/Assets/Scripts/BoidController.cs +++ b/Boids/Assets/Scripts/BoidController.cs @@ -64,7 +64,7 @@ public class BoidController : MonoBehaviour { Gizmos.DrawWireCube(Cam.transform.position, new Vector3(screenWidth, screenHeight, 1)); } - void Update() { + private void Update() { // Focus a different Boid if (Input.GetKeyDown("space")) { // Undo previous Boid's focus @@ -72,7 +72,7 @@ public class BoidController : MonoBehaviour { focusedBoid.DisableFocusing(); focusedBoid = boids[Random.Range(0, boids.Count)]; - focusedBoid.enableFocusing(); + focusedBoid.EnableFocusing(); } } diff --git a/Boids/Assets/Scripts/Util.cs b/Boids/Assets/Scripts/Util.cs index eef22e4..749cacf 100644 --- a/Boids/Assets/Scripts/Util.cs +++ b/Boids/Assets/Scripts/Util.cs @@ -8,6 +8,12 @@ public class Util { return new Vector2((float) rx, (float) (v.x * sa + v.y * ca)); } + + // Returns a velocity (Vector2) at a random angle with a specific overall magnitude + public static Vector2 GetRandomVelocity(float magnitude) { + var vector = new Vector2(magnitude, magnitude); + return Util.RotateBy(vector, Random.Range(0, 180)); + } public static Vector2 MaxVelocity(Vector2 v, float max) { if (v.sqrMagnitude > max * max)