[ Video ]
[ About ]
立方体を這う形にしたかったのですが、大部分が逃げて行ってしまいました
Random walk on cube ( mistake ).
[ 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(239); ofSetColor(39); ofEnableDepthTest(); for (int i = 0; i < 512; 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 19 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); ~Particle(); void Upate(); void Draw(); private: int depth; ofPoint location; ofPoint direction; ofPoint directions[6]; 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 |
#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); this->direction = ofPoint(); this->directions[0] = ofPoint(1, 0, 0); this->directions[1] = ofPoint(-1, 0, 0); this->directions[2] = ofPoint(0, 1, 0); this->directions[3] = ofPoint(0, -1, 0); this->directions[4] = ofPoint(0, 0, 1); this->directions[5] = ofPoint(0, 0, -1); } Particle::~Particle() { } void Particle::Upate() { if (ofGetFrameNum() % 20 == 0) { std::vector<ofPoint> select_directions; if (this->location.z == 0 || this->location.z == this->depth){ select_directions.push_back(this->directions[0]); select_directions.push_back(this->directions[1]); select_directions.push_back(this->directions[2]); select_directions.push_back(this->directions[3]); } if (this->location.y == 0 || this->location.y == ofGetHeight()) { select_directions.push_back(this->directions[0]); select_directions.push_back(this->directions[1]); select_directions.push_back(this->directions[4]); select_directions.push_back(this->directions[5]); } if (this->location.x == 0 || this->location.x == ofGetWidth()) { select_directions.push_back(this->directions[2]); select_directions.push_back(this->directions[3]); select_directions.push_back(this->directions[4]); select_directions.push_back(this->directions[5]); } if (select_directions.size() > 0) { int r = ofRandom(select_directions.size()); this->direction = select_directions[r]; } else { this->direction = ofPoint(); } } 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(); } |
[ Link ]