[ Video ]
[ About ]
近づくと線を引く Mover は一定の確率で分裂して増えます。
ただし、異常増殖を防止する機能を備えています。
The Mover that draws a line as it gets closer will split up and increase with a certain probability.
However, it does have the ability to prevent abnormal growth.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include "ofMain.h" #include "Mover.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) {}; vector<unique_ptr<Mover>> mover_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 58 59 60 61 62 63 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetColor(39); ofSetLineWidth(2); auto m = make_unique<Mover>(glm::vec2(ofGetWidth() * 0.5, ofGetHeight() * 0.5)); this->mover_list.push_back(move(m)); } //-------------------------------------------------------------- void ofApp::update() { // 数によって分割する確率を変化させる int separation_rate = ofMap(this->mover_list.size(), 1, 30, 100, 0); // 更新 for (auto& mover : this->mover_list) { mover->update(separation_rate); } // 分割 for (int i = 0; i < this->mover_list.size(); i++) { if(this->mover_list[i]->isSeparation()){ auto m = make_unique<Mover>(this->mover_list[i]->getLocation()); this->mover_list.push_back(move(m)); } } // 削除 for (int i = this->mover_list.size() - 1; i >= 0; i--) { if (this->mover_list[i]->isDead()) { this->mover_list.erase(this->mover_list.begin() + i); } } } //-------------------------------------------------------------- void ofApp::draw() { for (auto& mover : this->mover_list) { mover->draw(this->mover_list); } } //-------------------------------------------------------------- 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 |
#pragma once #include "ofMain.h" class Mover { public: Mover(glm::vec2 start); glm::vec2 think(); void update(int separation_rate); void draw(vector<unique_ptr<Mover>>& mover_list); glm::vec2 getLocation(); bool isDead(); bool isSeparation(); private: int motion_cycle = 60; unsigned int count = 0; glm::vec2 start; glm::vec2 next; vector<glm::vec2> log_list; bool dead = false; bool separation = false; }; |
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 |
#include "Mover.h" //-------------------------------------------------------------- Mover::Mover(glm::vec2 start) { this->start = start; this->next = this->think(); log_list.push_back(this->start); } //-------------------------------------------------------------- void Mover::update(int separation_rate) { // 分離判定用のフラグ this->separation = false; // 次の場所を探す int param = ofGetFrameNum() % this->motion_cycle; if (param == 0) { if (this->count > 0) { this->start = this->next; this->next = this->think(); } // 動きの節目のタイミングで分離有無を判定判定 this->separation = ofRandom(100) < separation_rate; } // 静 / 動の制御 if(param > this->motion_cycle * 0.55) { auto location = this->start + ((this->next - this->start) / this->motion_cycle) * ofMap(param, this->motion_cycle * 0.55, this->motion_cycle - 1, 0, this->motion_cycle); this->log_list.push_back(location); } else { this->log_list.push_back(this->log_list.back()); } // 画面外に出た Mover は削除対象 if (this->log_list.back().x < 0 || this->log_list.back().x > ofGetWidth() || this->log_list.back().y < 0 || this->log_list.back().y > ofGetHeight()) { this->dead = true; } // 軌跡表示用に5フレーム分だけ保存 while (this->log_list.size() > 5) { this->log_list.erase(this->log_list.begin()); } this->count++; } //-------------------------------------------------------------- void Mover::draw(vector<unique_ptr<Mover>>& mover_list) { // 近くにいたら線を引く ofFill(); bool draw_line = false; for (auto& other : mover_list) { if (this->log_list.back() == other->getLocation()) { continue; } if (glm::distance(this->log_list.back(), other->getLocation()) < 100) { ofDrawLine(this->log_list.back(), other->getLocation()); draw_line = true; } } // 線の基点となる円 if (draw_line) { ofDrawCircle(this->log_list.back(), 5); } // 自身の場所を示す円 ofNoFill(); for (int i = 0; i < this->log_list.size(); i++) { ofSetColor(ofMap(i, 0, this->log_list.size() - 1, 200, 39)); ofDrawCircle(this->log_list[i], 15); } } //-------------------------------------------------------------- glm::vec2 Mover::think() { int radius = 175; int deg = ofRandom(360); auto tmp_next = this->start + glm::vec2(radius * cos(deg * DEG_TO_RAD), radius * sin(deg * DEG_TO_RAD)); return tmp_next; } //-------------------------------------------------------------- glm::vec2 Mover::getLocation() { return this->log_list.back(); } //-------------------------------------------------------------- bool Mover::isDead() { return this->dead; } //-------------------------------------------------------------- bool Mover::isSeparation() { return this->separation; } |