1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
#include <iostream> #include <vector> #include <cmath> #include <cstdlib> #include <ctime> // Constants const float WIDTH = 800.0f; const float HEIGHT = 600.0f; const int BOID_COUNT = 100; const float MAX_SPEED = 5.0f; const float MAX_FORCE = 0.1f; const float NEIGHBOR_RADIUS = 50.0f; const float SEPARATION_RADIUS = 20.0f; // Vector2 class for 2D vector operations class Vector2 { public: float x, y; Vector2(float x = 0, float y = 0) : x(x), y(y) {} Vector2 operator+(const Vector2& v) const { return Vector2(x + v.x, y + v.y); } Vector2 operator-(const Vector2& v) const { return Vector2(x - v.x, y - v.y); } Vector2 operator*(float scalar) const { return Vector2(x * scalar, y * scalar); } Vector2& operator+=(const Vector2& v) { x += v.x; y += v.y; return *this; } Vector2& operator-=(const Vector2& v) { x -= v.x; y -= v.y; return *this; } Vector2& operator*=(float scalar) { x *= scalar; y *= scalar; return *this; } float length() const { return std::sqrt(x * x + y * y); } Vector2 normalize() const { float len = length(); if (len > 0) { return *this * (1.0f / len); } return *this; } }; // Boid class class Boid { public: Vector2 position; Vector2 velocity; Vector2 acceleration; Boid(float x, float y) : position(x, y), velocity(rand() % 2 - 1, rand() % 2 - 1), acceleration(0, 0) {} void update() { velocity += acceleration; if (velocity.length() > MAX_SPEED) { velocity = velocity.normalize() * MAX_SPEED; } position += velocity; acceleration *= 0; wrapAround(); } void applyForce(const Vector2& force) { acceleration += force; } void wrapAround() { if (position.x < 0) position.x += WIDTH; if (position.x > WIDTH) position.x -= WIDTH; if (position.y < 0) position.y += HEIGHT; if (position.y > HEIGHT) position.y -= HEIGHT; } }; // Main simulation function void simulate(std::vector<Boid>& boids) { for (auto& boid : boids) { Vector2 alignment(0, 0); Vector2 cohesion(0, 0); Vector2 separation(0, 0); int neighborCount = 0; for (const auto& other : boids) { if (&boid == &other) continue; Vector2 diff = boid.position - other.position; float distance = diff.length(); if (distance < NEIGHBOR_RADIUS) { alignment += other.velocity; cohesion += other.position; if (distance < SEPARATION_RADIUS) { separation += diff.normalize() / distance; } neighborCount++; } } if (neighborCount > 0) { alignment = alignment * (1.0f / neighborCount); cohesion = (cohesion * (1.0f / neighborCount) - boid.position).normalize(); separation = separation.normalize(); } alignment = alignment.normalize() * MAX_SPEED - boid.velocity; cohesion = cohesion.normalize() * MAX_SPEED - boid.velocity; separation = separation.normalize() * MAX_SPEED - boid.velocity; alignment *= 1.0f; cohesion *= 1.0f; separation *= 1.5f; boid.applyForce(alignment); boid.applyForce(cohesion); boid.applyForce(separation); boid.update(); } } int main() { srand(static_cast<unsigned>(time(0))); std::vector<Boid> boids; for (int i = 0; i < BOID_COUNT; ++i) { boids.emplace_back(rand() % static_cast<int>(WIDTH), rand() % static_cast<int>(HEIGHT)); } while (true) { simulate(boids); // Here you would typically add rendering code to visualize the boids } return 0; } |
Explanation:
- Vector2 Class: Implements basic 2D vector operations like addition, subtraction, scaling, and normalization.
- Boid Class: Represents a single boid with position, velocity, and acceleration. The
update()
Advertisement - simulate Function: Simulates the behavior of all boids. For each boid, it calculates alignment (average velocity of neighbors), cohesion (average position of neighbors), and separation (avoidance of too close neighbors). Forces are applied to steer the boid.
- Main Function: Initializes a set of boids and repeatedly simulates their behavior. Rendering code should be added to visualize the simulation.
Advertisement
Advertisement