[ Video ]
[ About ]
昨日のやつの3次元バージョン。わかりやすいように箱書きました。
3D Edition.
[ 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 "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) {}; ofEasyCam cam; 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 |
#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(60); ofBackground(0); ofSetWindowTitle("Insta"); ofNoFill(); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); for (int i = 0; i < 12; i++) { this->cleators.push_back(new Cleator()); } } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->cleators.size(); i++) { ofVec3f location = this->cleators[i]->get_location(); ofVec3f velocity = this->cleators[i]->get_velocity(); if (velocity.length() > 5) { this->particles.push_back(new Particle(location, velocity * 0.3)); } } 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(); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofRotateY(ofGetFrameNum() * 0.5); ofSetColor(255, 128); ofDrawBox(720); float distance; for (int i = 0; i < this->particles.size(); i++) { this->particles[i]->draw(); for (int j = i; j < this->particles.size(); j++) { distance = this->particles[i]->get_location().distance(this->particles[j]->get_location()); if (distance < 80) { 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->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 22 23 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); Particle(ofVec3f location, ofVec3f velocity); Particle(ofVec3f location, ofVec3f velocity, ofColor body_color); ~Particle(); void update(); void draw(); ofVec3f get_location(); bool isDead(); private: ofVec3f location; ofVec3f 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 |
#include "Particle.h" Particle::Particle() : Particle(ofVec3f(0, 0), ofVec3f(0, 0)) { } Particle::Particle(ofVec3f location, ofVec3f velocity){ this->location = location; this->velocity = velocity; this->alpha = 255; this->body_size = velocity.length() / 2; this->body_color.setHsb(ofRandom(255), 255, 255); } Particle::Particle(ofVec3f location, ofVec3f 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 -= 3; } void Particle::draw(){ ofSetColor(this->body_color, this->alpha); ofDrawSphere(this->location, this->body_size); } ofVec3f Particle::get_location(){ return this->location; } 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 30 |
#pragma once #include "ofMain.h" class Cleator { public: Cleator(); ~Cleator(); void applyForce(ofVec3f force); void seek(ofVec3f target); void update(); void draw(); ofVec3f get_location(); ofVec3f get_velocity(); private: ofVec3f location; ofVec3f velocity; ofVec3f acceleration; ofVec3f future; ofVec3f target; float max_speed; float max_force; float radius; float angle; float angle_2; }; |
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 |
#include "Cleator.h" Cleator::Cleator(){ this->location = ofVec3f(ofRandom(-1, 1), ofRandom(-1, 1), ofRandom(-1, 1)); this->velocity = ofVec3f(0, 0, 0); this->acceleration = ofVec3f(0, 0, 0); this->max_speed = 8; this->max_force = 3; this->radius = 30; this->angle = ofRandom(360); this->angle_2 = ofRandom(180); } Cleator::~Cleator(){ } void Cleator::applyForce(ofVec3f force){ this->acceleration += force; } void Cleator::seek(ofVec3f target){ ofVec3f 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; } ofVec3f steer = this->velocity - desired; steer.limit(this->max_force); applyForce(steer); } void Cleator::update(){ this->future.set(this->velocity); this->future.normalize(); this->future *= 30; this->future += this->location; this->angle = ofRandom(0, 360); this->angle_2 = ofRandom(0, 180); float x = 30 * cos(this->angle * DEG_TO_RAD) * sin(this->angle_2 * DEG_TO_RAD) + future.x; float y = 30 * sin(this->angle * DEG_TO_RAD) * sin(this->angle_2 * DEG_TO_RAD) + future.y; float z = 30 * cos(this->angle_2 * DEG_TO_RAD) + future.z; this->target = ofVec3f(x, y, z); 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; this->velocity.y *= -1; } else if (this->location.y > ofGetHeight() / 2) { this->location.y = ofGetHeight() / 2; this->velocity.y *= -1; } if (this->location.z < -360) { this->location.z = -360; this->velocity.z *= -1; }else if(this->location.z > 360) { this->location.z = 360; this->velocity.z *= -1; } this->acceleration *= 0; this->velocity *= 0.98; } void Cleator::draw(){ } ofVec3f Cleator::get_location() { return this->location; } ofVec3f Cleator::get_velocity() { return this->velocity; } |