mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-11 08:06:37 -06:00
implement seblague's acceleration based formulas, requires tweaking
This commit is contained in:
@@ -38,24 +38,29 @@ public class Boid : MonoBehaviour {
|
||||
transform.position = _position;
|
||||
}
|
||||
else {
|
||||
Vector2 acceleration = Vector2.zero;
|
||||
List<Boid> flock = _parent.localFlocks ? GetFlock(_parent.boids, _parent.boidGroupRange) : _parent.boids;
|
||||
_latestNeighborhoodCount = flock.Count;
|
||||
|
||||
// Calculate all offsets and multiple by magnitudes given
|
||||
if (flock.Count > 0) {
|
||||
if (_parent.enableCohesion)
|
||||
_velocity += Rule1(flock) * _parent.cohesionBias;
|
||||
acceleration += SteerTowards(Rule1(flock)) * _parent.cohesionBias;
|
||||
if (_parent.enableSeparation)
|
||||
_velocity += Rule2(flock) * _parent.separationBias;
|
||||
acceleration += SteerTowards(Rule2(flock)) * _parent.separationBias;
|
||||
if (_parent.enableAlignment)
|
||||
_velocity += Rule3(flock) * _parent.alignmentBias;
|
||||
acceleration += SteerTowards(Rule3(flock)) * _parent.alignmentBias;
|
||||
}
|
||||
|
||||
if (_parent.enableBoundary && _parent.Boundary.Contains(_position))
|
||||
_velocity += RuleBound() * _parent.boundaryBias;
|
||||
acceleration += SteerTowards(RuleBound()) * _parent.boundaryBias;
|
||||
|
||||
// Limit the Velocity Vector to a certain Magnitude
|
||||
_velocity = Util.MaxVelocity(_velocity, _parent.boidVelocityLimit);
|
||||
_velocity += acceleration;
|
||||
float speed = _velocity.magnitude;
|
||||
Vector2 dir = _velocity / speed;
|
||||
speed = Mathf.Clamp(speed, _parent.minSpeed, _parent.maxSpeed);
|
||||
_velocity = dir * speed;
|
||||
|
||||
_position += _velocity;
|
||||
transform.position = new Vector3(_position.x, _position.y, 0);
|
||||
@@ -65,6 +70,11 @@ public class Boid : MonoBehaviour {
|
||||
Wrapping();
|
||||
}
|
||||
|
||||
private Vector2 SteerTowards(Vector2 vector) {
|
||||
Vector2 v = vector.normalized * _parent.maxSpeed - _velocity;
|
||||
return Vector2.ClampMagnitude(v, _parent.maxSteerForce);
|
||||
}
|
||||
|
||||
private void Wrapping() {
|
||||
if (!_parent.Space.Contains(_position)) {
|
||||
// Activate Wrap, Move
|
||||
@@ -95,9 +105,9 @@ public class Boid : MonoBehaviour {
|
||||
|
||||
// When Wrapping, this Velocity directs the Boid to the center of the Rectangle
|
||||
private void UpdateCenteringVelocity() {
|
||||
_centeringVelocity = Util.RotateBy(new Vector2(_parent.boidVelocityLimit, _parent.boidVelocityLimit),
|
||||
_centeringVelocity = Util.RotateBy(new Vector2(_parent.maxSpeed, _parent.maxSpeed),
|
||||
Vector2.Angle(_position, _parent.Space.center));
|
||||
_centeringVelocity = Util.MaxVelocity(_parent.Space.center - _position, _parent.boidVelocityLimit / 2.0f);
|
||||
_centeringVelocity = Util.MaxVelocity(_parent.Space.center - _position, _parent.maxSpeed / 2.0f);
|
||||
}
|
||||
|
||||
// Cohesion: Steer towards center of mass of flock
|
||||
|
||||
@@ -11,10 +11,11 @@ public class BoidController : MonoBehaviour {
|
||||
[NonSerialized] public Rect Boundary;
|
||||
|
||||
// Swarm Attributes
|
||||
public int boidCount = 50;
|
||||
public float boidGroupRange = 1.0f;
|
||||
public float boidStartVelocity = 0.005f;
|
||||
[SerializeField] public float boidVelocityLimit = 1.0f;
|
||||
[SerializeField] public int boidCount = 50;
|
||||
[SerializeField] public float boidGroupRange = 1.0f;
|
||||
[SerializeField] public float boidStartVelocity = 0.005f;
|
||||
[SerializeField] public float minSpeed;
|
||||
[SerializeField] public float maxSpeed;
|
||||
|
||||
// Boid Rules are multiplied by this to allow rule 'tweaking'
|
||||
[SerializeField] public float globalBias = 1.0f;
|
||||
@@ -35,6 +36,7 @@ public class BoidController : MonoBehaviour {
|
||||
[SerializeField] public bool edgeWrapping = true; // Enforce Edge Wrapping
|
||||
[SerializeField] public int circleVertexCount = 40; // The number of vertices for circles displayed
|
||||
[SerializeField] public float circleWidth = 0.1f; // Width of circle
|
||||
[SerializeField] public float maxSteerForce = 1f;
|
||||
|
||||
|
||||
public Boid focusedBoid; // A focused Boid has special rendering
|
||||
|
||||
@@ -22,7 +22,8 @@ public class BoidControllerEditor : Editor {
|
||||
// Basic Boid Controller Attributes
|
||||
controller.boidGroupRange = EditorGUILayout.Slider("Group Range", controller.boidGroupRange, 0.01f, 7.5f);
|
||||
controller.boidStartVelocity = EditorGUILayout.Slider("Start Velocity", controller.boidStartVelocity, 0.01f, 5.0f);
|
||||
controller.boidVelocityLimit = EditorGUILayout.Slider("Max Velocity", controller.boidVelocityLimit, 0.01f, 5.0f);
|
||||
controller.minSpeed = EditorGUILayout.Slider("Minimum Speed", controller.minSpeed, 0.01f, 5.0f);
|
||||
controller.maxSpeed = EditorGUILayout.Slider("Maximum Speed", controller.maxSpeed, 0.01f, 5.0f);
|
||||
controller.boidSeparationRange = EditorGUILayout.Slider("Separation Range", controller.boidSeparationRange, 0.01f, 5.0f);
|
||||
controller.boundaryForce = EditorGUILayout.Slider("Boundary Force", controller.boundaryForce, 0.25f, 1f);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user