// MATCH VELOCITY WITH THE FLOCK Vector2 Rule3(Boid _boid) { Vector2 PercievedVelocity = new Vector2(); PercievedVelocity.X = _boid.velocity.X; PercievedVelocity.Y = _boid.velocity.Y; int effectedBy = 0; for (int i = 0; i < prey.Count; i++) { if (prey[i] != _boid) { if ((prey[i].position - _boid.position).Length() < awarenessRadius) { PercievedVelocity = PercievedVelocity + prey[i].velocity; effectedBy++; } } } PercievedVelocity = PercievedVelocity / (float)(effectedBy + 1); return (PercievedVelocity - _boid.velocity) / 500.0f; }
// MOVE AWAY FROM ONE ANOTHER Vector2 Rule2(Boid _boid) { Vector2 c = new Vector2(); ; for (int i = 0; i < prey.Count; i++) { if (prey[i] != _boid) { if ((_boid.position - prey[i].position).Length() < awarenessRadius) { Vector2 temp = prey[i].position - _boid.position; if (temp.Length() < 50.0f) { c = c - (prey[i].position - _boid.position); } } } } return c / 10.0f; }
// MOVE TOWARDS THE CENTER OF THE FLOCK Vector2 Rule1(Boid _boid) { _boid.influenceTracker = 0.0f; Vector2 PercievedCenterOfMass = new Vector2(); PercievedCenterOfMass.X = _boid.position.X; PercievedCenterOfMass.Y = _boid.position.Y; int effectedBy = 0; for (int i = 0; i < prey.Count; i++) { if (prey[i] != _boid) { if ((_boid.position - prey[i].position).Length() < awarenessRadius) { PercievedCenterOfMass = PercievedCenterOfMass + prey[i].position; _boid.influenceTracker += 1.0f / 20.0f; effectedBy++; } } } if (_boid.influenceTracker > 1.0f) _boid.influenceTracker = 1.0f; PercievedCenterOfMass = PercievedCenterOfMass / (float)(effectedBy + 1); return (PercievedCenterOfMass - _boid.position) / 500.0f; }
void LimitVelocity(Boid _boid) { float magmax = 200.0f; if (_boid.velocity.Length() > magmax) { if (_boid.velocity.X == 0.0f || _boid.velocity.Y == 0.0f) return; float xsign = _boid.velocity.X / Math.Abs(_boid.velocity.X); float ysign = _boid.velocity.Y / Math.Abs(_boid.velocity.Y); float r = Math.Abs(_boid.velocity.X / _boid.velocity.Y); float tempy = (float)Math.Sqrt((magmax * magmax) / (1.0f + (r * r))); float tempx = tempy * r; _boid.velocity.Y = tempy * ysign; _boid.velocity.X = tempx * xsign; } }
Vector2 FollowMouse(Boid _boid) { Vector2 mouse = Vector2.Zero; //new Vector2(Input.MousePos.X, Input.MousePos.Y); return (mouse - _boid.position) / 100.0f; }
Vector2 Cage(Boid _boid) { Vector2 v = new Vector2(); if (_boid.position.X < cageLeft) v.X = 100.0f; if (_boid.position.X > cageRight) v.X = -100.0f; if (_boid.position.Y < cageTop) v.Y = 100.0f; if (_boid.position.Y > cageBottom) v.Y = -100.0f; return v; }