#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofSetFrameRate(60);
ofSetWindowTitle("openFrameworks");
ofBackground(239);
ofSetColor(2);
ofColor color;
vector<int> hex_list = { 0xef476f, 0xffd166, 0x06d6a0, 0x118ab2, 0x073b4c };
for (auto hex : hex_list) {
color.setHex(hex);
this->base_color_list.push_back(color);
}
this->box2d.init();
this->box2d.setGravity(0, 50);
this->box2d.createBounds();
this->box2d.setFPS(60);
this->box2d.registerGrabbing();
}
//--------------------------------------------------------------
void ofApp::update() {
for (int i = this->circle_list.size() - 1; i > -1; i--) {
this->life_list[i] -= 0.75;
if (this->life_list[i] < 0) {
this->circle_list.erase(this->circle_list.begin() + i);
this->radius_list.erase(this->radius_list.begin() + i);
this->color_list.erase(this->color_list.begin() + i);
this->life_list.erase(this->life_list.begin() + i);
}
}
if (ofGetFrameNum() % 15 == 0) {
auto radius = ofRandom(10, 50);
auto circle = make_shared<ofxBox2dCircle>();
circle->setPhysics(0.5, 0.83, 0.1);
circle->setup(this->box2d.getWorld(), ofRandom(ofGetWidth() * 0.5 - radius, ofGetWidth() * 0.5 + radius), radius, radius);
circle->setRotation(ofRandom(360));
this->circle_list.push_back(circle);
this->radius_list.push_back(radius);
int color_index_1 = ofRandom(this->base_color_list.size());
int color_index_2 = ofRandom(this->base_color_list.size());
auto color = make_pair(this->base_color_list[color_index_1], this->base_color_list[color_index_2]);
this->color_list.push_back(color);
this->life_list.push_back(255);
}
this->box2d.update();
}
//--------------------------------------------------------------
void ofApp::draw() {
for (int i = 0; i < this->circle_list.size(); i++) {
this->draw_MAGATAMA(glm::vec2(this->circle_list[i]->getPosition()), this->radius_list[i], this->circle_list[i]->getRotation(), ofColor(this->color_list[i].first, this->life_list[i] > 64 ? 255 : ofMap(this->life_list[i], 64, 0, 255, 0)));
this->draw_MAGATAMA(glm::vec2(this->circle_list[i]->getPosition()), this->radius_list[i], this->circle_list[i]->getRotation() + 180, ofColor(this->color_list[i].second, this->life_list[i] > 64 ? 255 : ofMap(this->life_list[i], 64, 0, 255, 0)));
}
}
//--------------------------------------------------------------
void ofApp::draw_MAGATAMA(glm::vec2 location, float radius, float rotate_deg, ofColor color) {
float len = radius * 0.5522;
float deg = 0;
ofPushMatrix();
ofTranslate(location);
ofRotate(rotate_deg);
vector<glm::vec2> vertices;
vector<pair<glm::vec2, glm::vec2>> handle_list;
vertices.push_back(glm::vec2(0, 0));
deg = 180;
handle_list.push_back(make_pair<glm::vec2, glm::vec2>(
glm::vec2(vertices.back() + glm::vec2(len * 0.5 * cos(deg * DEG_TO_RAD), len * 0.5 * sin(deg * DEG_TO_RAD))),
glm::vec2(vertices.back() + glm::vec2(len * 0.5 * cos((deg + 180) * DEG_TO_RAD), len * 0.5 * sin((deg + 180) * DEG_TO_RAD)))));
vertices.push_back(glm::vec2(radius * 0.5, radius * -0.5));
deg = 90;
handle_list.push_back(make_pair<glm::vec2, glm::vec2>(
glm::vec2(vertices.back() + glm::vec2(len * 0.5 * cos(deg * DEG_TO_RAD), len * 0.5 * sin(deg * DEG_TO_RAD))),
glm::vec2(vertices.back() + glm::vec2(len * 0.5 * cos((deg + 180) * DEG_TO_RAD), len * 0.5 * sin((deg + 180) * DEG_TO_RAD)))));
vertices.push_back(glm::vec2(0, radius * -1));
deg = 0;
handle_list.push_back(make_pair<glm::vec2, glm::vec2>(
glm::vec2(vertices.back() + glm::vec2(len * 0.5 * cos(deg * DEG_TO_RAD), len * 0.5 * sin(deg * DEG_TO_RAD))),
glm::vec2(vertices.back() + glm::vec2(len * cos(deg * DEG_TO_RAD), len * sin(deg * DEG_TO_RAD)))));
vertices.push_back(glm::vec2(radius, 0));
deg = 270;
handle_list.push_back(make_pair<glm::vec2, glm::vec2>(
glm::vec2(vertices.back() + glm::vec2(len * cos(deg * DEG_TO_RAD), len * sin(deg * DEG_TO_RAD))),
glm::vec2(vertices.back() + glm::vec2(len * cos((deg + 180) * DEG_TO_RAD), len * sin((deg + 180) * DEG_TO_RAD)))));
vertices.push_back(glm::vec2(0, radius));
deg = 0;
handle_list.push_back(make_pair<glm::vec2, glm::vec2>(
glm::vec2(vertices.back() + glm::vec2(len * cos(deg * DEG_TO_RAD), len * sin(deg * DEG_TO_RAD))),
glm::vec2(vertices.back() + glm::vec2(len * 0.5 * cos((deg + 180) * DEG_TO_RAD), len * 0.5 * sin((deg + 180) * DEG_TO_RAD)))));
vertices.push_back(glm::vec2(radius * -0.5, radius * 0.5));
deg = 90;
handle_list.push_back(make_pair<glm::vec2, glm::vec2>(
glm::vec2(vertices.back() + glm::vec2(len * 0.5 * cos(deg * DEG_TO_RAD), len * 0.5 * sin(deg * DEG_TO_RAD))),
glm::vec2(vertices.back() + glm::vec2(len * 0.5 * cos((deg + 180) * DEG_TO_RAD), len * 0.5 * sin((deg + 180) * DEG_TO_RAD)))));
ofSetColor(color);
ofFill();
ofBeginShape();
for (int i = 0; i < vertices.size(); i++) {
int n = (i + 1) % vertices.size();
ofVertex(vertices[i]);
ofBezierVertex(handle_list[i].second, handle_list[n].first, vertices[n]);
}
ofNextContour(true);
for (deg = 0; deg <= 360; deg += 5) {
ofVertex(glm::vec2(0, radius * 0.5) + glm::vec2(radius * 0.2 * cos(deg * DEG_TO_RAD), radius * 0.2 * sin(deg * DEG_TO_RAD)));
}
ofEndShape(true);
ofSetColor(ofColor(39, color.a));
ofNoFill();
ofBeginShape();
for (int i = 0; i < vertices.size(); i++) {
int n = (i + 1) % vertices.size();
ofVertex(vertices[i]);
ofBezierVertex(handle_list[i].second, handle_list[n].first, vertices[n]);
}
ofNextContour(true);
for (deg = 0; deg <= 360; deg += 5) {
ofVertex(glm::vec2(0, radius * 0.5) + glm::vec2(radius * 0.2 * cos(deg * DEG_TO_RAD), radius * 0.2 * sin(deg * DEG_TO_RAD)));
}
ofEndShape(true);
ofPopMatrix();
}
//--------------------------------------------------------------
int main() {
ofSetupOpenGL(720, 720, OF_WINDOW);
ofRunApp(new ofApp());
}