[ Video ]
[ About ]
560日前にProcessing + Box2dで作ったやつをopenFrameworks + ofxBox2dでサウンド付きでリメイク
I made this work using Processing and JBox2D 560 days ago. I added a sound and remade it using openFrameworks and ofxBox2d.
[ 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 "ofxBox2d.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) {}; 	void contactStart(ofxBox2dContactArgs &e); 	void contactEnd(ofxBox2dContactArgs &e); 	ofxBox2d box2d; 	vector<unique_ptr<Particle>> particles; 	vector<tuple<ofPoint, ofColor, float>> waves; }; enum WaveFileds{ Point, Color, Life };  | 
					
| 
					 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 123 124 125 126 127 128  | 
						#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { 	ofSetFrameRate(60); 	ofSetWindowTitle("Insta"); 	ofBackground(239); 	ofSetLineWidth(3); 	ofSetCircleResolution(72); 	this->box2d.init(); 	this->box2d.enableEvents(); 	this->box2d.setGravity(0, 10); 	this->box2d.createBounds(); 	this->box2d.setFPS(60); 	this->box2d.registerGrabbing(); 	ofAddListener(box2d.contactStartEvents, this, &ofApp::contactStart); 	ofAddListener(box2d.contactEndEvents, this, &ofApp::contactEnd); 	for (int i = 0; i < 3; i++) { 		unique_ptr<Particle> particle(new Particle(this->box2d, ofPoint(i * 240 + 120, i % 2 == 1 ? ofGetHeight() * 0.4 : ofGetHeight() * 0.6), true)); 		this->particles.push_back(std::move(particle)); 	} } //-------------------------------------------------------------- void ofApp::update() { 	if (ofGetFrameNum() % 45 == 0) { 		unique_ptr<Particle> particle(new Particle(this->box2d, ofPoint(ofRandom(ofGetWidth()), ofGetHeight() * 0.05))); 		this->particles.push_back(std::move(particle)); 	} 	for (int i = this->particles.size() - 1; i >= 0; i--) { 		this->particles[i]->Update(); 		if (this->particles[i]->IsDead()) { 			this->particles.erase(this->particles.begin() + i); 		} 	} 	for (int i = this->waves.size() - 1; i >= 0; i--) { 		if (get<WaveFileds::Life>(this->waves[i])++ > 120) { 			this->waves.erase(this->waves.begin() + i); 		} 	} 	this->box2d.update(); 	ofSoundUpdate(); } //-------------------------------------------------------------- void ofApp::draw() { 	ofNoFill(); 	for (int i = 0; i < this->waves.size(); i++) { 		ofSetColor(get<WaveFileds::Color>(this->waves[i])); 		ofDrawCircle(get<WaveFileds::Point>(this->waves[i]), get<WaveFileds::Life>(this->waves[i])); 	} 	for (int i = 0; i < this->particles.size(); i++) { 		this->particles[i]->Draw(); 	} } //-------------------------------------------------------------- void ofApp::contactStart(ofxBox2dContactArgs &e) { 	if (e.a != NULL && e.b != NULL) { 		if (e.a->GetType() == b2Shape::e_circle && e.b->GetType() == b2Shape::e_circle) { 			Particle* p_a = (Particle*)e.a->GetBody()->GetUserData(); 			Particle* p_b = (Particle*)e.b->GetBody()->GetUserData(); 			if (p_a != nullptr && p_b != nullptr) { 				if (p_a->IsObstruct() && p_b->IsObstruct() == false) { 					p_a->SetColor(p_b->GetColor()); 					p_b->PlaySound(); 					tuple<ofPoint, ofColor, float> wave = make_tuple(p_a->GetPoint(), p_b->GetColor(), 80); 					this->waves.push_back(wave); 				} 				if (p_b->IsObstruct() && p_a->IsObstruct() == false) { 					p_b->SetColor(p_a->GetColor()); 					p_a->PlaySound(); 					tuple<ofPoint, ofColor, float> wave = make_tuple(p_b->GetPoint(), p_a->GetColor(), 80); 					this->waves.push_back(wave); 				} 			} 			return; 		} 	} } //-------------------------------------------------------------- void ofApp::contactEnd(ofxBox2dContactArgs &e) { 	if (e.a != NULL && e.b != NULL) { 		// do nothing 	} } //-------------------------------------------------------------- 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 20 21 22 23 24 25 26 27 28  | 
						#pragma once #include "ofMain.h" #include "ofxBox2d.h" class Particle { public: 	Particle(ofxBox2d& box2d, ofPoint point, bool is_obstruct = false); 	~Particle(); 	void Update(); 	void Draw(); 	void PlaySound(); 	void SetColor(ofColor color); 	ofColor GetColor(); 	ofPoint GetPoint(); 	bool IsObstruct(); 	bool IsDead(); private: 	ofxBox2dCircle* circle = nullptr; 	int sound_type; // 0 - 7 	ofSoundPlayer sound; 	ofColor color; 	float life; 	bool is_obstruct; };  | 
					
| 
					 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  | 
						#include "Particle.h" Particle::Particle(ofxBox2d& box2d, ofPoint point, bool is_obstruct) { 	string sound_path_list[] = { "sound/pianoC.mp3", "sound/pianoD.mp3", "sound/pianoE.mp3", "sound/pianoF.mp3", "sound/pianoG.mp3", "sound/pianoA.mp3", "sound/pianoB.mp3", "sound/pianoC2.mp3" }; 	this->circle = new ofxBox2dCircle(); 	if (is_obstruct) { 		this->circle->setPhysics(0.0, 1.0, 1.0); 		this->circle->setup(box2d.getWorld(), point.x, point.y, 80); 		this->circle->setData(this); 		this->sound_type = -1; 		this->color = ofColor(39); 	} 	else { 		this->circle = new ofxBox2dCircle(); 		this->circle->setPhysics(1.0, 0.3, 1.0); 		this->circle->setup(box2d.getWorld(), point.x, point.y, 15); 		this->circle->setData(this); 		this->sound_type = ofRandom(8); 		this->sound.load(sound_path_list[sound_type]); 		this->sound.setMultiPlay(true); 		this->sound.setVolume(0.5); 		this->color.setHsb(ofMap(this->sound_type, 0, 7, 0, 255), 255, 255); 	} 	this->life = 255.f; 	this->is_obstruct = is_obstruct; } Particle::~Particle(){ 	if (this->circle != nullptr) { 		this->circle->destroy(); 		delete this->circle; 	} } void Particle::Update() { 	if (this->is_obstruct == false) { 		this->life -= 1.0; 	} } void Particle::Draw() { 	ofFill(); 	ofSetColor(this->color, this->life > 64 ? 255 : ofMap(this->life, 0, 64, 0, 255)); 	ofDrawCircle(this->circle->getPosition(), this->circle->getRadius()); 	if (this->is_obstruct == false) { 		ofNoFill(); 		ofSetColor(this->color); 		ofDrawCircle(this->circle->getPosition(), this->circle->getRadius()); 	} } void Particle::PlaySound() { 	if (this->is_obstruct) { 		return; 	} 	this->sound.play(); } void Particle::SetColor(ofColor color) { this->color = color; } ofColor Particle::GetColor() { return this->color; } ofPoint Particle::GetPoint() { return this->circle->getPosition(); } bool Particle::IsObstruct() { return this->is_obstruct; } bool Particle::IsDead() { return this->life < 0 ? true : false; }  |