mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-13 00:11:01 -06:00
fixed wrapping, prototype 'working'
This commit is contained in:
@@ -1,49 +1,93 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Debug = System.Diagnostics.Debug;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
public class BoidController : MonoBehaviour {
|
||||
// Controller Attributes
|
||||
public Rect space = new Rect(new Vector2(0, 0), new Vector2(24000, 14000));
|
||||
public Rect space;
|
||||
|
||||
// Swarm Attributes
|
||||
public int boidCount = 50;
|
||||
public float boidGroupRange = 1.0f;
|
||||
public float boidVelocity = 0.005f;
|
||||
|
||||
// Bias changes how different rules influence individual Boids more or less
|
||||
public float separationBias = 1.0f;
|
||||
public float alignmentBias = 1.0f;
|
||||
public float cohesionBias = 1.0f;
|
||||
|
||||
// Boid Object Prefab
|
||||
public GameObject boidObject;
|
||||
|
||||
// Boid Objects for Updates
|
||||
public List<Boid> boids = new List<Boid>();
|
||||
// Used for wrapping
|
||||
Camera _cam;
|
||||
|
||||
private void OnDrawGizmos() {
|
||||
Gizmos.DrawWireCube(space.center, space.size);
|
||||
}
|
||||
|
||||
void Start() {
|
||||
private void Start() {
|
||||
// Setup Camera
|
||||
_cam = Camera.main;
|
||||
|
||||
var size = new Vector2(142, 80);
|
||||
space = new Rect((Vector2) transform.position - (size / 2), size);
|
||||
|
||||
// Size the Rectangle
|
||||
_cam.rect = space;
|
||||
|
||||
// Add in Boid Objects / Spawn Boid Prefabs
|
||||
for (int i = 0; i < boidCount; i++) {
|
||||
var position = new Vector2(Random.Range(-15, 15), Random.Range(-15, 15));
|
||||
var boid = Instantiate(boidObject, position, Quaternion.identity);
|
||||
|
||||
GameObject boid = Instantiate(boidObject, position, Quaternion.identity);
|
||||
|
||||
boid.transform.parent = transform;
|
||||
boids.Add(boid.GetComponent<Boid>());
|
||||
boids[boids.Count - 1].position = position;
|
||||
}
|
||||
}
|
||||
|
||||
void Update() {
|
||||
// foreach (Boid boid in boids) {
|
||||
// if (!space.Contains(boid.position))
|
||||
// boid.position = transform.position;
|
||||
// }
|
||||
|
||||
private void Update() {
|
||||
// Wrapping Functionality
|
||||
foreach (Boid boid in boids) {
|
||||
print($"{boid.IsWrappingX} {boid.IsWrappingY}");
|
||||
|
||||
if (!space.Contains(boid.position)) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
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