mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-06 01:14:23 -06:00
adjust boundary forces and boundary dimensions
This commit is contained in:
@@ -9,7 +9,7 @@ public class BoidControllerEditor : UnityEditor.Editor {
|
|||||||
|
|
||||||
// Boid Count update
|
// Boid Count update
|
||||||
EditorGUI.BeginChangeCheck();
|
EditorGUI.BeginChangeCheck();
|
||||||
controller.boidCount = EditorGUILayout.IntSlider("Boid Count", controller.boidCount, -1, 500);
|
controller.boidCount = EditorGUILayout.IntSlider("Boid Count", controller.boidCount, 0, 500);
|
||||||
// Check must be performed or Boids will be added outside of gameplay
|
// Check must be performed or Boids will be added outside of gameplay
|
||||||
if (EditorGUI.EndChangeCheck() && Application.isPlaying) {
|
if (EditorGUI.EndChangeCheck() && Application.isPlaying) {
|
||||||
int diff = controller.boidCount - controller.boids.Count;
|
int diff = controller.boidCount - controller.boids.Count;
|
||||||
@@ -23,7 +23,7 @@ public class BoidControllerEditor : UnityEditor.Editor {
|
|||||||
EditorGUILayout.MinMaxSlider("Speed Limit", ref controller.minSpeed, ref controller.maxSpeed, 0.01f, 25f);
|
EditorGUILayout.MinMaxSlider("Speed Limit", ref controller.minSpeed, ref controller.maxSpeed, 0.01f, 25f);
|
||||||
// controller.minSpeed = EditorGUILayout.Slider("Minimum Speed", controller.minSpeed, 0.01f, 25.0f);
|
// controller.minSpeed = EditorGUILayout.Slider("Minimum Speed", controller.minSpeed, 0.01f, 25.0f);
|
||||||
// controller.maxSpeed = EditorGUILayout.Slider("Maximum Speed", controller.maxSpeed, 0.01f, 25.0f);
|
// controller.maxSpeed = EditorGUILayout.Slider("Maximum Speed", controller.maxSpeed, 0.01f, 25.0f);
|
||||||
controller.boundaryForce = EditorGUILayout.Slider("Boundary Force", controller.boundaryForce, 0.25f, 50f);
|
controller.boundaryForce = EditorGUILayout.Slider("Boundary Force", controller.boundaryForce, 0.25f, 500f);
|
||||||
controller.maxSteerForce = EditorGUILayout.Slider("Max Steer Force", controller.maxSteerForce, 1f, 500f);
|
controller.maxSteerForce = EditorGUILayout.Slider("Max Steer Force", controller.maxSteerForce, 1f, 500f);
|
||||||
|
|
||||||
EditorGUI.BeginChangeCheck();
|
EditorGUI.BeginChangeCheck();
|
||||||
|
|||||||
@@ -1254,7 +1254,6 @@ MonoBehaviour:
|
|||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
boidCount: 244
|
boidCount: 244
|
||||||
boidGroupRange: 3.29
|
boidGroupRange: 3.29
|
||||||
boidStartVelocity: 14.8
|
|
||||||
minSpeed: 9.225757
|
minSpeed: 9.225757
|
||||||
maxSpeed: 9.225757
|
maxSpeed: 9.225757
|
||||||
globalBias: 1
|
globalBias: 1
|
||||||
@@ -1266,15 +1265,13 @@ MonoBehaviour:
|
|||||||
enableAlignment: 1
|
enableAlignment: 1
|
||||||
enableCohesion: 1
|
enableCohesion: 1
|
||||||
enableBoundary: 1
|
enableBoundary: 1
|
||||||
enableFOVChecks: 1
|
enableFovChecks: 1
|
||||||
boidSeparationRange: 1.4
|
boidSeparationRange: 1.4
|
||||||
boundaryForce: 50
|
boundaryForce: 50
|
||||||
localFlocks: 1
|
localFlocks: 1
|
||||||
edgeWrapping: 1
|
edgeWrapping: 1
|
||||||
circleVertexCount: 360
|
|
||||||
circleWidth: 0.1
|
|
||||||
maxSteerForce: 383
|
maxSteerForce: 383
|
||||||
boidFOV: 297
|
boidFov: 239
|
||||||
focusedBoid: {fileID: 0}
|
focusedBoid: {fileID: 0}
|
||||||
boidObject: {fileID: 1737515784064720040, guid: 23e1eaaf69d4ef342ac3ef9590f6c642,
|
boidObject: {fileID: 1737515784064720040, guid: 23e1eaaf69d4ef342ac3ef9590f6c642,
|
||||||
type: 3}
|
type: 3}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class Boid : MonoBehaviour {
|
|||||||
[NonSerialized] public int latestNeighborhoodCount = 0;
|
[NonSerialized] public int latestNeighborhoodCount = 0;
|
||||||
[NonSerialized] public List<Boid> latestNeighborhood;
|
[NonSerialized] public List<Boid> latestNeighborhood;
|
||||||
[NonSerialized] private BoidController _parent;
|
[NonSerialized] private BoidController _parent;
|
||||||
[NonSerialized] public bool _isFocused;
|
[NonSerialized] public bool _isFocused = false;
|
||||||
[NonSerialized] private LineRenderer[] _lineRenderers;
|
[NonSerialized] private LineRenderer[] _lineRenderers;
|
||||||
|
|
||||||
|
|
||||||
@@ -38,11 +38,12 @@ public class Boid : MonoBehaviour {
|
|||||||
transform.position = _position;
|
transform.position = _position;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
// Find local neighborhood flock
|
||||||
Vector2 acceleration = Vector2.zero;
|
Vector2 acceleration = Vector2.zero;
|
||||||
List<Boid> flock = _parent.localFlocks ? GetFlock(_parent.boids, _parent.boidGroupRange) : _parent.boids;
|
List<Boid> flock = _parent.localFlocks ? GetFlock(_parent.boids, _parent.boidGroupRange) : _parent.boids;
|
||||||
latestNeighborhoodCount = flock.Count;
|
latestNeighborhoodCount = flock.Count;
|
||||||
|
|
||||||
// Only update latest neighborhood when we need it for focused boid gizmo draws
|
// Only update latest neighborhood when we need it for focused boid Gizmo draws
|
||||||
if (_isFocused)
|
if (_isFocused)
|
||||||
latestNeighborhood = flock;
|
latestNeighborhood = flock;
|
||||||
|
|
||||||
@@ -56,8 +57,9 @@ public class Boid : MonoBehaviour {
|
|||||||
acceleration += SteerTowards(Rule3(flock)) * _parent.alignmentBias;
|
acceleration += SteerTowards(Rule3(flock)) * _parent.alignmentBias;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_parent.enableBoundary && _parent.Boundary.Contains(_position))
|
if (_parent.enableBoundary && !_parent.Boundary.Contains(_position)) {
|
||||||
acceleration += SteerTowards(RuleBound()) * _parent.boundaryBias;
|
acceleration += SteerTowards(RuleBound()) * _parent.boundaryBias;
|
||||||
|
}
|
||||||
|
|
||||||
// Limit the Velocity Vector to a certain Magnitude
|
// Limit the Velocity Vector to a certain Magnitude
|
||||||
_velocity += acceleration * Time.deltaTime;
|
_velocity += acceleration * Time.deltaTime;
|
||||||
@@ -75,6 +77,11 @@ public class Boid : MonoBehaviour {
|
|||||||
Wrapping();
|
Wrapping();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Assists with clamping and normalizing a Vector2 force
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="vector">Force Vector being applied by a rule</param>
|
||||||
|
/// <returns>Vector2 force to be applied</returns>
|
||||||
private Vector2 SteerTowards(Vector2 vector) {
|
private Vector2 SteerTowards(Vector2 vector) {
|
||||||
Vector2 v = vector.normalized * _parent.maxSpeed - _velocity;
|
Vector2 v = vector.normalized * _parent.maxSpeed - _velocity;
|
||||||
return Vector2.ClampMagnitude(v, _parent.maxSteerForce);
|
return Vector2.ClampMagnitude(v, _parent.maxSteerForce);
|
||||||
@@ -252,6 +259,10 @@ public class Boid : MonoBehaviour {
|
|||||||
return child.AddComponent<LineRenderer>();
|
return child.AddComponent<LineRenderer>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Draw (or re-draw) all lines corresponding to separation and group circles and FOV Arc
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="redraw"><c>true</c> if draw operation should be treated as a re-draw</param>
|
||||||
public void Draw(bool redraw) {
|
public void Draw(bool redraw) {
|
||||||
// Clear positions when redrawing
|
// Clear positions when redrawing
|
||||||
if(redraw)
|
if(redraw)
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ public class BoidController : MonoBehaviour {
|
|||||||
float height = 2f * Cam.orthographicSize;
|
float height = 2f * Cam.orthographicSize;
|
||||||
var size = new Vector2(height * Cam.aspect, height);
|
var size = new Vector2(height * Cam.aspect, height);
|
||||||
Space = new Rect((Vector2) transform.position - size / 2, size);
|
Space = new Rect((Vector2) transform.position - size / 2, size);
|
||||||
Boundary = new Rect(Vector2.zero, Space.size * 0.95f);
|
Boundary = new Rect(Vector2.zero, Space.size * 0.90f);
|
||||||
Boundary.center = Space.center;
|
Boundary.center = Space.center;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ public class BoidController : MonoBehaviour {
|
|||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
// Instantiate a Boid prefab within the boundaries randomly
|
// Instantiate a Boid prefab within the boundaries randomly
|
||||||
Vector2 position = RandomPosition() * 0.95f;
|
Vector2 position = RandomPosition() * 0.90f;
|
||||||
GameObject boid = Instantiate(boidObject, position, Quaternion.identity);
|
GameObject boid = Instantiate(boidObject, position, Quaternion.identity);
|
||||||
|
|
||||||
// Set parent, add Boid component to Boids list
|
// Set parent, add Boid component to Boids list
|
||||||
|
|||||||
Reference in New Issue
Block a user