[ Video ]
[ About ]
Particleで雪っぽく出来るかと思ったけど、そんな事は無かった。
Merry Christmas!
[ 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 "ofFbo.h" #include "Particle.h" #include "Cleator.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) {}; ofFbo fbo; vector<Particle*> particles; vector<Cleator*> cleators; }; |
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 |
#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); } for (int i = this->cleators.size() - 1; i > -1; i--) { delete this->cleators[i]; this->cleators.erase(this->cleators.begin() + i); } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofBackground(0); ofSetWindowTitle("Insta"); this->fbo.allocate(ofGetWidth(), ofGetHeight()); for (int i = 0; i < 12; i++) { this->cleators.push_back(new Cleator()); } } //-------------------------------------------------------------- void ofApp::update() { ofColor body_color = ofColor(255); for (int i = 0; i < this->cleators.size(); i++) { ofVec2f location = this->cleators[i]->get_location(); ofVec2f velocity = this->cleators[i]->get_velocity(); if (velocity.length() > 5) { this->particles.push_back(new Particle(location, velocity * 0.3, body_color)); } } 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); } } for (int i = 0; i < this->cleators.size(); i++) { this->cleators[i]->update(); } this->fbo.begin(); ofClear(0); ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2); float distance; for (int i = 0; i < this->particles.size(); i++) { for (int j = i; j < this->particles.size(); j++) { distance = this->particles[i]->get_location().distanceSquared(this->particles[j]->get_location()); if (distance < 400) { ofSetColor(this->particles[i]->get_body_color(), this->particles[i]->get_alpha()); ofDrawLine(this->particles[i]->get_location(), this->particles[j]->get_location()); } } } for (int i = 0; i < this->cleators.size(); i++) { this->cleators[i]->draw(); } this->fbo.end(); } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); } //-------------------------------------------------------------- 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" class Particle { public: Particle(); Particle(ofVec2f location, ofVec2f velocity, ofColor body_color); ~Particle(); void update(); void draw(); ofVec2f get_location(); ofColor get_body_color(); float get_alpha(); 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 32 33 34 35 36 37 38 39 40 41 42 |
#include "Particle.h" Particle::Particle() : Particle(ofVec2f(0, 0), ofVec2f(0, 0), ofColor(128)) { } Particle::Particle(ofVec2f location, ofVec2f velocity, ofColor body_color) { this->location = location; this->velocity = velocity; this->alpha = 255; this->body_size = velocity.length() / 2; this->body_color = body_color; } Particle::~Particle() { } void Particle::update() { this->location += this->velocity; this->alpha -= 1; } void Particle::draw() { } ofVec2f Particle::get_location() { return this->location; } ofColor Particle::get_body_color(){ return this->body_color; } float Particle::get_alpha() { return this->alpha; } bool Particle::isDead() { return this->alpha < 0; } |
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 |
#pragma once #include "ofMain.h" class Cleator { public: Cleator(); ~Cleator(); void applyForce(ofVec2f force); void seek(ofVec2f target); void update(); void draw(); ofVec2f get_location(); ofVec2f get_velocity(); private: ofVec2f location; ofVec2f velocity; ofVec2f acceleration; ofVec2f future; ofVec2f target; float max_speed; float max_force; float radius; float angle; }; |
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 "Cleator.h" Cleator::Cleator() { this->location = ofVec2f(ofRandom(-ofGetWidth() / 2, ofGetWidth() / 2), ofRandom(-ofGetHeight() / 2, ofGetHeight() / 2)); this->velocity = ofVec2f(0, 0); this->acceleration = ofVec2f(0, 0); this->max_speed = 8; this->max_force = 3; this->radius = 15; this->angle = ofRandom(360); } Cleator::~Cleator() { } void Cleator::applyForce(ofVec2f force) { this->acceleration += force; } void Cleator::seek(ofVec2f target) { ofVec2f desired = this->location - target; float distance = desired.length(); desired.normalize(); if (distance < this->radius) { float m = ofMap(distance, 0, this->radius, 0, this->max_speed); desired *= m; } else { desired *= this->max_speed; } ofVec2f steer = this->velocity - desired; steer.limit(this->max_force); applyForce(steer); } void Cleator::update() { this->future.set(this->velocity); this->future.normalize(); this->future *= 60; this->future += this->location; this->angle = ofRandom(-30, 210); float x = 60 * cos(this->angle * DEG_TO_RAD) + future.x; float y = 60 * sin(this->angle * DEG_TO_RAD) + future.y; this->target = ofVec2f(x, y); this->seek(this->target); this->velocity += this->acceleration; this->velocity.limit(max_speed); this->location += this->velocity; if (this->location.x < -ofGetWidth() / 2) { this->location.x = ofGetWidth() / 2; //this->velocity.x *= -1; } else if (this->location.x > ofGetWidth() / 2) { this->location.x = -ofGetWidth() / 2; //this->velocity.x *= -1; } if (this->location.y > ofGetHeight() / 2) { this->location.y = -ofGetHeight() / 2 - 100; } this->acceleration *= 0; this->velocity *= 0.98; } void Cleator::draw() { } ofVec2f Cleator::get_location() { return this->location; } ofVec2f Cleator::get_velocity() { return this->velocity; } |