[ Video ]
[ About ]
Particleをsin関数で移動させています
[ 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 |
#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 |
#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(30); ofBackground(255); ofSetWindowTitle("Insta"); ofEnableSmoothing(); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); for (int i = 0; i < 2048; i++) { this->particles.push_back(new Particle()); } } //-------------------------------------------------------------- void ofApp::update() { for (Particle* p : this->particles) { p->update(); } } //-------------------------------------------------------------- void ofApp::draw() { ofBackground(0); this->cam.begin(); for (Particle* p : this->particles) { p->draw(); } this->cam.end(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(1920, 1080, OF_WINDOW); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include "ofMain.h" class Particle { public: Particle(); ~Particle() {} void update(); void draw(); private: ofVec2f location; float step; float body_size; ofColor body_color; float 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 |
#include "Particle.h" Particle::Particle() { this->location = ofVec2f(ofRandom(-ofGetWidth() / 2, ofGetWidth() / 2), ofRandom(-ofGetHeight() / 2, ofGetHeight() / 2)); this->step = ofRandom(2.0, 5.0); this->body_size = ofRandom(10, 40); this->body_color.setHsb(ofRandom(255), 255, 255); this->alpha = ofRandom(200, 255); } void Particle::update() { float x = this->location.x + this->step; float y = sin(x * this->step * 0.001) * ofGetHeight() * 0.5; //float y = sin(x * this->step * 0.0025) * ofGetHeight() * 0.5; this->location = ofVec2f(x, y); if (this->location.x > ofGetWidth() / 2 + this->body_size) { this->location.x = -ofGetWidth() / 2 - this->body_size; } } void Particle::draw() { ofSetColor(this->body_color, this->alpha); ofDrawEllipse(this->location, this->body_size, this->body_size); } |