// Applies the three laws to the flock of boids void Boid::flock(vector<Boid> v) { Pvector sep = Separation(v); Pvector ali = Alignment(v); Pvector coh = Cohesion(v); // Arbitrarily weight these forces sep.mulScalar(SepW); ali.mulScalar(AliW); // Might need to alter weights for different characteristics coh.mulScalar(CohW); // Add the force vectors to acceleration applyForce(sep); applyForce(ali); applyForce(coh); }
void Boid::AvoidAsteroids(vector<Boid> v, sf::Vector2f asteroidPos) { Pvector sep = AsteroidSeparation(v, asteroidPos); // Arbitrarily weight these forces sep.mulScalar(1.5); // Add the force vectors to acceleration applyForce(sep); }
tuple<bool, Pvector> Boid::CalcLJ(Boid b, sf::Vector2f playerPos) { Pvector R; Pvector p(playerPos.x, playerPos.y); float neighbordist = 500; float neighbordist2 = 200; float A = 100; float B = 5000; float N = 1; float M = 2; float d = location.distance(b.location); float d2 = location.distance(p); if ((d2 > 0) && (d2 < neighbordist2)) { R = location; R.subVector(p); float D = R.magnitude(); float U = -A / pow(D, N) + B / pow(D, M); R.normalize(); R.mulScalar(U); return tuple<bool, Pvector>(true, R); } else if ((d > 0) && (d < neighbordist)) // 0 < d < 500 { R = location; R.subVector(b.location); float D = R.magnitude(); float U = -A / pow(D, N) + B / pow(D, M); R.normalize(); R.mulScalar(U); return tuple<bool, Pvector>(true, R); } else return tuple<bool, Pvector>(false, R); }
// Limits the maxSpeed, finds necessary steering force and // normalizes vectors Pvector Boid::seek(Pvector v) { Pvector desired; desired.subVector(v); // A vector pointing from the location to the target // Normalize desired and scale to maximum speed desired.normalize(); desired.mulScalar(maxSpeed); // Steering = Desired minus Velocity acceleration.subTwoVector(desired, velocity); acceleration.limit(maxForce); // Limit to maximum steering force return acceleration; }
Pvector Predator::Seek(Pvector playerLocation) { auto temp = playerLocation; temp.subScalar(30); Pvector desired = temp - *location; // desired.subVector(playerLocation); // A vector pointing from the location to the target // Normalize desired and scale to maximum speed desired.normalize(); desired.mulScalar(speed); // Steering = Desired minus Velocity *acceleration = desired - *velocity; acceleration->limit(maxSteeringForce); // Limit to maximum steering force return *acceleration; }