[ Video ]
[ About ]
群れのアルゴリズムはNature of Codeを参照しています。
The swarm algorithm refers to the Nature of Code.
THE NATURE OF CODE
DANIEL SHIFFMAN
https://natureofcode.com/
[ Source ]
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 |
#pragma once #include "ofMain.h" #include "Particle.h" class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void keyPressed(int key) {}; void keyReleased(int key) {}; void mouseMoved(int x, int y) {}; void mouseDragged(int x, int y, int button) {}; void mousePressed(int x, int y, int button) {}; void mouseReleased(int x, int y, int button) {}; void mouseEntered(int x, int y) {}; void mouseExited(int x, int y) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; void draw_digital(glm::vec2 location, int number_index); void draw_hexagon(glm::vec2 location, float deg); vector<unique_ptr<Particle>> particles; vector<pair<glm::vec2, int>> number_list; float hexagon_height; float hexagon_width; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(239); for (int i = 0; i < 50; i++) { auto particle = make_unique<Particle>(); this->particles.push_back(move(particle)); } this->hexagon_height = 6; this->hexagon_width = 3; for (float x = this->hexagon_width * 18; x <= ofGetWidth() - this->hexagon_width * 18; x += this->hexagon_height * 1.5) { for (float y = this->hexagon_height * 6; y <= ofGetHeight() - this->hexagon_height * 6; y += this->hexagon_height * 3) { this->number_list.push_back(make_pair(glm::vec2(x, y), 0)); } } } //-------------------------------------------------------------- void ofApp::update() { for (auto& particle : this->particles) { particle->update(this->particles); } for (auto& number : this->number_list) { number.second = number.second > 0 ? number.second - 1 : 0; } for (auto& particle : this->particles) { for (auto& number : this->number_list) { if (glm::distance(particle->location, number.first) < 10) { number.second = number.second < 50 ? number.second + 8 : 50; } } } } //-------------------------------------------------------------- void ofApp::draw() { for (auto& number : this->number_list) { if (number.second > 0) { int number_index = ofMap(ofNoise(glm::vec3(number.first * 0.05, ofGetFrameNum() * 0.03)), 0, 1, 0, 10); this->draw_digital(number.first, number_index); } } } //-------------------------------------------------------------- void ofApp::draw_digital(glm::vec2 location, int number_index) { vector<pair<glm::vec2, float>> part_list = { make_pair<glm::vec2, float>(location + glm::vec2(0, -this->hexagon_height), 90), make_pair<glm::vec2, float>(location + glm::vec2(this->hexagon_height * -0.5, this->hexagon_height * 0.5), 0), make_pair<glm::vec2, float>(location + glm::vec2(this->hexagon_height * 0.5, this->hexagon_height * 0.5), 0), make_pair<glm::vec2, float>(location + glm::vec2(0, 0), 90), make_pair<glm::vec2, float>(location + glm::vec2(this->hexagon_height * -0.5, this->hexagon_height * -0.5), 0), make_pair<glm::vec2, float>(location + glm::vec2(this->hexagon_height * 0.5, this->hexagon_height * -0.5), 0), make_pair<glm::vec2, float>(location + glm::vec2(0, this->hexagon_height), 90) }; vector<vector<int>> index_list = { { 0, 1, 2, 4, 5, 6 }, { 2, 5 }, { 0, 1, 3, 5, 6 }, { 0, 2, 3, 5, 6 }, { 2, 3, 4, 5 }, { 0, 2, 3, 4, 6 }, { 0, 1, 2, 3, 4, 6 }, { 0, 2, 5 }, { 0, 1, 2, 3, 4, 5, 6 }, { 0, 2, 3, 4, 5, 6 }, }; for (auto& index : index_list[number_index]) { this->draw_hexagon(part_list[index].first, part_list[index].second); } } //-------------------------------------------------------------- void ofApp::draw_hexagon(glm::vec2 location, float deg) { ofPushMatrix(); ofTranslate(location); ofRotate(deg); vector<glm::vec2> vertices; vertices.push_back(glm::vec2(this->hexagon_width * -0.4, this->hexagon_height * -0.4)); vertices.push_back(glm::vec2(this->hexagon_width * -0.4, this->hexagon_height * 0.4)); vertices.push_back(glm::vec2(0, this->hexagon_height * 0.5)); vertices.push_back(glm::vec2(this->hexagon_width * 0.4, this->hexagon_height * 0.4)); vertices.push_back(glm::vec2(this->hexagon_width * 0.4, this->hexagon_height * -0.4)); vertices.push_back(glm::vec2(0, this->hexagon_height * -0.5)); ofFill(); ofSetColor(39); ofBeginShape(); ofVertices(vertices); ofEndShape(true); ofPopMatrix(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
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 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); ~Particle(); void update(vector<unique_ptr<Particle>>& particles); void draw(); glm::vec2 separate(vector<unique_ptr<Particle>>& particles); glm::vec2 align(vector<unique_ptr<Particle>>& particles); glm::vec2 cohesion(vector<unique_ptr<Particle>>& particles); glm::vec2 seek(glm::vec2 target); void applyForce(glm::vec2 force); glm::vec2 location; vector<glm::vec2> log; private: glm::vec2 velocity; glm::vec2 acceleration; float range; float max_force; float max_speed; }; |
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
#include "Particle.h" //-------------------------------------------------------------- Particle::Particle() { this->location = glm::vec2(ofRandom(ofGetWidth()), ofRandom(ofGetHeight())); this->velocity = glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1)); this->range = 25; this->max_force = 1; this->max_speed = 8; } //-------------------------------------------------------------- Particle::~Particle() {} //-------------------------------------------------------------- void Particle::update(vector<unique_ptr<Particle>>& particles) { // 分離 glm::vec2 separate = this->separate(particles); this->applyForce(separate); // 整列 glm::vec2 align = this->align(particles); this->applyForce(align); // 結合 glm::vec2 cohesion = this->cohesion(particles); this->applyForce(cohesion); // 自我 if (glm::length(this->velocity) > 0) { glm::vec2 future = glm::normalize(this->velocity) * this->range; future += this->location; float angle = ofRandom(360); glm::vec2 target = future + glm::vec2(this->range * 0.5 * cos(angle * DEG_TO_RAD), this->range * 0.5 * sin(angle * DEG_TO_RAD)); glm::vec2 ego = this->seek(target); this->applyForce(ego); } // 境界 if (glm::length(this->location - glm::vec2(ofGetWidth() * 0.5, ofGetHeight() * 0.5)) > 500) { glm::vec2 area = this->seek(glm::vec2(ofGetWidth() * 0.5, ofGetHeight() * 0.5)); this->applyForce(area * 10); } // 前進 this->velocity += this->acceleration; if (glm::length(this->velocity) > this->max_speed) { this->velocity = glm::normalize(this->velocity) * this->max_speed; } this->location += this->velocity; this->acceleration *= 0; this->velocity *= 0.98; // 記録 this->log.push_back(this->location); while (this->log.size() > 15) { this->log.erase(this->log.begin()); } } //-------------------------------------------------------------- void Particle::draw() { if (this->log.size() < 3) { return; } auto head_size = 5; ofMesh mesh; vector<glm::vec3> right, left; glm::vec3 last_location; float last_theta; ofColor color(39); for (int k = 0; k < this->log.size() - 1; k++) { auto loc = glm::vec3(this->log[k], 0); auto next = glm::vec3(this->log[k + 1], 0); auto direction = next - loc; auto theta = atan2(direction.y, direction.x); right.push_back(loc + glm::vec3(ofMap(k, 0, this->log.size(), 0, head_size) * cos(theta + PI * 0.5), ofMap(k, 0, this->log.size(), 0, head_size) * sin(theta + PI * 0.5), 0)); left.push_back(loc + glm::vec3(ofMap(k, 0, this->log.size(), 0, head_size) * cos(theta - PI * 0.5), ofMap(k, 0, this->log.size(), 0, head_size) * sin(theta - PI * 0.5), 0)); last_location = loc; last_theta = theta; } for (int k = 0; k < right.size(); k++) { mesh.addVertex(left[k]); mesh.addVertex(right[k]); mesh.addColor(ofColor(color, ofMap(k, 0, this->log.size(), 0, 255))); mesh.addColor(ofColor(color, ofMap(k, 0, this->log.size(), 0, 255))); } for (int k = 0; k < mesh.getNumVertices() - 2; k += 2) { mesh.addIndex(k + 0); mesh.addIndex(k + 1); mesh.addIndex(k + 3); mesh.addIndex(k + 0); mesh.addIndex(k + 2); mesh.addIndex(k + 3); } auto tmp_header_size = ofMap(this->log.size() - 2, 0, this->log.size(), 0, head_size); auto tmp_alpha = ofMap(this->log.size() - 2, 0, this->log.size(), 0, 255); mesh.addVertex(last_location); mesh.addColor(ofColor(color, tmp_alpha)); int index = mesh.getNumVertices(); for (auto theta = last_theta - PI * 0.5; theta <= last_theta + PI * 0.5; theta += PI / 20) { mesh.addVertex(last_location + glm::vec3(tmp_header_size * cos(theta), tmp_header_size * sin(theta), 0)); mesh.addColor(ofColor(color, tmp_alpha)); } for (int k = index; k < mesh.getNumVertices() - 1; k++) { mesh.addIndex(index); mesh.addIndex(k + 0); mesh.addIndex(k + 1); } mesh.draw(); } //-------------------------------------------------------------- glm::vec2 Particle::separate(vector<unique_ptr<Particle>>& particles) { glm::vec2 result; glm::vec2 sum; int count = 0; for (auto& other : particles) { glm::vec2 difference = this->location - other->location; if (glm::length(difference) > 0 && glm::length(difference) < this->range * 0.5) { sum += glm::normalize(difference); count++; } } if (count > 0) { glm::vec2 avg = sum / count; avg = avg * this->max_speed; if (glm::length(avg) > this->max_speed) { avg = glm::normalize(avg) * this->max_speed; } glm::vec2 steer = avg - this->velocity; if (glm::length(steer) > this->max_force) { steer = glm::normalize(steer) * this->max_force; } result = steer; } return result; } //-------------------------------------------------------------- glm::vec2 Particle::align(vector<unique_ptr<Particle>>& particles) { glm::vec2 result; glm::vec2 sum; int count = 0; for (auto& other : particles) { glm::vec2 difference = this->location - other->location; if (glm::length(difference) > 0 && glm::length(difference) < this->range) { sum += other->velocity; count++; } } if (count > 0) { glm::vec2 avg = sum / count; avg = avg * this->max_speed; if (glm::length(avg) > this->max_speed) { avg = glm::normalize(avg) * this->max_speed; } glm::vec2 steer = avg - this->velocity; if (glm::length(steer) > this->max_force) { steer = glm::normalize(steer) * this->max_force; } result = steer; } return result; } //-------------------------------------------------------------- glm::vec2 Particle::cohesion(vector<unique_ptr<Particle>>& particles) { glm::vec2 result; glm::vec2 sum; int count = 0; for (auto& other : particles) { glm::vec2 difference = this->location - other->location; if (glm::length(difference) > 0 && glm::length(difference) < this->range * 0.5) { sum += other->location; count++; } } if (count > 0) { result = this->seek(sum / count); } return result; } //-------------------------------------------------------------- glm::vec2 Particle::seek(glm::vec2 target) { glm::vec2 desired = target - this->location; float distance = glm::length(desired); desired = glm::normalize(desired); desired *= distance < this->range ? ofMap(distance, 0, this->range, 0, this->max_speed) : max_speed; glm::vec2 steer = desired - this->velocity; if (glm::length(steer) > this->max_force) { steer = glm::normalize(steer) * this->max_force; } return steer; } //-------------------------------------------------------------- void Particle::applyForce(glm::vec2 force) { this->acceleration += force; } |