[ Video ]
[ About ]
物理演算のポリゴンで矢印を作る。
Make arrows with physics polygons.
最近、新しく開発環境のPCを買い換えました。
Recently, I bought a new PC for my development environment.
[ 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> base_color_list; vector<glm::vec2> noise_param_list; 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(0); ofSetLineWidth(1); this->box2d.init(); this->box2d.setGravity(0, 30); this->box2d.createBounds(); this->box2d.setFPS(60); for (int i = 0; i < 4; i++) { this->noise_param_list.push_back(glm::vec2(ofRandom(1000), ofRandom(1000))); ofColor base_color; base_color.setHsb(ofMap(i, 0, 4, 0, 255), 230, 255); this->base_color_list.push_back(base_color); } } //-------------------------------------------------------------- void ofApp::update() { if (ofGetFrameNum() % 8 == 0) { for (int i = 0; i < this->noise_param_list.size(); i++) { auto location = glm::vec2( ofMap(ofNoise(this->noise_param_list[i].x, ofGetFrameNum() * 0.0075), 0, 1, 20, 700), ofMap(ofNoise(this->noise_param_list[i].y, ofGetFrameNum() * 0.0075), 0, 1, 100, 360)); auto target = glm::vec2( ofMap(ofNoise(this->noise_param_list[i].x, (ofGetFrameNum() + 1) * 0.0075), 0, 1, 20, 700), ofMap(ofNoise(this->noise_param_list[i].y, (ofGetFrameNum() + 1) * 0.0075), 0, 1, 100, 360)); auto size = 30; auto angle = std::atan2(target.y - location.y, target.x - location.x); vector<glm::vec2> vertices; vertices.push_back(location + glm::vec2(size * 0.5 * cos(angle + PI * 0.5), size * 0.5 * sin(angle + PI * 0.5)) * 0.25); vertices.push_back(location + glm::vec2(size * 0.5 * cos(angle + PI * 0.5), size * 0.5 * sin(angle + PI * 0.5))); vertices.push_back(location + glm::vec2(size * cos(angle), size * sin(angle))); vertices.push_back(location + glm::vec2(size * 0.5 * cos(angle - PI * 0.5), size * 0.5 * sin(angle - PI * 0.5))); vertices.push_back(location + glm::vec2(size * 0.5 * cos(angle - PI * 0.5), size * 0.5 * sin(angle - PI * 0.5)) * 0.25); vertices.push_back(location + glm::vec2(size * 0.5 * cos(angle + PI * 0.5), size * 0.5 * sin(angle + PI * 0.5)) * 0.25); vertices.push_back(location + glm::vec2(size * 0.5 * cos(angle + PI * 0.5), size * 0.5 * sin(angle + PI * 0.5)) * 0.25 - glm::vec2(size * cos(angle), size * sin(angle)) * 0.5); vertices.push_back(location + glm::vec2(size * 0.5 * cos(angle - PI * 0.5), size * 0.5 * sin(angle - PI * 0.5)) * 0.25 - glm::vec2(size * cos(angle), size * sin(angle)) * 0.5); vertices.push_back(location + glm::vec2(size * 0.5 * cos(angle - PI * 0.5), size * 0.5 * sin(angle - PI * 0.5)) * 0.25); 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->base_color_list[i]); polygon = make_shared<ofxBox2dPolygon>(); polygon->addTriangle(vertices[0], vertices[2], vertices[4]); 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->base_color_list[i]); polygon = make_shared<ofxBox2dPolygon>(); polygon->addTriangle(vertices[4], 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->base_color_list[i]); polygon = make_shared<ofxBox2dPolygon>(); polygon->addTriangle(vertices[5], vertices[6], vertices[7]); 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->base_color_list[i]); polygon = make_shared<ofxBox2dPolygon>(); polygon->addTriangle(vertices[5], vertices[7], vertices[8]); 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->base_color_list[i]); } } 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(); } //-------------------------------------------------------------- void ofApp::draw() { ofNoFill(); ofSetColor(39); for (int i = 0; i < this->noise_param_list.size(); i++) { auto location = glm::vec2( ofMap(ofNoise(this->noise_param_list[i].x, ofGetFrameNum() * 0.0075), 0, 1, 20, 700), ofMap(ofNoise(this->noise_param_list[i].y, ofGetFrameNum() * 0.0075), 0, 1, 100, 360)); auto target = glm::vec2( ofMap(ofNoise(this->noise_param_list[i].x, (ofGetFrameNum() + 1) * 0.0075), 0, 1, 20, 700), ofMap(ofNoise(this->noise_param_list[i].y, (ofGetFrameNum() + 1) * 0.0075), 0, 1, 100, 360)); auto size = 30; auto angle = std::atan2(target.y - location.y, target.x - location.x); ofSetColor(this->base_color_list[i]); ofNoFill(); ofBeginShape(); ofVertex(location + glm::vec2(size * 0.5 * cos(angle + PI * 0.5), size * 0.5 * sin(angle + PI * 0.5)) * 0.25); ofVertex(location + glm::vec2(size * 0.5 * cos(angle + PI * 0.5), size * 0.5 * sin(angle + PI * 0.5))); ofVertex(location + glm::vec2(size * cos(angle), size * sin(angle))); ofVertex(location + glm::vec2(size * 0.5 * cos(angle - PI * 0.5), size * 0.5 * sin(angle - PI * 0.5))); ofVertex(location + glm::vec2(size * 0.5 * cos(angle - PI * 0.5), size * 0.5 * sin(angle - PI * 0.5)) * 0.25); ofEndShape(); ofBeginShape(); ofVertex(location + glm::vec2(size * 0.5 * cos(angle - PI * 0.5), size * 0.5 * sin(angle - PI * 0.5)) * 0.25); ofVertex(location + glm::vec2(size * 0.5 * cos(angle - PI * 0.5), size * 0.5 * sin(angle - PI * 0.5)) * 0.25 - glm::vec2(size * cos(angle), size * sin(angle)) * 0.5); ofVertex(location + glm::vec2(size * 0.5 * cos(angle + PI * 0.5), size * 0.5 * sin(angle + PI * 0.5)) * 0.25 - glm::vec2(size * cos(angle), size * sin(angle)) * 0.5); ofVertex(location + glm::vec2(size * 0.5 * cos(angle + PI * 0.5), size * 0.5 * sin(angle + PI * 0.5)) * 0.25); ofEndShape(); } ofNoFill(); 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(); } ofFill(); for (int i = 0; i < this->polygon_list.size(); i++) { if (this->polygon_life_list[i] > 64) { ofSetColor(this->polygon_color_list[i], 128); } else { ofSetColor(this->polygon_color_list[i], ofMap(this->polygon_life_list[i], 64, 0, 128, 0)); } this->polygon_list[i]->draw(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |