ウインドウサイズの変更時の留意点
実行時にウインドウサイズを変更した際、表示が上手くいかない時。
・対象のオブジェクトをwindowResized()内に記述。
・windowResized()ブロック内の最初に、対象のオブジェクトをclear()した後再定義して、初期化。
ex.
[ofApp.h]
ofPolyline groundLine;
int w = ofGetWidth();
int h = ofGetHeight();
int lX1 = w/10;
int lX2 = w-w/10;
int lY = h-h/10;
[ofApp.cpp]
void ofApp::windowResized(int w, int h){
groundLine.clear();
int lX1 = w/10;
int lX2 = w-w/10;
int lY = h-h/10;
world2d.createGround(lX1, lY, lX2, lY);
groundLine.addVertex(lX1, lY);
groundLine.addVertex(lX2, lY);
}
関数を初期化する場合は、clear()関数定義し、その中に数値を書いて初期化。
対象の関数の前で呼び出す。
[particleVec2.h]
particleVec2::particleVec2(){
friction = 0.01;
radius = 5.0;
mass = 1.0;
position = ofVec2f(ofGetWidth()/2.0, ofGetHeight()/2.0);
velocity = ofVec2f(0,0);
minx = 0;
miny = 0;
maxx = ofGetWidth();
maxy = ofGetHeight();
}
void particleVec2::bounceOffWalls(){
if(position.x > maxx){
position.x = maxx;
velocity.x *= -1;
}
if(position.x < minx){
position.x = minx;
velocity.x *= -1;
}
if(position.y > maxy){
position.y = maxy;
velocity.y *= -1;
}
if(position.x < miny){
position.y = miny;
velocity.y *= -1;
}
}
void particleVec2::clear(){
minx = 0;
miny = 0;
maxx = ofGetWidth();
maxy = ofGetHeight();
}
[ofApp.cpp]
void ofApp::update(){
for(int i = 0; i<num; i++){
particle[i].addForce(0,1.0);
particle[i].bounceOffWalls();
particle[i].update();
}
}
void ofApp::windowResized(int w, int h){
for(int i = 0; i<num; i++){
particle[i].clear();
particle[i].bounceOffWalls();
}
}