[ Video ]
[ About ]
初期位置に戻ろうとするBoxと吸引力をもつSphereを配置しました。昨日の落書きの改造版です。
SphereがBoxを引き付ける力は強いので、Boxは近づいてきたSphereに絡めとられてしまいますが、初期位置との距離が離れれば離れる程、Boxが戻ろうとする力が強くなるので、離れすぎたBoxはSphereから離脱するのが出てきます。
反発・吸引共に面白いので、次はこれをLeap Motionで操る事をしたいですね。
[ 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 32 33 | #pragma once #include "ofMain.h" #include "ofxBullet.h" class ofApp : public ofBaseApp { public: 	~ofApp(); 	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) {}; 	ofEasyCam cam; 	ofLight light; 	ofxBulletWorldRigid world; 	vector<ofxBulletBox*> boxes; 	vector<ofColor> bxes_color; 	vector<ofVec3f> boxes_location; 	ofVec3f hater_location; }; | 
| 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 | #include "ofApp.h" //-------------------------------------------------------------- ofApp::~ofApp() { 	for (ofxBulletBox* tmp : this->boxes) { 		delete tmp; 	} 	this->boxes.clear(); } //-------------------------------------------------------------- void ofApp::setup() { 	ofSetFrameRate(60); 	ofEnableDepthTest(); 	ofBackground(0); 	ofSetWindowTitle("Insta"); 	this->world.setup(); 	this->world.setGravity(ofVec3f(0.0, 0.0, 0.0)); 	float size = 15; 	for (float x = -100; x < 100; x += size) { 		for (float y = -100; y < 100; y += size) { 			for (float z = -100; z < 100; z += size) { 				ofxBulletBox* box = new ofxBulletBox(); 				box->create(this->world.world, ofVec3f(x, y, z), 1.0, size, size, size); 				box->setRestitution(1.0); 				box->add(); 				this->boxes.push_back(box); 				ofColor color; 				color.setHsb(ofRandom(255), 255, 255); 				this->bxes_color.push_back(color); 				this->boxes_location.push_back(ofVec3f(x, y, z)); 			} 		} 	} 	this->light.setPosition(ofVec3f(0, 0, 512)); 	ofEnableLighting(); 	this->light.enable(); } //-------------------------------------------------------------- void ofApp::update() { 	ofVec3f diff; 	for (int i = 0; i < this->boxes.size(); i++) { 		diff = this->boxes_location[i] - this->boxes[i]->getPosition(); 		diff *= 20; 		this->boxes[i]->applyCentralForce(diff); 	} 	float x = ofMap(ofNoise(ofGetFrameNum() * 0.01), 0, 1, -1000, 1000); 	float y = ofMap(ofNoise((ofGetFrameNum() + 100) * 0.01), 0, 1, -1000, 1000); 	float z = ofMap(ofNoise((ofGetFrameNum() + 200) * 0.01), 0, 1, -1000, 1000); 	this->hater_location = ofVec3f(x, y, z); 	for (int i = 0; i < this->boxes.size(); i++) { 		diff = this->boxes[i]->getPosition() - this->hater_location; 		if (diff.length() < 150) { 			diff *= ofMap(diff.length(), 0, 150, -300, 10); 			this->boxes[i]->applyCentralForce(diff); 		} 	} 	this->world.update(); } //-------------------------------------------------------------- void ofApp::draw() { 	this->cam.begin(); 	for (int i = 0; i < this->boxes.size(); i++) { 		ofSetColor(this->bxes_color[i]); 		this->boxes[i]->draw(); 		//ofDrawLine(this->boxes[i]->getPosition(), this->boxes_location[i]); 	} 	ofSetColor(200); 	ofDrawSphere(this->hater_location, 30); 	this->cam.end(); } //======================================================================== int main() { 	ofSetupOpenGL(720, 720, OF_WINDOW); 	ofRunApp(new ofApp()); } |