mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-16 06:11:13 -06:00
general cleanup, fix BoidController GetComponent referencing/performance issues, readying for feature implementation
This commit is contained in:
@@ -9,13 +9,14 @@ using Random = UnityEngine.Random;
|
|||||||
public class Boid : MonoBehaviour {
|
public class Boid : MonoBehaviour {
|
||||||
[NonSerialized] public Vector2 position = Vector2.zero;
|
[NonSerialized] public Vector2 position = Vector2.zero;
|
||||||
public Vector2 velocity;
|
public Vector2 velocity;
|
||||||
[NonSerialized] public Renderer[] renderers;
|
|
||||||
[NonSerialized] public bool IsWrappingX = false;
|
[NonSerialized] public bool IsWrappingX = false;
|
||||||
[NonSerialized] public bool IsWrappingY = false;
|
[NonSerialized] public bool IsWrappingY = false;
|
||||||
|
private BoidController parent;
|
||||||
|
|
||||||
void Start() {
|
void Start() {
|
||||||
velocity = new Vector2(0.03f * Random.value < 0.5 ? 1 : -1, 0.03f * Random.value < 0.5 ? 1 : -1);
|
parent = transform.parent.GetComponent<BoidController>();
|
||||||
renderers = GetComponents<Renderer>();
|
// Acquire a Velocity Vector with a magnitude
|
||||||
|
velocity = GetRandomVelocity(parent.boidStartVelocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update() {
|
void Update() {
|
||||||
@@ -24,29 +25,32 @@ public class Boid : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 NextPosition(List<Boid> boids, float[] magnitudes) {
|
public Vector2 NextPosition(List<Boid> boids, float[] magnitudes) {
|
||||||
|
// Skip Flock Calculations if wrapping in progress
|
||||||
if (IsWrappingX || IsWrappingY)
|
if (IsWrappingX || IsWrappingY)
|
||||||
return position + velocity;
|
return position + velocity;
|
||||||
|
|
||||||
// Acquires all
|
// Acquires all Boids within the local flock
|
||||||
List<Boid> flock = GetFlock(boids, 20);
|
List<Boid> flock = GetFlock(parent.boids, parent.boidGroupRange);
|
||||||
|
|
||||||
if (flock.Count > 0) {
|
if (flock.Count > 0) {
|
||||||
// Calculate all offsets and multiple by magnitudes given
|
// Calculate all offsets and multiple by magnitudes given
|
||||||
Vector2 r1 = Rule1(flock) * magnitudes[0];
|
Vector2 r1 = Rule1(flock) * parent.cohesionBias;
|
||||||
Vector2 r2 = Rule2(flock) * magnitudes[1];
|
Vector2 r2 = Rule2(flock) * parent.separationBias;
|
||||||
Vector2 r3 = Rule3(flock) * magnitudes[2];
|
Vector2 r3 = Rule3(flock) * parent.alignmentBias;
|
||||||
velocity += r1 + r2 + r3;
|
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;
|
return position + velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LimitVelocity() {
|
Vector2 GetRandomVelocity(float magnitude) {
|
||||||
if (velocity.magnitude > 2f) {
|
Vector2 vector = new Vector2(magnitude, magnitude);
|
||||||
velocity = (velocity / velocity.magnitude) * 2f;
|
return Util.RotateBy(vector, Random.Range(0, 180));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cohesion: Steer towards center of mass of flock
|
// Cohesion: Steer towards center of mass of flock
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ public class BoidController : MonoBehaviour {
|
|||||||
// Swarm Attributes
|
// Swarm Attributes
|
||||||
public int boidCount = 50;
|
public int boidCount = 50;
|
||||||
public float boidGroupRange = 1.0f;
|
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
|
// Bias changes how different rules influence individual Boids more or less
|
||||||
public float separationBias = 1.0f;
|
public float separationBias = 1.0f;
|
||||||
@@ -59,16 +60,13 @@ public class BoidController : MonoBehaviour {
|
|||||||
// Activate Wrap, Move
|
// Activate Wrap, Move
|
||||||
Vector2 newPosition = boid.transform.position;
|
Vector2 newPosition = boid.transform.position;
|
||||||
Vector3 viewportPosition = _cam.WorldToViewportPoint(newPosition);
|
Vector3 viewportPosition = _cam.WorldToViewportPoint(newPosition);
|
||||||
print($"{boid.IsWrappingX} {boid.IsWrappingY} {viewportPosition}");
|
|
||||||
|
|
||||||
if (!boid.IsWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) {
|
if (!boid.IsWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) {
|
||||||
print("WrappingX");
|
|
||||||
newPosition.x = -newPosition.x;
|
newPosition.x = -newPosition.x;
|
||||||
boid.IsWrappingX = true;
|
boid.IsWrappingX = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!boid.IsWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) {
|
if (!boid.IsWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) {
|
||||||
print("WrappingY");
|
|
||||||
newPosition.y = -newPosition.y;
|
newPosition.y = -newPosition.y;
|
||||||
boid.IsWrappingY = true;
|
boid.IsWrappingY = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user