[ Video ]
[ About ]
♪ 物理エンジンを使ったピアノ演奏
♪ Play piano by physics engine.
[ 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" #include "Obstruct.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<unique_ptr<Obstruct>> obstructs; }; |
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 129 130 131 132 133 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); this->box2d.init(); this->box2d.enableEvents(); this->box2d.setGravity(0, 15); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); ofAddListener(box2d.contactStartEvents, this, &ofApp::contactStart); ofAddListener(box2d.contactEndEvents, this, &ofApp::contactEnd); 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"}; for (int i = 0; i < 8; i++) { unique_ptr<Obstruct> ob(new Obstruct(this->box2d, ofPoint(ofGetWidth() * 0.125 * i + ofGetWidth() * 0.0625 , ofGetHeight() * 0.50), 50, 50, i * 28)); ob->SetSound(sound_path_list[i]); this->obstructs.push_back(std::move(ob)); } } //-------------------------------------------------------------- void ofApp::update() { if (ofGetFrameNum() % 10 == 0) { ofPoint point(ofRandom(ofGetWidth()), 10); float radius = 8; unique_ptr<Particle> p(new Particle(this->box2d, point, radius)); this->particles.push_back(std::move(p)); } 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 = 0; i < this->obstructs.size(); i++) { this->obstructs[i]->Update(); } this->box2d.update(); ofSoundUpdate(); } //-------------------------------------------------------------- void ofApp::draw() { for (int i = 0; i < this->obstructs.size(); i++) { this->obstructs[i]->Draw(); } 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) { // Particle Collide } return; } if (e.a->GetType() == b2Shape::e_polygon && e.b->GetType() == b2Shape::e_circle) { Obstruct* o_a = (Obstruct*)e.a->GetBody()->GetUserData(); Particle* p_b = (Particle*)e.b->GetBody()->GetUserData(); if (o_a != nullptr && p_b != nullptr) { p_b->SetColor(o_a->GetColor()); o_a->SoundPlay(); } return; } if (e.a->GetType() == b2Shape::e_circle && e.b->GetType() == b2Shape::e_polygon) { Particle* p_a = (Particle*)e.a->GetBody()->GetUserData(); Obstruct* o_b = (Obstruct*)e.b->GetBody()->GetUserData(); if (p_a != nullptr && o_b != nullptr) { p_a->SetColor(o_b->GetColor()); o_b->SoundPlay(); } 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 |
#pragma once #include "ofMain.h" #include "ofxBox2d.h" class Obstruct { public: Obstruct(ofxBox2d& box2d, ofPoint point, float width, float height, float fue); ~Obstruct(); void Update(); void Draw(); void SoundPlay(); void SetSound(string soude_path); ofColor GetColor(); private: ofxBox2dRect* rect = nullptr; ofColor color; ofSoundPlayer sound; vector<float> waves; }; |
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 |
#include "Obstruct.h" Obstruct::Obstruct(ofxBox2d& box2d, ofPoint point, float width, float height, float hue) { this->rect = new ofxBox2dRect(); this->rect->setPhysics(0.0, 1.0, 1.0); this->rect->setup(box2d.getWorld(), point.x , point.y, width, height); this->rect->setData(this); this->color.setHsb(hue, 255, 230); } Obstruct::~Obstruct() { if (this->rect != nullptr) { this->rect->destroy(); delete this->rect; } } void Obstruct::Update() { for (int i = this->waves.size() - 1; i >= 0; i -= 1) { this->waves[i] += 1; if (this->waves[i] > 60) { this->waves.erase(this->waves.begin() + i); } } } void Obstruct::Draw() { float width = this->rect->getWidth(); float height = this->rect->getHeight(); ofFill(); ofSetColor(this->color); ofDrawRectangle(this->rect->getPosition() + ofPoint(width * -0.5, height * -0.5) , width, height); for (int i = 0; i < this->waves.size(); i++) { ofNoFill(); ofSetColor(this->color, ofMap(this->waves[i], 0, 60, 255, 0)); float wave_width = width + waves[i]; float wave_height = height + waves[i]; ofDrawRectangle(this->rect->getPosition() + ofPoint(wave_width * -0.5, wave_height * -0.5), wave_width, wave_height); } } void Obstruct::SoundPlay() { this->sound.play(); this->waves.push_back(0); } // Setter void Obstruct::SetSound(string sound_path) { this->sound.load(sound_path); this->sound.setVolume(0.5); this->sound.setMultiPlay(true); } // Getter ofColor Obstruct::GetColor() { return this->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 |
#pragma once #include "ofMain.h" #include "ofxBox2d.h" class Particle { public: Particle(ofxBox2d& box2d, ofPoint point, float radius); ~Particle(); void Update(); void Draw(); void SetColor(ofColor color); ofColor GetColor(); ofPoint GetVelocity(); bool IsDead(); private: ofxBox2dCircle* circle = nullptr; ofColor color; float 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 |
#include "Particle.h" Particle::Particle(ofxBox2d& box2d, ofPoint point, float radius) { this->circle = new ofxBox2dCircle(); this->circle->setPhysics(1.0, 0.3, 1.0); this->circle->setup(box2d.getWorld(), point.x, point.y, radius); this->circle->setData(this); this->color = ofColor(39); this->life = 255; } Particle::~Particle() { if (this->circle != nullptr) { this->circle->destroy(); delete this->circle; } } void Particle::Update() { this->life -= 1.5; } 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()); } // Setter void Particle::SetColor(ofColor color) { this->color = color; } // Getter ofColor Particle::GetColor() { return this->color; } ofPoint Particle::GetVelocity() { return this->circle->getVelocity(); } bool Particle::IsDead() { return this->life <= 0 ? true : false; } |