[ Video ]
[ About ]
動的に動く領域の中を動くエージェント
Agents are moving on a noisy shape.
[ 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 |
#pragma once #include "ofMain.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) {}; int number_of_agent; int len_of_tail; vector<deque<glm::vec2>> logs; vector<glm::vec2> directions; vector<ofColor> colors; vector<glm::vec2> vertices;; ofFbo fbo; ofPixels pixels; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetLineWidth(1.5); this->fbo.allocate(ofGetWidth(), ofGetHeight()); this->number_of_agent = 200; this->len_of_tail = 10; } //-------------------------------------------------------------- void ofApp::update() { this->vertices.clear(); this->fbo.begin(); ofClear(0); float radius = 350; for (int deg = 0; deg < 360; deg += 3) { glm::vec2 noise_point = glm::vec2(radius * cos(deg * DEG_TO_RAD), radius * sin(deg * DEG_TO_RAD)); float noise_radius = ofMap(ofNoise(noise_point.x * 0.005, noise_point.y * 0.005, ofGetFrameNum() * 0.005), 0, 1, radius * 0.3, radius); this->vertices.push_back(glm::vec2(noise_radius * cos(deg * DEG_TO_RAD) + ofGetWidth() * 0.5, noise_radius * sin(deg * DEG_TO_RAD) + ofGetHeight() * 0.5)); } ofFill(); ofBeginShape(); ofVertices(this->vertices); ofEndShape(true); this->fbo.end(); this->fbo.readToPixels(this->pixels); while (this->logs.size() < this->number_of_agent) { int x = ofRandom(ofGetWidth()); int y = ofRandom(ofGetHeight()); if (this->pixels.getColor(x, y) != ofColor(0, 0)) { deque<glm::vec2> log; log.push_front(glm::vec2(x, y)); this->logs.push_back(log); this->directions.push_back(glm::normalize(glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1)))); ofColor color; color.setHsb(ofRandom(255), 255, 200); this->colors.push_back(color); } } for (int i = 0; i < this->number_of_agent; i++) { glm::vec2 future = this->logs[i].front() + this->directions[i] * 5; int x = future.x; int y = future.y; while (this->pixels.getColor(x, y) == ofColor(0, 0)) { this->directions[i] = glm::normalize(glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1))); future = this->logs[i].front() + this->directions[i] * 10; x = future.x; y = future.y; } this->logs[i].push_front(future); while (this->logs[i].size() > this->len_of_tail) { this->logs[i].pop_back(); } } } //-------------------------------------------------------------- void ofApp::draw() { ofNoFill(); ofSetColor(39); ofBeginShape(); ofVertices(this->vertices); ofEndShape(true); for (int i = 0; i < this->number_of_agent; i++) { for (int l = 0; l < this->logs[i].size() - 1; l++) { ofSetColor(this->colors[i], ofMap(l, 0, this->len_of_tail, 255, 64)); ofDrawLine(this->logs[i][l], this->logs[i][l + 1]); } } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |