[ Video ]
[ About ]
立方体の面上に留まって頂く事に成功しました
Random walk on cube ( Success )
[ 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; vector<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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(39); ofSetColor(239); ofEnableDepthTest(); for (int i = 0; i < 255; i++) { this->particles.push_back(Particle()); } } //-------------------------------------------------------------- void ofApp::update() { for (Particle& particle : particles) { particle.Upate(); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofRotateY(ofGetFrameNum() * 0.25); for (Particle& particle : 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 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); ~Particle(); void Upate(); void Draw(); private: int depth; ofPoint location; ofPoint direction; std::deque<ofPoint> logs; }; |
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 |
#include "Particle.h" Particle::Particle() { this->depth = ofGetWidth() > ofGetHeight() ? ofGetWidth() : ofGetHeight(); int x, y, z; int start_surface = ofRandom(6); switch (start_surface) { case 0: // 正面 x = (int)(ofRandom(ofGetWidth()) / 40) * 40; y = (int)(ofRandom(ofGetHeight()) / 40) * 40; z = this->depth; break; case 1: // 背面 x = (int)(ofRandom(ofGetWidth()) / 40) * 40; y = (int)(ofRandom(ofGetHeight()) / 40) * 40; z = 0; break; case 2: // 上面 x = (int)(ofRandom(ofGetWidth()) / 40) * 40; y = ofGetHeight(); z = (int)(ofRandom(this->depth) / 40) * 40; break; case 3: // 底面 x = (int)(ofRandom(ofGetWidth()) / 40) * 40; y = 0; z = (int)(ofRandom(this->depth) / 40) * 40; break; case 4: // 右面 x = ofGetWidth(); y = (int)(ofRandom(ofGetHeight()) / 40) * 40; z = (int)(ofRandom(this->depth) / 40) * 40; break; case 5: // 左面 x = 0; y = (int)(ofRandom(ofGetHeight()) / 40) * 40; z = (int)(ofRandom(this->depth) / 40) * 40; break; } this->location = ofPoint(x, y, z); } Particle::~Particle() { } void Particle::Upate() { if (ofGetFrameNum() % 20 == 0) { ofPoint directions[6]; directions[0] = ofPoint(1, 0, 0); directions[1] = ofPoint(-1, 0, 0); directions[2] = ofPoint(0, 1, 0); directions[3] = ofPoint(0, -1, 0); directions[4] = ofPoint(0, 0, 1); directions[5] = ofPoint(0, 0, -1); if (this->location.x == 0 || this->location.x == ofGetWidth()) { if (this->location.y == 0 || this->location.y == ofGetHeight() || this->location.z == 0 || this->location.z == this->depth) { this->location.x == ofGetWidth() ? directions[0] = directions[1] : directions[1] = directions[0]; } else { directions[0] = this->direction; directions[1] = this->direction; } } if (this->location.y == 0 || this->location.y == ofGetHeight()) { if (this->location.x == 0 || this->location.x == ofGetWidth() || this->location.z == 0 || this->location.z == this->depth) { this->location.y == ofGetHeight() ? directions[2] = directions[3] : directions[3] = directions[2]; } else { directions[2] = this->direction; directions[3] = this->direction; } } if (this->location.z == 0 || this->location.z == this->depth) { if (this->location.x == 0 || this->location.x == ofGetWidth() || this->location.y == 0 || this->location.y == ofGetHeight()) { this->location.z == this->depth ? directions[4] = directions[5] : directions[5] = directions[4]; } else { directions[4] = this->direction; directions[5] = this->direction; } } int r = ofRandom(6); this->direction = directions[r]; } this->location += this->direction * 2; this->logs.push_front(location - ofPoint(ofGetWidth() * 0.5, ofGetHeight() * 0.5, this->depth * 0.5)); while (this->logs.size() > 100) { this->logs.pop_back(); } } void Particle::Draw() { ofFill(); ofDrawSphere(this->logs.front(), 3); ofNoFill(); ofBeginShape(); for (ofPoint& log : this->logs) { ofVertex(log); } ofEndShape(); } |