[ Video ]
[ About ]
一定周期でParticle同士が、お互いに引き合う力・突き放す力が入れ替わります。
[ 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  | 
						#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) {} 	int direction; 	ofxBox2d box2d; 	vector<shared_ptr<ofxBox2dCircle>> circles; 	vector<ofColor> circles_color; };  | 
					
| 
					 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  | 
						#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { 	ofSetFrameRate(60); 	ofBackground(239); 	ofSetWindowTitle("Insta"); 	ofSetColor(39); 	// Initialize ofxBox2d 	this->box2d.init(); 	this->box2d.setGravity(0, 0); 	this->box2d.createBounds(); 	this->box2d.setFPS(60); 	this->box2d.registerGrabbing(); 	// Add Circle 	for (int i = 0; i < 255; i++) { 		float radius = 12; 		auto circle = make_shared<ofxBox2dCircle>(); 		circle->setPhysics(1.0, 0.63, 0.1); 		circle->setup(this->box2d.getWorld(), ofRandom(ofGetWidth() / 2), ofRandom(ofGetHeight() / 2), radius); 		this->circles.push_back(circle); 		ofColor circle_color; 		circle_color.setHsb(ofRandom(255), 239, 239); 		this->circles_color.push_back(circle_color); 	} 	this->direction = 1; } //-------------------------------------------------------------- void ofApp::update() { 	// Change Power Direction 	if (ofGetFrameNum() % 300 == 0) { 		this->direction *= -1; 	} 	// Circles Compute Power 	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 < 80) { 				this->circles[i]->addForce(this->circles[i]->getPosition() - this->circles[j]->getPosition(), 0.75 * this->direction); 				this->circles[j]->addForce(this->circles[j]->getPosition() - this->circles[i]->getPosition(), 0.75 * this->direction); 			} 		} 	} 	// Compute box2d 	this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { 	// Draw Circles 	for (int i = 0; i < this->circles.size(); i++) { 		float radius = this->circles[i]->getRadius(); 		ofPoint point = this->circles[i]->getPosition(); 		ofPoint velocity = this->circles[i]->getVelocity(); 		ofSetColor(this->circles_color[i]); 		ofPushMatrix(); 		ofTranslate(point); 		if (velocity.length() < radius) { 			ofDrawCircle(ofPoint(), radius); 		} 		else { 			float velocity_deg = atan2f(velocity.y, velocity.x) * RAD_TO_DEG + 180; 			ofBeginShape(); 			for (int deg = velocity_deg + 90; deg < velocity_deg + 270; deg++) { 				ofVertex(radius * cos(deg * DEG_TO_RAD), radius * sin(deg * DEG_TO_RAD)); 			} 			ofVertex(-velocity * 2); 			ofEndShape(true); 		} 		ofPopMatrix(); 	} } //-------------------------------------------------------------- int main() { 	ofSetupOpenGL(720, 720, OF_WINDOW); 	ofRunApp(new ofApp()); }  |