[ Video ]
[ About ]
黒い帯の上だけ移動が出来るエージェント。帯はノイズ関数で動かしています。エラー処理をサボったのでエージェントの移動先が無くなると無限ループでデッドします
Agents moving on the black band.
[ 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 |
#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; vector<glm::vec2> locations; vector<glm::vec2> directions; 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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetLineWidth(1.5); this->number_of_agent = 200; this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { this->fbo.begin(); ofClear(0); ofSetColor(39); for (int i = 1; i <= 4; i++) { vector<glm::vec2> vertices; for (int x = 0; x <= ofGetWidth(); x += 10) { vertices.push_back(glm::vec2(x, ofMap(ofNoise(i * 39, x * 0.001, ofGetFrameNum() * 0.001 * i), 0, 1, ofGetHeight() * 0.1, ofGetHeight() * 0.9))); } int count = vertices.size(); for (int vertices_index = count - 1; vertices_index > -1; vertices_index--) { vertices.push_back(vertices[vertices_index] - glm::vec2(0, -60)); } ofBeginShape(); ofVertices(vertices); ofEndShape(); } this->fbo.end(); this->fbo.readToPixels(this->pixels); while (this->locations.size() < this->number_of_agent) { int x = ofRandom(ofGetWidth()); int y = ofRandom(ofGetHeight()); if (this->pixels.getColor(x, y) != ofColor(0, 0)) { this->locations.push_back(glm::vec2(x, y)); this->directions.push_back(glm::normalize(glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1)))); } } for (int i = 0; i < this->number_of_agent; i++) { glm::vec2 future; future = this->locations[i] + this->directions[i] * 3; int x = future.x; int y = future.y; while (x < 0 || x > ofGetWidth() || y < 0 || y > ofGetHeight() || this->pixels.getColor(x, y) == ofColor(0, 0)) { this->directions[i] = glm::normalize(glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1))); future = this->locations[i] + this->directions[i] * 10; x = future.x; y = future.y; } this->locations[i] = future; } } //-------------------------------------------------------------- void ofApp::draw() { ofSetColor(255); this->fbo.draw(0, 0); ofFill(); for (int i = 0; i < this->number_of_agent; i++) { ofDrawCircle(this->locations[i], 3); for (int j = 0; j < this->number_of_agent; j++) { if (glm::length(this->locations[i] - this->locations[j]) < 30) { ofDrawLine(this->locations[i], this->locations[j]); } } } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |