[ Video ]
[ About ]
領域に閉じ込められた帯の群れ。
A group of bands trapped in a realm.
[ 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 |
#pragma once #include "ofMain.h" #include "ofxBullet.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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; ofCamera cam; ofxBulletWorldRigid world; unique_ptr<ofxBulletCustomShape> bounds_shape; vector<unique_ptr<ofxBulletSphere>> sphere_list; vector<vector<glm::vec3>> log_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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofEnableDepthTest(); // Initialize ofxBulletWorld this->cam.setPosition(glm::vec3(0, 3.f, -5.f)); this->cam.lookAt(glm::vec3(0, 0, 0), glm::vec3(0, -1, 0)); this->world.setup(); this->world.enableGrabbing(); this->world.setCamera(&this->cam); this->world.setGravity(ofVec3f()); // Create Big Box ofVec3f start_location; ofPoint dimens; float bounds_width = 300; float h_width = bounds_width * 0.5; float depth = 10.; float h_depth = depth * 0.5; this->bounds_shape.reset(new ofxBulletCustomShape()); this->bounds_shape->create(world.world, ofVec3f(0, 0, 0), 10.); for (int i = 0; i < 6; i++) { if (i == 0) { // ground // start_location.set(0., h_width + h_depth, 0.); dimens.set(bounds_width, depth, bounds_width); } else if (i == 1) { // back wall // start_location.set(0, 0, h_width + h_depth); dimens.set(bounds_width, bounds_width, depth); } else if (i == 2) { // right wall // start_location.set(h_width + h_depth, 0, 0.); dimens.set(depth, bounds_width, bounds_width); } else if (i == 3) { // left wall // start_location.set(-h_width - h_depth, 0, 0.); dimens.set(depth, bounds_width, bounds_width); } else if (i == 4) { // ceiling // start_location.set(0, -h_width - h_depth, 0.); dimens.set(bounds_width, depth, bounds_width); } else if (i == 5) { // front wall // start_location.set(0, 0, -h_width - h_depth); dimens.set(bounds_width, bounds_width, depth); } btBoxShape* boxShape = ofBtGetBoxCollisionShape(dimens.x, dimens.y, dimens.z); this->bounds_shape->addShape(boxShape, start_location); } this->bounds_shape->add(); // Create Spheres for (int i = 0; i < 255; i++) { unique_ptr<ofxBulletSphere> sphere(new ofxBulletSphere); float size = 5; sphere->create(this->world.world, ofVec3f(ofRandom(-100, 100), ofRandom(-100, 100), ofRandom(-100, 100)), 1, size); sphere->setRestitution(1.0); sphere->add(); this->sphere_list.push_back(move(sphere)); vector<glm::vec3> log; this->log_list.push_back(log); } } //-------------------------------------------------------------- void ofApp::update() { ofSeedRandom(39); for (int i = 0; i < this->sphere_list.size(); i++) { this->log_list[i].push_back(this->sphere_list[i]->getPosition()); while (this->log_list[i].size() > 30) { this->log_list[i].erase(this->log_list[i].begin()); } } // Big Box Moving To Center if (!ofGetMousePressed()) { ofVec3f diff = ofVec3f(0, 0, 0) - bounds_shape->getPosition(); diff *= 500.f; bounds_shape->applyCentralForce(diff); } // Compute Spheres Relation for (int i = 0; i < this->sphere_list.size(); i++) { for (int j = i + 1; j < this->sphere_list.size(); j++) { float distance = this->sphere_list[i]->getPosition().distance(this->sphere_list[j]->getPosition()); if (distance < 80) { ofVec3f diff = this->sphere_list[i]->getPosition() - this->sphere_list[j]->getPosition(); ofMap(distance, 0, 80, 20, -2); this->sphere_list[i]->applyCentralForce(diff); this->sphere_list[j]->applyCentralForce(-diff); } } } this->world.update(); } //-------------------------------------------------------------- void ofApp::draw() { ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5); // Draw Big Box ofNoFill(); if (ofGetMousePressed()) { ofSetColor(39, 39, 239); } else { ofSetColor(39); } bounds_shape->transformGL(); ofDrawBox(glm::vec3(0, 0, 0), 300); bounds_shape->restoreTransformGL(); for (int i = 0; i < this->sphere_list.size(); i++) { glm::vec3 param; switch ((int)ofRandom(3)) { case 0: param = glm::vec3(5, 0, 0); break; case 1: param = glm::vec3(0, 5, 0); break; case 2: param = glm::vec3(0, 0, 5); break; } ofMesh face, frame; frame.setMode(ofPrimitiveMode::OF_PRIMITIVE_LINES); for (int k = 0; k < this->log_list[i].size(); k++) { auto& log = this->log_list[i][k]; face.addVertex(log + param); face.addVertex(log - param); frame.addVertex(log + param); frame.addVertex(log - param); if (k > 0) { face.addIndex(k * 2); face.addIndex(k * 2 - 1); face.addIndex(k * 2 - 2); face.addIndex(k * 2); face.addIndex(k * 2 + 1); face.addIndex(k * 2 - 1); frame.addIndex(k * 2); frame.addIndex(k * 2 - 2); frame.addIndex(k * 2 + 1); frame.addIndex(k * 2 - 1); } } frame.addIndex(0); frame.addIndex(1); frame.addIndex(frame.getNumVertices() - 1); frame.addIndex(frame.getNumVertices() - 2); ofSetColor(39); face.drawFaces(); ofSetColor(239); frame.drawWireframe(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |