131 lines
4.9 KiB
C#
131 lines
4.9 KiB
C#
using UnityEngine;
|
|
|
|
|
|
public class Boid : MonoBehaviour {
|
|
public float speed = 0.2f;
|
|
public int neighbourCount = 0;
|
|
public float inertia = 0.2f;
|
|
public float alignmentForce = 1.0f;
|
|
public float cohesionForce = 1.0f;
|
|
public float separationForce = 1.0f;
|
|
public float separationDistance = 0.5f;
|
|
public float bodyForce = 1;
|
|
|
|
public bool debug = false;
|
|
|
|
public SwarmControl sc;
|
|
public Vector3 velocity = Vector3.zero;
|
|
public Vector3 acceleration = Vector3.zero;
|
|
|
|
private Bounds bounds;
|
|
|
|
readonly Collider[] results = new Collider[10];
|
|
|
|
//public SensoryNeuroid[] neighbourSensor = new SensoryNeuroid[6];
|
|
public Perception perception;
|
|
|
|
public NeuroidNetwork neuroidNet = new();
|
|
public Neuroid bodyVector;
|
|
public Neuroid cohesion;
|
|
public Neuroid alignment;
|
|
public Neuroid separation;
|
|
// public Neuroid target;
|
|
public Neuroid boundary;
|
|
|
|
public Neuroid totalForce;
|
|
|
|
public int id;
|
|
|
|
void Awake() {
|
|
this.id = this.GetInstanceID();
|
|
|
|
sc = FindFirstObjectByType<SwarmControl>();
|
|
|
|
bounds = new(sc.transform.position, sc.spaceSize - 2 * sc.boundaryWidth);
|
|
|
|
perception = new Perception(neuroidNet);
|
|
|
|
cohesion = new(neuroidNet, "Cohesion");
|
|
perception.SendPositions(cohesion);
|
|
|
|
alignment = new(neuroidNet, "Alignment") { average = true };
|
|
perception.SendVelocities(alignment);
|
|
|
|
separation = new(neuroidNet, "Separation") { inverse = true, exponent = 2 };
|
|
perception.SendPositions(separation, sc.separationForce);
|
|
|
|
boundary = new(neuroidNet, "Boundary");
|
|
|
|
|
|
totalForce = new(neuroidNet, "Total");
|
|
totalForce.GetInputFrom(alignment, sc.alignmentForce);
|
|
totalForce.GetInputFrom(cohesion, sc.cohesionForce);
|
|
totalForce.GetInputFrom(separation, -sc.separationForce);
|
|
totalForce.GetInputFrom(boundary, sc.boundaryForce);
|
|
}
|
|
|
|
void Update() {
|
|
Physics.OverlapSphereNonAlloc(this.transform.position, sc.perceptionDistance, results);
|
|
foreach (Collider c in results) {
|
|
if (c == null)
|
|
continue;
|
|
|
|
if (c as CapsuleCollider != null) {
|
|
Boid neighbour = c.GetComponentInParent<Boid>();
|
|
if (neighbour == null || neighbour == this)
|
|
continue;
|
|
|
|
Vector3 localPosition = neighbour.transform.position - this.transform.position;
|
|
if (debug)
|
|
Debug.Log($" distance {localPosition.magnitude}");
|
|
|
|
int thingId = neighbour.GetInstanceID();
|
|
perception.ProcessStimulus(thingId, localPosition);
|
|
}
|
|
}
|
|
|
|
// if (!bounds.Contains(this.transform.position)) {
|
|
// Vector3 point = this.transform.position;
|
|
// // Vector3 distanceOutside = Vector3.Max(bounds.min - this.transform.position, this.transform.position - bounds.max);
|
|
// // // Ensure value is > 0 (but isn't this already)
|
|
// // Vector3 outside = distanceOutside; // Vector3.Max(Vector3.zero, distanceOutside);
|
|
|
|
// Vector3 below = bounds.min - point; // positive where point < min
|
|
// Vector3 above = point - bounds.max; // positive where point > max
|
|
|
|
// // outside distances per axis (0 if inside on that axis)
|
|
// Vector3 outside = Vector3.Max(Vector3.zero, Vector3.Max(below, above));
|
|
// float magnitude = outside.magnitude;
|
|
// Vector3 direction = (sc.transform.position - this.transform.position).normalized;
|
|
// outside = direction * magnitude;
|
|
|
|
// boundary.SetInput(id, outside, sc.boundaryForce, neuroidNet);
|
|
// // Debug.Log($"boundary {this.transform.position} {outside} force = {outside * sc.boundaryForce}");
|
|
// }
|
|
|
|
Vector3 totalForceVector = totalForce.outputValue;
|
|
//Debug.DrawRay(this.transform.position, totalForceVector, Color.magenta);
|
|
|
|
if (this.debug) {
|
|
Debug.Log($"Cohesion {cohesion.outputValue.magnitude} separation {separation.outputValue.magnitude} alignment {alignment.outputValue.magnitude}");
|
|
}
|
|
|
|
this.velocity = (1 - sc.inertia) * (totalForceVector * Time.deltaTime) + sc.inertia * velocity + (sc.speed * transform.forward);
|
|
//this.velocity = Vector3.ClampMagnitude(this.velocity, sc.speed);
|
|
|
|
this.transform.position += this.velocity * Time.deltaTime;
|
|
|
|
if (this.velocity != Vector3.zero) {
|
|
Quaternion targetRotation = Quaternion.LookRotation(this.velocity);
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2f); // Adjust the speed of rotation
|
|
}
|
|
|
|
//Debug.Log($"neighbours: {neighbourCount} synapses: {cohesion.synapses.Count}");
|
|
neuroidNet.Update();
|
|
}
|
|
|
|
void OnDrawGizmosSelected() {
|
|
Gizmos.DrawWireSphere(transform.position, sc.perceptionDistance);
|
|
}
|
|
}
|