[ Video ]
[ About ]
お互いに反発するパーティクル。
数字は反発関係にあるパーティクルの個数で変化します。
Particle repel each other.
Number on particle is count of repel particles.
[ 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 28 29 30 31 |
#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 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) {}; ofxBox2d box2d; float range; float radius; int font_size; ofTrueTypeFont font; vector<shared_ptr<ofxBox2dCircle>> circles; vector<int> near_count; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("Insta"); ofBackground(239); ofSetLineWidth(3); this->box2d.init(); this->box2d.setGravity(0, 0); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); this->radius = 12; this->range = 50; this->font_size = this->radius; this->font.loadFont("fonts/Kazesawa-Bold.ttf", this->font_size, true, true, true); for (int i = 0; i < 192; i++) { this->circles.push_back(shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle)); this->circles.back().get()->setPhysics(1.5, 0.5, 0.1); this->circles.back().get()->setup(this->box2d.getWorld(), ofRandom(ofGetWidth()), ofRandom(ofGetHeight()), this->radius); this->near_count.push_back(0); } } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->near_count.size(); i++) { this->near_count[i] = 0; } for (int i = 0; i < this->circles.size(); i++) { for (int j = i + 1; j < this->circles.size(); j++) { float distance = this->circles[i]->getPosition().distance(this->circles[j]->getPosition()); if (distance < this->range) { this->circles[i]->addForce(this->circles[i]->getPosition() - this->circles[j]->getPosition(), ofMap(distance, 0, this->range, 1.2, 0.1)); this->circles[j]->addForce(this->circles[j]->getPosition() - this->circles[i]->getPosition(), ofMap(distance, 0, this->range, 1.2, 0.1)); this->near_count[i]++; this->near_count[j]++; } } } this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { for (int i = 0; i < this->circles.size(); i++) { ofSetColor(39); ofDrawCircle(this->circles[i].get()->getPosition(), this->radius); for (int j = i + 1; j < this->circles.size(); j++) { float distance = this->circles[i]->getPosition().distance(this->circles[j]->getPosition()); if (distance < this->range) { ofDrawLine(this->circles[i]->getPosition(), this->circles[j]->getPosition()); } } ofSetColor(239); this->font.drawString(std::to_string(this->near_count[i]), this->circles[i].get()->getPosition().x - this->font_size * 0.5 , this->circles[i].get()->getPosition().y + this->font_size * 0.5); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |