mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-06 15:14:26 -06:00
make GetRandomVelocity static and a Utility method, additional privilege fixes/method renames
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,6 +1,6 @@
|
|||||||
# Repository
|
# Repository
|
||||||
.idea/**
|
.idea/**
|
||||||
/procedural-placement/.idea/**
|
/Boids/.idea/**
|
||||||
|
|
||||||
# This .gitignore file should be placed at the root of your Unity project directory
|
# This .gitignore file should be placed at the root of your Unity project directory
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ Preset:
|
|||||||
value: 0.3
|
value: 0.3
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 0}
|
- target: {fileID: 0}
|
||||||
propertyPath: separationRange
|
propertyPath: globalBias
|
||||||
value: 2.82
|
value: 1
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 0}
|
- target: {fileID: 0}
|
||||||
propertyPath: separationBias
|
propertyPath: separationBias
|
||||||
@@ -57,268 +57,56 @@ Preset:
|
|||||||
propertyPath: cohesionBias
|
propertyPath: cohesionBias
|
||||||
value: 0.05
|
value: 0.05
|
||||||
objectReference: {fileID: 0}
|
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}
|
- target: {fileID: 0}
|
||||||
propertyPath: localFlocks
|
propertyPath: localFlocks
|
||||||
value: 1
|
value: 1
|
||||||
objectReference: {fileID: 0}
|
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}
|
- target: {fileID: 0}
|
||||||
propertyPath: boidObject
|
propertyPath: boidObject
|
||||||
value:
|
value:
|
||||||
objectReference: {fileID: 1737515784064720040, guid: 23e1eaaf69d4ef342ac3ef9590f6c642,
|
objectReference: {fileID: 1737515784064720040, guid: 23e1eaaf69d4ef342ac3ef9590f6c642,
|
||||||
type: 3}
|
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}
|
|
||||||
|
|||||||
@@ -18,21 +18,16 @@ public class Boid : MonoBehaviour {
|
|||||||
[NonSerialized] private BoidController _parent;
|
[NonSerialized] private BoidController _parent;
|
||||||
[NonSerialized] public bool isFocused = false;
|
[NonSerialized] public bool isFocused = false;
|
||||||
|
|
||||||
void Start() {
|
private void Start() {
|
||||||
_parent = transform.parent
|
_parent = transform.parent
|
||||||
.GetComponent<BoidController>(); // Parent used to perform physics math without caching
|
.GetComponent<BoidController>(); // Parent used to perform physics math without caching
|
||||||
_renderers = transform.GetComponents<Renderer>(); // Acquire Renderer(s) to check for Boid visibility
|
_renderers = transform.GetComponents<Renderer>(); // 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
|
_position = transform.position; // Track 2D position separately
|
||||||
transform.name = $"Boid {transform.GetSiblingIndex()}"; // Name the Game Object so Boids can be tracked somewhat
|
transform.name = $"Boid {transform.GetSiblingIndex()}"; // Name the Game Object so Boids can be tracked somewhat
|
||||||
}
|
}
|
||||||
|
|
||||||
// void OnDrawGizmos() {
|
private void Update() {
|
||||||
// var transform_ = transform;
|
|
||||||
// Handles.Label(transform_.position, $"{transform_.name} {_latestNeighborhoodCount}");
|
|
||||||
// }
|
|
||||||
|
|
||||||
void Update() {
|
|
||||||
// Updates the rotation of the object based on the Velocity
|
// Updates the rotation of the object based on the Velocity
|
||||||
transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * -Mathf.Atan2(_velocity.x, _velocity.y));
|
transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * -Mathf.Atan2(_velocity.x, _velocity.y));
|
||||||
|
|
||||||
@@ -105,12 +100,6 @@ public class Boid : MonoBehaviour {
|
|||||||
_centeringVelocity = Util.MaxVelocity(_parent.Space.center - _position, _parent.boidVelocityLimit / 2.0f);
|
_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
|
// Cohesion: Steer towards center of mass of flock
|
||||||
private Vector2 Rule1(List<Boid> flock) {
|
private Vector2 Rule1(List<Boid> flock) {
|
||||||
Vector2 center = Vector2.zero;
|
Vector2 center = Vector2.zero;
|
||||||
@@ -133,7 +122,7 @@ public class Boid : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Alignment: Steer to align with the average heading of the flock
|
// Alignment: Steer to align with the average heading of the flock
|
||||||
public Vector2 Rule3(List<Boid> flock) {
|
private Vector2 Rule3(List<Boid> flock) {
|
||||||
if (flock.Count == 0)
|
if (flock.Count == 0)
|
||||||
return Vector2.zero;
|
return Vector2.zero;
|
||||||
|
|
||||||
@@ -163,6 +152,7 @@ public class Boid : MonoBehaviour {
|
|||||||
else if (_position.y > _parent.Boundary.yMax)
|
else if (_position.y > _parent.Boundary.yMax)
|
||||||
vector.y = -_parent.boundaryForce *
|
vector.y = -_parent.boundaryForce *
|
||||||
Mathf.InverseLerp(_parent.Boundary.yMax, _parent.Space.yMax, _position.y);
|
Mathf.InverseLerp(_parent.Boundary.yMax, _parent.Space.yMax, _position.y);
|
||||||
|
|
||||||
return vector;
|
return vector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +162,7 @@ public class Boid : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sets up a Boid to be 'Focused', adds Circles around object and changes color
|
// Sets up a Boid to be 'Focused', adds Circles around object and changes color
|
||||||
public void enableFocusing() {
|
public void EnableFocusing() {
|
||||||
if (isFocused) {
|
if (isFocused) {
|
||||||
Debug.LogWarning("enableFocusing called on previously focused Boid");
|
Debug.LogWarning("enableFocusing called on previously focused Boid");
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class BoidController : MonoBehaviour {
|
|||||||
Gizmos.DrawWireCube(Cam.transform.position, new Vector3(screenWidth, screenHeight, 1));
|
Gizmos.DrawWireCube(Cam.transform.position, new Vector3(screenWidth, screenHeight, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update() {
|
private void Update() {
|
||||||
// Focus a different Boid
|
// Focus a different Boid
|
||||||
if (Input.GetKeyDown("space")) {
|
if (Input.GetKeyDown("space")) {
|
||||||
// Undo previous Boid's focus
|
// Undo previous Boid's focus
|
||||||
@@ -72,7 +72,7 @@ public class BoidController : MonoBehaviour {
|
|||||||
focusedBoid.DisableFocusing();
|
focusedBoid.DisableFocusing();
|
||||||
|
|
||||||
focusedBoid = boids[Random.Range(0, boids.Count)];
|
focusedBoid = boids[Random.Range(0, boids.Count)];
|
||||||
focusedBoid.enableFocusing();
|
focusedBoid.EnableFocusing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,12 @@ public class Util {
|
|||||||
return new Vector2((float) rx, (float) (v.x * sa + v.y * ca));
|
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) {
|
public static Vector2 MaxVelocity(Vector2 v, float max) {
|
||||||
if (v.sqrMagnitude > max * max)
|
if (v.sqrMagnitude > max * max)
|
||||||
v = (v / v.magnitude) * max;
|
v = (v / v.magnitude) * max;
|
||||||
|
|||||||
Reference in New Issue
Block a user