[ Video ]
[ About ]
マウスの加速度を計算して、Particleインスタンスを追加。
加速度は、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 29 30 |
#pragma once #include "ofMain.h" #include "Particle.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 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) {}; ofEasyCam cam; vector<Particle*> particles; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- ofApp::~ofApp(){ for (int i = this->particles.size() - 1; i > -1; i--) { delete this->particles[i]; this->particles.erase(this->particles.begin() + i); } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(255); ofSetWindowTitle("Insta"); } //-------------------------------------------------------------- void ofApp::update(){ ofVec2f mouse_point = ofVec2f(ofGetMouseX() - ofGetWidth() / 2, ofGetHeight() / 2 - ofGetMouseY()); ofVec2f prev_mouse_point = ofVec2f(ofGetPreviousMouseX() - ofGetWidth() / 2, ofGetHeight() / 2 - ofGetPreviousMouseY()); ofVec2f velocity = prev_mouse_point - mouse_point; if (velocity.length() > 5) { this->particles.push_back(new Particle(mouse_point, velocity)); } for (int i = this->particles.size() - 1; i > -1; i--) { this->particles[i]->update(); if (this->particles[i]->isDead()){ delete this->particles[i]; this->particles.erase(this->particles.begin() + i); } } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); for (Particle* p : this->particles) { p->draw(); } this->cam.end(); } //-------------------------------------------------------------- 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 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); Particle(ofVec2f location, ofVec2f velocity); ~Particle(); void update(); void draw(); bool isDead(); private: ofVec2f location; ofVec2f velocity; float body_size; ofColor body_color; int alpha; }; |
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 |
#include "Particle.h" Particle::Particle() : Particle(ofVec2f(0, 0), ofVec2f(0, 0)){ } Particle::Particle(ofVec2f location, ofVec2f velocity) { this->location = location; this->velocity = velocity; this->alpha = 255; this->body_size = velocity.length() * 3; this->body_color.setHsb(ofRandom(255), 255, 255); } Particle::~Particle(){ } void Particle::update() { this->location += this->velocity; this->alpha -= 5; } void Particle::draw() { ofSetColor(this->body_color, this->alpha); ofEllipse(this->location, this->body_size, this->body_size); } bool Particle::isDead() { return this->alpha < 0; } |
[ Link ]