[ Video ]
[ About ]
パーティクル同士が衝突すると、速度の高い方の色が伝達して変化します。物理エンジンはofxBox2dを使ってます。
When particles collide with each other, color change to color of faster particle. I use physics engine is 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 |
#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<shared_ptr<ofxBox2dJoint>> joints; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("Insta"); ofBackground(239); 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); } //-------------------------------------------------------------- void ofApp::update() { if (ofGetFrameNum() % 1 == 0) { ofPoint point(ofRandom(ofGetWidth()), ofRandom(ofGetHeight() * 0.1)); float radius = ofRandom(10, 15); 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); } } this->box2d.update(); } //-------------------------------------------------------------- void ofApp::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) { Particle* p_a = (Particle*)e.a->GetBody()->GetUserData(); Particle* p_b = (Particle*)e.b->GetBody()->GetUserData(); if (p_a != nullptr && p_b != nullptr) { ofPoint p_a_velocity = p_a->GetVelocity(); ofPoint p_b_velocity = p_b->GetVelocity(); if (p_a_velocity.length() < p_b_velocity.length()) { p_a->SetColor(p_b->GetColor()); } else { p_b->SetColor(p_a->GetColor()); } } } } //-------------------------------------------------------------- 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 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 |
#include "Particle.h" Particle::Particle(ofxBox2d& box2d, ofPoint point, float radius) { this->circle = new ofxBox2dCircle(); this->circle->setPhysics(1.0, 0.5, 1.0); this->circle->setup(box2d.getWorld(), point.x, point.y, radius); this->circle->setData(this); this->color.setHsb(ofRandom(255), 255, 230); this->life = 255; } Particle::~Particle() { if (this->circle != nullptr) { this->circle->destroy(); delete this->circle; } } void Particle::Update() { this->life -= 0.75; } void Particle::Draw() { 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; } |