Created
October 18, 2015 10:02
-
-
Save micromeeeter/6fd9dd6fa21f3b6d3134 to your computer and use it in GitHub Desktop.
rain_2D_a : make raindrops with openFrameworks which use one parameter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
//-------------------------------------------------------------- | |
void ofApp::setup(){ | |
ofSetFrameRate(60); | |
ofSetBackgroundAuto(false); | |
ofBackground(0, 0, 0); | |
ofEnableBlendMode(OF_BLENDMODE_ALPHA); | |
control = true; //state of log, true -> on; false-> off log画面オンオフの状態 true->あり false->なし | |
} | |
//-------------------------------------------------------------- | |
void ofApp::update(){ | |
theta = PI / 2 + PI / 2 / 700 * pos.size(); //theta of raindrops, perpendicular = PI / 2 雨粒が落ちる角度 PI/2が鉛直 | |
float speed = pos.size() / 15;; //a speed of raindrops 雨粒が落ちる速度 | |
for(int i = 0; i < pos.size(); i++) { | |
//convert speed from vector to x, y 雨粒が落ちる速度に傾きをつけてx,yに変換 | |
ofVec2f vel; | |
vel.x = ofGetHeight() / 2300.0f * (length[i] + speed) * cos(theta + noise[i]); | |
vel.y = ofGetHeight() / 2300.0f * (length[i] + speed) * sin(theta + noise[i]); | |
pos[i].x += vel.x; | |
pos[i].y += vel.y; | |
//if a raindrop reach the bottom of window, it returns first position 画面下まで落ちきった時に上に戻る | |
if(pos[i].y - length[i] * sin(theta + noise[i]) > ofGetHeight()){ | |
pos[i].y = 0; | |
pos[i].x -= vel.x * (ofGetHeight() / vel.y); | |
} | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw(){ | |
ofSetColor(0, 0, 0, 150); | |
ofRect(0, 0, ofGetWidth(), ofGetHeight()); //fadeout フェードアウト | |
ofSetColor(255, 255, 255); //color of letters and raindrops 文字と雨粒の色 | |
for(int i = 0; i < pos.size(); i++) { | |
//coordinate of a raindrop 雨粒の座標 | |
float x1 = pos[i].x; | |
float y1 = pos[i].y; | |
float x2 = pos[i].x - length[i] * cos(theta + noise[i]); | |
float y2 = pos[i].y - length[i] * sin(theta + noise[i]); | |
ofSetLineWidth(length[i] / 40); //width of a raindrop 雨粒の太さ | |
ofLine(x1, y1, x2, y2); //draw a raindrop雨粒の描画 | |
} | |
//log画面 | |
if(control == true){ | |
string log; | |
log = "raindrops num = " + ofToString(pos.size(), 0) + "\n"; | |
log += "framerate = " + ofToString(ofGetFrameRate(), 4) + "\n"; | |
log += "1:add 5 raindrops, 2:subtract 5 raindrops \n"; | |
log += "c:clear raindrops num, f:fullScreen \n"; | |
log += "press spacekey to change log on/off"; | |
ofDrawBitmapString(log, 20, 20); | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyPressed(int key){ | |
if(key == '1'){ | |
//add 5 raindrops 雨粒の個数を5ずつ増やす | |
for(int i = 0; i < 5; i++){ | |
//define first position 初期位置 | |
ofVec2f p; | |
float x = ofRandom(ofGetWidth() + ofGetHeight()); | |
p.set(x, 0); | |
pos.push_back(p); | |
//define length 雨粒の長さ | |
float l = ofRandom(ofGetHeight() / 60, ofGetHeight() / 10); | |
length.push_back(l); | |
//define speed 雨粒の速さ | |
ofVec2f v; | |
float vx = length[length.size() - 1] / 10 * cos(theta); | |
float vy = length[length.size() - 1] / 10 * sin(theta); | |
v.set(vx, vy); | |
velocity.push_back(v); | |
//define noise of slant 傾きのバラつき具合 | |
float n; | |
n = ofRandom(-PI/90, PI/90); | |
noise.push_back(n); | |
} | |
} | |
if(key == '2'){ | |
//subtract 5 raindrops 雨粒の個数を5ずつ減らす | |
if(pos.size() > 0){ | |
for(int i = 0; i < 5; i++){ | |
pos.pop_back(); | |
length.pop_back(); | |
velocity.pop_back(); | |
noise.pop_back(); | |
} | |
} | |
} | |
if(key == 'c'){ | |
//clear raindrops 雨粒の個数を0個にする | |
pos.clear(); | |
length.clear(); | |
velocity.clear(); | |
noise.clear(); | |
} | |
if(key == 'f'){ | |
ofSetFullscreen(true); | |
} | |
if(key == ' '){ | |
//state of log logの表示の有無 | |
control =! control; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
class ofApp : public ofBaseApp{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void keyPressed(int key); | |
void keyReleased(int key); | |
vector <ofVec2f> pos; //a position of the raindrop 雨粒の位置 | |
vector <float> length; //a length of the raindop 雨粒の長さ | |
vector <ofVec2f> velocity; //a speed of the raindrop 雨粒の速度 | |
vector <float> noise; //a noise of a slant of the raidrop 個々の雨粒の向きのブレ具合 | |
float theta; //general slamt of raindrops 雨粒の基準の傾き | |
bool control; //switch of log log画面のon/off | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment