[ Video ]
[ About ]
帯の群れにして個体を追従。
Belts flock and target red.
[ 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 |
#pragma once #include "ofMain.h" #include "Actor.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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; ofEasyCam cam; int number_of_particle; vector<unique_ptr<Actor>> actor_list; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(239); ofEnableDepthTest(); auto actor = make_unique<Actor>(ofColor(255, 0, 0)); this->actor_list.push_back(move(actor)); this->number_of_particle = 500; for (int i = 0; i < this->number_of_particle; i++) { auto actor = make_unique<Actor>(); this->actor_list.push_back(move(actor)); } } //-------------------------------------------------------------- void ofApp::update() { for (auto& actor : this->actor_list) { actor->think(this->actor_list); } for (auto& actor : this->actor_list) { actor->update(); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.lookAt(this->actor_list.front()->get_location()); this->cam.setPosition(this->actor_list.front()->get_last_log() + glm::vec3(0, 0, 100)); this->cam.begin(); for (auto& actor : this->actor_list) { actor->draw(); } this->cam.end(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(1280, 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 28 29 30 31 32 33 34 |
#pragma once #include "ofMain.h" class Actor { public: Actor(); Actor(ofColor color); ~Actor(); void think(vector<unique_ptr<Actor>>& particles); void update(); void draw(); glm::vec3 separate(vector<unique_ptr<Actor>>& particles); glm::vec3 align(vector<unique_ptr<Actor>>& particles); glm::vec3 cohesion(vector<unique_ptr<Actor>>& particles); glm::vec3 seek(glm::vec3 target); void applyForce(glm::vec3 force); glm::vec3 get_location(); glm::vec3 get_last_log(); private: glm::vec3 location; glm::vec3 velocity; glm::vec3 acceleration; glm::vec3 param; deque<glm::vec3> log_list; float range; float max_force; float max_speed; int len; ofColor color; }; |
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
#include "Actor.h" //-------------------------------------------------------------- Actor::Actor() : Actor(ofColor(39)) { } //-------------------------------------------------------------- Actor::Actor(ofColor color) { this->location = glm::vec3(ofRandom(-300, 300), ofRandom(-300, 300), ofRandom(-300, 300)); this->velocity = glm::vec3(ofRandom(-1, 1), ofRandom(-1, 1), ofRandom(-1, 1)); this->range = 40; this->max_force = 1; this->max_speed = 6; this->color = color; this->len = 50; switch ((int)ofRandom(3)) { case 0: this->param = glm::vec3(5, 0, 0); break; case 1: this->param = glm::vec3(0, 5, 0); break; case 2: this->param = glm::vec3(0, 0, 5); break; } } //-------------------------------------------------------------- Actor::~Actor() { } //-------------------------------------------------------------- void Actor::think(vector<unique_ptr<Actor>>& Actors) { // 分離 glm::vec3 separate = this->separate(Actors); this->applyForce(separate * 1.05); // 整列 glm::vec3 align = this->align(Actors); this->applyForce(align); // 結合 glm::vec3 cohesion = this->cohesion(Actors); this->applyForce(cohesion); // 自我 if (glm::length(this->velocity) > 0) { glm::vec3 feature = this->location + glm::normalize(velocity) * this->range; glm::vec3 target = feature + glm::normalize(glm::vec3(ofRandom(-1, 1), ofRandom(-1, 1), ofRandom(-1, 1))) * this->range * 0.5; glm::vec3 ego = this->seek(target); this->applyForce(ego); } // 境界 if (glm::length(this->location - glm::vec3()) > 600) { glm::vec3 area = this->seek(glm::vec3()); this->applyForce(area * 5); } } //-------------------------------------------------------------- void Actor::update() { // 前進 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.9; this->log_list.push_front(this->location); while (this->log_list.size() > this->len) { this->log_list.pop_back(); } } //-------------------------------------------------------------- void Actor::draw() { ofMesh face, frame; frame.setMode(ofPrimitiveMode::OF_PRIMITIVE_LINES); for (int i = 0; i < this->log_list.size() * 0.75; i++) { auto log = this->log_list[i]; face.addVertex(log + this->param); face.addVertex(log - this->param); frame.addVertex(log + this->param); frame.addVertex(log - this->param); if (i > 0) { face.addIndex(i * 2); face.addIndex(i * 2 - 1); face.addIndex(i * 2 - 2); face.addIndex(i * 2); face.addIndex(i * 2 + 1); face.addIndex(i * 2 - 1); frame.addIndex(i * 2); frame.addIndex(i * 2 - 2); frame.addIndex(i * 2 + 1); frame.addIndex(i * 2 - 1); } } frame.addIndex(0); frame.addIndex(1); frame.addIndex(frame.getNumVertices() - 1); frame.addIndex(frame.getNumVertices() - 2); ofSetColor(this->color); face.drawFaces(); ofSetColor(239); frame.drawWireframe(); } //-------------------------------------------------------------- glm::vec3 Actor::separate(vector<unique_ptr<Actor>>& Actors) { float tmp_range = this->range * 0.5; glm::vec3 result; glm::vec3 sum; int count = 0; for (auto& other : Actors) { glm::vec3 difference = this->location - other->location; float len = glm::length(difference); if (len > 0 && len < tmp_range) { sum += glm::normalize(difference); count++; } } if (count > 0) { glm::vec3 avg = sum / count; avg = avg * this->max_speed; if (glm::length(avg) > this->max_speed) { avg = glm::normalize(avg) * this->max_speed; } glm::vec3 steer = avg - this->velocity; if (glm::length(steer) > this->max_force) { steer = glm::normalize(steer) * this->max_force; } result = steer; } return result; } //-------------------------------------------------------------- glm::vec3 Actor::align(vector<unique_ptr<Actor>>& Actors) { float tmp_range = this->range; glm::vec3 result; glm::vec3 sum; int count = 0; for (auto& other : Actors) { glm::vec3 difference = this->location - other->location; float len = glm::length(difference); if (len > 0 && len < tmp_range) { sum += other->velocity; count++; } } if (count > 0) { glm::vec3 avg = sum / count; avg = avg * this->max_speed; if (glm::length(avg) > this->max_speed) { avg = glm::normalize(avg) * this->max_speed; } glm::vec3 steer = avg - this->velocity; if (glm::length(steer) > this->max_force) { steer = glm::normalize(steer) * this->max_force; } result = steer; } return result; } //-------------------------------------------------------------- glm::vec3 Actor::cohesion(vector<unique_ptr<Actor>>& Actors) { float tmp_range = this->range * 0.5; glm::vec3 result; glm::vec3 sum; int count = 0; for (auto& other : Actors) { glm::vec3 difference = this->location - other->location; float len = glm::length(difference); if (len > 0 && len < tmp_range) { sum += other->location; count++; } } if (count > 0) { result = this->seek(sum / count); } return result; } //-------------------------------------------------------------- glm::vec3 Actor::seek(glm::vec3 target) { glm::vec3 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::vec3 steer = desired - this->velocity; if (glm::length(steer) > this->max_force) { steer = glm::normalize(steer) * this->max_force; } return steer; } //-------------------------------------------------------------- void Actor::applyForce(glm::vec3 force) { this->acceleration += force; } //-------------------------------------------------------------- glm::vec3 Actor::get_location() { return this->location; } //-------------------------------------------------------------- glm::vec3 Actor::get_last_log() { return this->log_list.back(); } |
Looking good!