mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-09 22:06:31 -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 {
|
||||
[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<Renderer>();
|
||||
parent = transform.parent.GetComponent<BoidController>();
|
||||
// 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<Boid> boids, float[] magnitudes) {
|
||||
// Skip Flock Calculations if wrapping in progress
|
||||
if (IsWrappingX || IsWrappingY)
|
||||
return position + velocity;
|
||||
|
||||
// Acquires all
|
||||
List<Boid> flock = GetFlock(boids, 20);
|
||||
// Acquires all Boids within the local flock
|
||||
List<Boid> 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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user