[ Video ]
[ About ]
物理計算はofxBox2dで計算する。
速度を持っている間は文字が切り替わって、静止中は切り替わらない。
Physics calculations are done in ofxBox2d.
The character switches while it has speed, and does not switch while at rest.
半角カナを追加するには → https://junkiyoshi.com/openframeworks20210809/
Box2Dを動かすには → https://qiita.com/junkiyoshi/items/68e602568a0c94840bec
[ 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 33 34 35 36 37 38 39 |
#pragma once #include "ofMain.h" #include "ofxBox2d.h" class dCharacter { public: dCharacter() {}; void update(vector<string>& words); shared_ptr<ofxBox2dRect> rect; int index = 0; }; 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) {}; int font_size; ofTrueTypeFont font; vector<string> words; ofxBox2d box2d; vector<dCharacter> drop_char_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 |
#include "ofApp.h" //-------------------------------------------------------------- void dCharacter::update(vector<string>& words) { if (ofGetFrameNum() % 5 && this->rect->getVelocity().length() > 1.5) { this->index = ofRandom(words.size()); } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(239); ofSetColor(39); this->font_size = 20; ofTrueTypeFontSettings font_settings("fonts/msgothic.ttc", this->font_size); font_settings.antialiased = true; font_settings.addRanges(ofAlphabet::Japanese); this->font.load(font_settings); this->words = { //u8"!", u8"#", u8"$", u8"%", u8"&", //u8"'", u8"(", u8")", u8"*", u8"+", //u8",", u8"-", u8".", u8"/", u8"0", //u8"1", u8"2", u8"3", u8"4", u8"5", //u8"6", u8"7", u8"8", u8"9", u8"0", //u8":", u8";", u8"<", u8"=", u8">", //u8"?", u8"@", //u8"A", u8"B", u8"C", u8"D", u8"E", //u8"F", u8"G", u8"H", u8"I", u8"J", //u8"K", u8"L", u8"M", u8"N", u8"O", //u8"P", u8"Q", u8"R", u8"S", u8"T", //u8"U", u8"V", u8"W", u8"X", u8"Y", u8"Z", //u8"a", u8"b", u8"c", u8"d", u8"e", //u8"f", u8"g", u8"h", u8"i", u8"j", //u8"k", u8"l", u8"m", u8"n", u8"o", //u8"p", u8"q", u8"r", u8"s", u8"t", //u8"u", u8"v", u8"w", u8"x", u8"y", u8"z", u8"ア", u8"イ", u8"ウ", u8"エ", u8"オ", u8"カ", u8"キ", u8"ク", u8"ケ", u8"コ", u8"サ", u8"シ", u8"ス", u8"セ", u8"ソ", u8"タ", u8"チ", u8"ツ", u8"テ", u8"ト", u8"ナ", u8"ニ", u8"ヌ", u8"ネ", u8"ノ", u8"ハ", u8"ヒ", u8"フ", u8"ヘ", u8"ホ", u8"マ", u8"ミ", u8"ム", u8"メ", u8"モ", u8"ヤ", u8"ユ", u8"ヨ", u8"ラ", u8"リ", u8"ル", u8"レ", u8"ロ", u8"ワ", u8"ヲ", u8"ン", }; this->box2d.init(); this->box2d.setGravity(0, 15); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); } //-------------------------------------------------------------- void ofApp::update() { if (ofRandom(100) < 35) { auto rect = make_shared<ofxBox2dRect>(); rect->setPhysics(1.0, 0.3, 0.3); rect->setup(this->box2d.getWorld(), ofRandom(ofGetWidth() * 0.5 - 25, ofGetWidth() * 0.5 + 25), 25, this->font_size, this->font_size); auto drop_char = dCharacter(); drop_char.rect = rect; drop_char.index = ofRandom(this->words.size()); this->drop_char_list.push_back(drop_char); } for (auto& drop_char : this->drop_char_list) { drop_char.update(this->words); } this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { for (auto& drop_char : this->drop_char_list) { ofPushMatrix(); ofTranslate(drop_char.rect->getPosition().x, drop_char.rect->getPosition().y); ofRotate(drop_char.rect->getRotation()); this->font.drawString(this->words[drop_char.index], -5, 10); ofPopMatrix(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
[ Link ]
https://github.com/junkiyoshi/Insta20210828
[ Kinako ]