[ Video ]
[ About ]
中央の力場で右往左往するパーティクル
Particle move by force field.
[ 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 "ofxBox2d.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) {}; ofxBox2d box2d; vector<shared_ptr<ofxBox2dCircle>> circles; vector<ofColor> circles_color; vector<deque<ofPoint>> circles_log; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetLineWidth(3); this->box2d.init(); this->box2d.setGravity(0, 0); this->box2d.createBounds(); this->box2d.setFPS(60); for (int i = 0; i < 25; i++) { auto circle = make_shared<ofxBox2dCircle>(); circle.get()->setPhysics(0.5, 1.0, 1.0); circle.get()->setup(this->box2d.getWorld(), ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()), 8); this->circles.push_back(move(circle)); ofColor c; c.setHsb(i * 10, 255, 230); this->circles_color.push_back(c); deque<ofPoint> log; this->circles_log.push_back(log); } } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->circles.size(); i++) { int span = 30; for (int x = ofGetWidth() * 0.2; x <= ofGetWidth() * 0.8; x += span) { for (int y = ofGetHeight() * 0.2; y <= ofGetHeight() * 0.8; y += span) { if ((this->circles[i]->getPosition().x > x - span * 0.5 && this->circles[i]->getPosition().x < x + span * 0.5) && (this->circles[i]->getPosition().y > y - span * 0.5 && this->circles[i]->getPosition().y < y + span * 0.5)) { float noise_deg = ofNoise(x * 0.0005, y * 0.0005, ofGetFrameNum() * 0.01) * 360; ofPoint power = ofPoint(cos(noise_deg * DEG_TO_RAD), sin(noise_deg * DEG_TO_RAD)); this->circles[i]->addForce(power, 1.0); } } } } this->box2d.update(); for (int i = 0; i < this->circles.size(); i++) { this->circles_log[i].push_front(this->circles[i]->getPosition()); while (this->circles_log[i].size() > 10) { this->circles_log[i].pop_back(); } } } //-------------------------------------------------------------- void ofApp::draw() { ofFill(); ofSetColor(39); int span = 30; for (int x = ofGetWidth() * 0.2; x <= ofGetWidth() * 0.8; x += span) { for (int y = ofGetHeight() * 0.2; y <= ofGetHeight() * 0.8; y += span) { float noise_deg = ofNoise(x * 0.0005, y * 0.0005, ofGetFrameNum() * 0.01) * 360; ofBeginShape(); ofVertex((x + span * 0.5) + span * 0.35 * cos(noise_deg * DEG_TO_RAD), (y + span * 0.5) + span * 0.35 * sin(noise_deg * DEG_TO_RAD)); ofVertex((x + span * 0.5) + span * 0.35 * cos((noise_deg + 135) * DEG_TO_RAD), (y + span * 0.5) + span * 0.35 * sin((noise_deg + 135) * DEG_TO_RAD)); ofVertex((x + span * 0.5) + span * 0.35 * cos((noise_deg + 225) * DEG_TO_RAD), (y + span * 0.5) + span * 0.35 * sin((noise_deg + 225) * DEG_TO_RAD)); ofEndShape(true); } } for (int i = 0; i < this->circles.size(); i++) { ofFill(); ofSetColor(this->circles_color[i]); ofDrawCircle(this->circles[i]->getPosition(), this->circles[i]->getRadius()); ofNoFill(); ofBeginShape(); for (int log_index = 0; log_index < this->circles_log[i].size(); log_index++) { ofVertex(this->circles_log[i][log_index]); } ofEndShape(false); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |