mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-12 14:11:00 -06:00
fixed wrapping, prototype 'working'
This commit is contained in:
@@ -1,71 +1,32 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Analytics;
|
using UnityEngine.Analytics;
|
||||||
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
// Boids are represented by a moving, rotating triangle.
|
// Boids are represented by a moving, rotating triangle.
|
||||||
// Boids should communicate with sibling Boids
|
// Boids should communicate with sibling Boids
|
||||||
public class Boid : MonoBehaviour {
|
public class Boid : MonoBehaviour {
|
||||||
public Vector2 position = Vector2.zero;
|
[NonSerialized] public Vector2 position = Vector2.zero;
|
||||||
public Vector2 velocity;
|
public Vector2 velocity;
|
||||||
private Renderer[] _renderers;
|
[NonSerialized] public Renderer[] renderers;
|
||||||
bool _isWrappingX = false;
|
[NonSerialized] public bool IsWrappingX = false;
|
||||||
bool _isWrappingY = false;
|
[NonSerialized] public bool IsWrappingY = false;
|
||||||
Camera _cam;
|
|
||||||
Vector3 _viewportPosition;
|
|
||||||
|
|
||||||
void Start() {
|
void Start() {
|
||||||
velocity = new Vector2(0.03f * Random.value < 0.5 ? 1 : -1, 0.03f * Random.value < 0.5 ? 1 : -1);
|
velocity = new Vector2(0.03f * Random.value < 0.5 ? 1 : -1, 0.03f * Random.value < 0.5 ? 1 : -1);
|
||||||
_renderers = GetComponents<Renderer>();
|
renderers = GetComponents<Renderer>();
|
||||||
_cam = Camera.main;
|
|
||||||
_viewportPosition = _cam.WorldToViewportPoint(transform.position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
||||||
ScreenWrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CheckRenderers() {
|
|
||||||
foreach (var renderer in _renderers) {
|
|
||||||
// If at least one render is visible, return true
|
|
||||||
if (renderer.isVisible) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, the object is invisible
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ScreenWrap() {
|
|
||||||
var isVisible = CheckRenderers();
|
|
||||||
|
|
||||||
if (isVisible) {
|
|
||||||
_isWrappingX = false;
|
|
||||||
_isWrappingY = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_isWrappingX && _isWrappingY) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var newPosition = transform.position;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 NextPosition(List<Boid> boids, float[] magnitudes) {
|
public Vector2 NextPosition(List<Boid> boids, float[] magnitudes) {
|
||||||
|
if (IsWrappingX || IsWrappingY)
|
||||||
|
return position + velocity;
|
||||||
|
|
||||||
// Acquires all
|
// Acquires all
|
||||||
List<Boid> flock = GetFlock(boids, 20);
|
List<Boid> flock = GetFlock(boids, 20);
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,48 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Debug = System.Diagnostics.Debug;
|
||||||
using Random = UnityEngine.Random;
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
public class BoidController : MonoBehaviour {
|
public class BoidController : MonoBehaviour {
|
||||||
// Controller Attributes
|
// Controller Attributes
|
||||||
public Rect space = new Rect(new Vector2(0, 0), new Vector2(24000, 14000));
|
public Rect space;
|
||||||
|
|
||||||
// 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;
|
||||||
|
|
||||||
// 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;
|
||||||
public float alignmentBias = 1.0f;
|
public float alignmentBias = 1.0f;
|
||||||
public float cohesionBias = 1.0f;
|
public float cohesionBias = 1.0f;
|
||||||
|
|
||||||
|
// Boid Object Prefab
|
||||||
public GameObject boidObject;
|
public GameObject boidObject;
|
||||||
|
// Boid Objects for Updates
|
||||||
public List<Boid> boids = new List<Boid>();
|
public List<Boid> boids = new List<Boid>();
|
||||||
|
// Used for wrapping
|
||||||
|
Camera _cam;
|
||||||
|
|
||||||
private void OnDrawGizmos() {
|
private void OnDrawGizmos() {
|
||||||
Gizmos.DrawWireCube(space.center, space.size);
|
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++) {
|
for (int i = 0; i < boidCount; i++) {
|
||||||
var position = new Vector2(Random.Range(-15, 15), Random.Range(-15, 15));
|
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;
|
boid.transform.parent = transform;
|
||||||
boids.Add(boid.GetComponent<Boid>());
|
boids.Add(boid.GetComponent<Boid>());
|
||||||
@@ -34,16 +50,44 @@ public class BoidController : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update() {
|
private void Update() {
|
||||||
// foreach (Boid boid in boids) {
|
// Wrapping Functionality
|
||||||
// if (!space.Contains(boid.position))
|
foreach (Boid boid in boids) {
|
||||||
// boid.position = transform.position;
|
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};
|
float[] magnitudes = {cohesionBias, separationBias, alignmentBias};
|
||||||
// Update all Boid positions
|
// Update all Boid positions
|
||||||
foreach (Boid boid in boids) {
|
foreach (Boid boid in boids) {
|
||||||
Vector2 next = boid.NextPosition(boids, magnitudes);
|
Vector2 next = boid.NextPosition(boids, magnitudes);
|
||||||
|
|
||||||
boid.position = next;
|
boid.position = next;
|
||||||
boid.transform.position = new Vector3(next.x, next.y, 0);
|
boid.transform.position = new Vector3(next.x, next.y, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user