mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-09 22:06:31 -06:00
hide boidlist in inspector, fix ortho camera/rect implementation properly, move Boid movement functionality into Boid Update, get rid of NextUpdate
This commit is contained in:
@@ -21,16 +21,17 @@ public class Boid : MonoBehaviour {
|
|||||||
void Update() {
|
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));
|
||||||
}
|
|
||||||
|
|
||||||
public Vector2 NextPosition(List<Boid> boids, float[] magnitudes) {
|
|
||||||
// Skip Flock Calculations if wrapping in progress
|
// Skip Flock Calculations if wrapping in progress
|
||||||
if (IsWrappingX || IsWrappingY)
|
if (IsWrappingX || IsWrappingY) {
|
||||||
return position + velocity;
|
position += velocity;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Acquires all Boids within the local flock
|
// Acquires all Boids within the local flock
|
||||||
List<Boid> flock = GetFlock(parent.boids, parent.boidGroupRange);
|
// List<Boid> flock = GetFlock(parent.boids, parent.boidGroupRange);
|
||||||
|
List<Boid> flock = parent.boids;
|
||||||
|
|
||||||
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) * parent.cohesionBias;
|
Vector2 r1 = Rule1(flock) * parent.cohesionBias;
|
||||||
@@ -44,7 +45,36 @@ public class Boid : MonoBehaviour {
|
|||||||
velocity = (velocity / velocity.magnitude) * parent.boidVelocityLimit;
|
velocity = (velocity / velocity.magnitude) * parent.boidVelocityLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
return position + velocity;
|
position += velocity;
|
||||||
|
transform.position = new Vector3(position.x, position.y, 0);
|
||||||
|
|
||||||
|
Wrapping();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Wrapping() {
|
||||||
|
if (!parent.space.Contains(position)) {
|
||||||
|
// Activate Wrap, Move
|
||||||
|
Vector2 newPosition = transform.position;
|
||||||
|
Vector3 viewportPosition = parent._cam.WorldToViewportPoint(newPosition);
|
||||||
|
|
||||||
|
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;
|
||||||
|
position = newPosition;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Within the rectangle again
|
||||||
|
IsWrappingX = false;
|
||||||
|
IsWrappingY = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 GetRandomVelocity(float magnitude) {
|
Vector2 GetRandomVelocity(float magnitude) {
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ public class BoidController : MonoBehaviour {
|
|||||||
public GameObject boidObject;
|
public GameObject boidObject;
|
||||||
|
|
||||||
// Boid Objects for Updates
|
// Boid Objects for Updates
|
||||||
public List<Boid> boids = new List<Boid>();
|
[HideInInspector] public List<Boid> boids = new List<Boid>();
|
||||||
|
|
||||||
// Used for wrapping
|
// Used for wrapping
|
||||||
Camera _cam;
|
internal Camera _cam;
|
||||||
|
|
||||||
private void OnDrawGizmos() {
|
private void OnDrawGizmos() {
|
||||||
Gizmos.DrawWireCube(space.center, space.size);
|
Gizmos.DrawWireCube(space.center, space.size);
|
||||||
@@ -38,8 +38,8 @@ public class BoidController : MonoBehaviour {
|
|||||||
|
|
||||||
// Size the Rectangle based on the Camera's Orthographic View
|
// Size the Rectangle based on the Camera's Orthographic View
|
||||||
float height = 2f * _cam.orthographicSize;
|
float height = 2f * _cam.orthographicSize;
|
||||||
float width = height * _cam.aspect;
|
Vector2 size = new Vector2(height * _cam.aspect, height);
|
||||||
space = new Rect(transform.position, new Vector2(width, height));
|
space = new Rect((Vector2) transform.position - size / 2, size);
|
||||||
|
|
||||||
// Add in Boid Objects / Spawn Boid Prefabs
|
// Add in Boid Objects / Spawn Boid Prefabs
|
||||||
for (int i = 0; i < boidCount; i++) {
|
for (int i = 0; i < boidCount; i++) {
|
||||||
@@ -53,42 +53,4 @@ public class BoidController : MonoBehaviour {
|
|||||||
boids[boids.Count - 1].position = position;
|
boids[boids.Count - 1].position = position;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update() {
|
|
||||||
// Wrapping Functionality
|
|
||||||
foreach (Boid boid in boids) {
|
|
||||||
if (!space.Contains(boid.position)) {
|
|
||||||
// Activate Wrap, Move
|
|
||||||
Vector2 newPosition = boid.transform.position;
|
|
||||||
Vector3 viewportPosition = _cam.WorldToViewportPoint(newPosition);
|
|
||||||
|
|
||||||
if (!boid.IsWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) {
|
|
||||||
newPosition.x = -newPosition.x;
|
|
||||||
boid.IsWrappingX = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!boid.IsWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user