[ Video ]
[ About ]
立方体をランダムウォーク
Random walk of boxes.
[ 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 "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) {}; ofEasyCam cam; std::vector<std::unique_ptr<Particle>> particles; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(239); ofSetColor(39); ofNoFill(); ofEnableDepthTest(); for (int i = 0; i < 50; i++) { std::unique_ptr<Particle> particle(new Particle()); this->particles.push_back(std::move(particle)); } } //-------------------------------------------------------------- void ofApp::update() { for (std::unique_ptr<Particle>& particle : this->particles) { particle->Upate(this->particles); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); for (std::unique_ptr<Particle>& particle : this->particles) { particle->Draw(); } this->cam.end(); } //-------------------------------------------------------------- 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 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); ~Particle(); void Upate(std::vector<std::unique_ptr<Particle>>& particles); void Draw(); ofPoint next; private: int size; int depth; ofPoint location; int direction_index; ofPoint directions[5]; }; |
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 |
#include "Particle.h" Particle::Particle() { this->size = 40; this->depth = ofGetWidth() > ofGetHeight() ? ofGetWidth() : ofGetHeight(); int x = (int)(ofRandom(ofGetWidth()) / this->size) * this->size; int y = (int)(ofRandom(ofGetHeight()) / this->size) * this->size; this->location = ofPoint(x, y); this->directions[0] = ofPoint(1, 0); this->directions[1] = ofPoint(-1, 0); this->directions[2] = ofPoint(0, 1); this->directions[3] = ofPoint(0, -1); this->directions[4] = ofPoint(0, 0); } Particle::~Particle() { } void Particle::Upate(std::vector<std::unique_ptr<Particle>>& particles) { if (ofGetFrameNum() % this->size == 0) { int try_count = 0; int r = ofRandom(4); while (try_count < 4) { this->direction_index = (r + try_count) % 4; if (this->location.x < 0) { this->direction_index = 0; } else if (this->location.x > ofGetWidth()) { this->direction_index = 1; } if (this->location.y < 0) { this->direction_index = 2; } else if (this->location.y > ofGetHeight()) { this->direction_index = 3; } this->next = this->location + this->directions[this->direction_index] * this->size; bool overlap = false; for (std::unique_ptr<Particle>& particle : particles) { if (this != particle.get()) { if (this->next == particle->next) { overlap = true; break; } } } if (overlap == false) { break; } try_count++; } if (try_count == 4) { this->direction_index = 4; } } else if (ofGetFrameNum() % this->size == 1) { this->next = ofPoint(); } this->location += this->directions[this->direction_index]; } void Particle::Draw() { ofPushMatrix(); ofTranslate(this->location - ofPoint(ofGetWidth() * 0.5, ofGetHeight() * 0.5)); switch (this->direction_index) { case 0: ofRotateY(ofMap(ofGetFrameNum() % this->size, 0, this->size - 1, 0, 90)); break; case 1: ofRotateY(ofMap(ofGetFrameNum() % this->size, 0, this->size - 1, 0, -90)); break; case 2: ofRotateX(ofMap(ofGetFrameNum() % this->size, 0, this->size - 1, 0, -90)); break; case 3: ofRotateX(ofMap(ofGetFrameNum() % this->size, 0, this->size - 1, 0, 90)); break; default: break; } ofDrawBox(this->size); ofPopMatrix(); } |