[ Video ]
[ About ]
ノイズで変化する図形をBox2dのポリゴンに変換。
Convert shapes that change with noise into Box2d polygons.
[ 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" 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) {} ofxBox2d box2d; vector<ofColor> color_list; float noise_param; vector<shared_ptr<ofxBox2dPolygon>> polygon_list; vector<float> polygon_life_list; vector<ofColor> polygon_color_list; }; |
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetLineWidth(2); this->box2d.init(); this->box2d.setGravity(0, 25); this->box2d.createBounds(); this->box2d.setFPS(60); this->noise_param = ofRandom(1000); ofColor color; vector<int> hex_list = { 0xef476f, 0xffd166, 0x06d6a0, 0x118ab2, 0x073b4c }; for (auto hex : hex_list) { color.setHex(hex); this->color_list.push_back(color); } } //-------------------------------------------------------------- void ofApp::update() { if(ofGetFrameNum() % 60 == 45) { int x_span = 20; int height = 60; for (int x = x_span * 3; x <= ofGetWidth() - x_span * 3; x += x_span) { auto noise_value = ofNoise(x * 0.02, 360, this->noise_param); vector<glm::vec2> vertices; vertices.push_back(glm::vec2(x - x_span * 0.5, 360)); vertices.push_back(glm::vec2(x - x_span * 0.5 + x_span * 0.8, 360)); vertices.push_back(glm::vec2(x - x_span * 0.5 + x_span * 0.8, 360 + ofMap(noise_value, 0, 1, -1, -height))); vertices.push_back(glm::vec2(x - x_span * 0.5, 360 + ofMap(noise_value, 0, 1, -1, -height))); auto polygon = make_shared<ofxBox2dPolygon>(); polygon->addTriangle(vertices[0], vertices[1], vertices[2]); polygon->setPhysics(1.0, 0.7, 0.3); polygon->create(box2d.getWorld()); this->polygon_list.push_back(move(polygon)); this->polygon_life_list.push_back(128); this->polygon_color_list.push_back(this->color_list[(int)ofRandom(this->color_list.size())]); polygon = make_shared<ofxBox2dPolygon>(); polygon->addTriangle(vertices[0], vertices[2], vertices[3]); polygon->setPhysics(1.0, 0.7, 0.3); polygon->create(box2d.getWorld()); this->polygon_list.push_back(move(polygon)); this->polygon_life_list.push_back(128); this->polygon_color_list.push_back(this->color_list[(int)ofRandom(this->color_list.size())]); } } for (int i = this->polygon_list.size() - 1; i > -1; i--) { this->polygon_life_list[i] -= 1; if (this->polygon_life_list[i] < 0) { this->polygon_list[i]->destroy(); this->polygon_list.erase(this->polygon_list.begin() + i); this->polygon_life_list.erase(this->polygon_life_list.begin() + i); this->polygon_color_list.erase(this->polygon_color_list.begin() + i); } } this->box2d.update(); if (ofGetFrameNum() % 60 < 45) { this->noise_param += ofMap(ofGetFrameNum() % 60, 0, 45, 0.1, 0); } } //-------------------------------------------------------------- void ofApp::draw() { ofNoFill(); ofSetColor(39); int x_span = 20; int height = 60; for (int x = x_span * 3; x <= ofGetWidth() - x_span * 3; x += x_span) { auto noise_value = ofNoise(x * 0.02, 360, this->noise_param); ofDrawRectangle(x - x_span * 0.5, 360, x_span * 0.8, ofMap(noise_value, 0, 1, -1, -height)); } ofFill(); for (int i = 0; i < this->polygon_list.size(); i++) { if (this->polygon_life_list[i] > 64) { ofSetColor(this->polygon_color_list[i]); } else { ofSetColor(this->polygon_color_list[i], ofMap(this->polygon_life_list[i], 64, 0, 255, 0)); } this->polygon_list[i]->draw(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
[ Link ]
https://github.com/junkiyoshi/Insta20210720
[ Kinako ]