110 lines
4.6 KiB
C#

using UnityEngine;
//[RequireComponent(typeof(NanoBrain))]
[RequireComponent(typeof(NanoBrainComponent))]
public class Boid : MonoBehaviour {
public static int BoundaryType = 1;
public static int BoidType = 2;
public static int BoidVelocityType =3;
public SwarmControl sc;
public Vector3 velocity = Vector3.zero;
public Vector3 acceleration = Vector3.zero;
private Bounds innerBounds;
public NanoBrainComponent nanoBrain;
public Receptor boundaryReceptor;
public Receptor boidReceptor;
public Receptor boidVelocityReceptor;
public int id;
public Material red;
public Material gray;
void Awake() {
this.id = this.GetInstanceID();
nanoBrain = GetComponent<NanoBrainComponent>();
// boundaryReceptor = Receptor.GetReceptor(nanoBrain.brain, BoundaryType);
// boidReceptor = Receptor.GetReceptor(nanoBrain.brain, BoidType);
// boidVelocityReceptor = Receptor.GetReceptor(nanoBrain.brain, BoidVelocityType);
boundaryReceptor = Receptor.CreateReceptor(nanoBrain.brain, "Avoidance");
boundaryReceptor.name = "Boundary";
boundaryReceptor.receivers[0].synapses[0].weight = -1;
boidReceptor = Receptor.CreateReceptor(nanoBrain.brain, "Boid");
boidVelocityReceptor = Receptor.CreateReceptor(nanoBrain.brain, "Alignment");
sc = FindFirstObjectByType<SwarmControl>();
innerBounds = new(sc.transform.position, sc.spaceSize - 2 * sc.boundaryWidth);
}
void Update() {
Collider[] results = Physics.OverlapSphere(this.transform.position, sc.perceptionDistance);
foreach (Collider c in results) {
if (c as CapsuleCollider != null) {
Boid neighbour = c.GetComponentInParent<Boid>();
if (neighbour == null || neighbour == this)
continue;
int thingId = neighbour.GetInstanceID();
Vector3 localPosition = this.transform.InverseTransformPoint(neighbour.transform.position);
float d = localPosition.magnitude;
if (d <= sc.separationDistance)
localPosition = localPosition.normalized * 0.01f;
else
localPosition = localPosition.normalized * (localPosition.magnitude - sc.separationDistance);
if (localPosition.sqrMagnitude > 0)
boidReceptor?.ProcessStimulus(thingId, localPosition, neighbour.name);
Vector3 localVelocity = this.transform.InverseTransformVector(neighbour.velocity);
if (localVelocity.sqrMagnitude > 0)
boidVelocityReceptor?.ProcessStimulus(thingId, localVelocity);
}
}
if (!innerBounds.Contains(this.transform.position)) {
Vector3 point = this.transform.position;
Vector3 pointOnBounds = innerBounds.ClosestPoint(point);
Vector3 desiredWorldSpace = (pointOnBounds - point).normalized * sc.speed;
Vector3 desiredLocalSpace = -this.transform.InverseTransformPoint(desiredWorldSpace);
boundaryReceptor.ProcessStimulus(777, desiredLocalSpace);
}
Vector3 worldForce = this.transform.TransformDirection(nanoBrain.root.outputValue);
this.velocity = (1 - sc.inertia) * (worldForce * Time.deltaTime) + sc.inertia * velocity;
if (this.velocity.magnitude > 0)
this.velocity = this.velocity.normalized * sc.speed;
else
this.velocity = this.transform.forward * sc.speed;
//Debug.DrawRay(this.transform.position, this.velocity, Color.blue);
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 * 5f); // Adjust the speed of rotation
}
nanoBrain.brain.UpdateNuclei();
// Renderer renderer = GetComponentInChildren<Renderer>();
// results = Physics.OverlapSphere(this.transform.position, 0.1f);
// if (results.Length > 1) {
// // string s= this.name;
// // foreach (Collider c in results)
// // s += " " + c.transform.parent.gameObject.name;
// // Debug.Log(s);
// renderer.sharedMaterial = red;
// }
// else {
// renderer.sharedMaterial = gray;
// }
}
}