物理エンジン Box2D
提供:kuhalaboWiki
(版間での差分)
(→入手) |
|||
| 15行: | 15行: | ||
</pre> | </pre> | ||
| − | ofApp.cpp | + | ;ofApp.cpp |
<pre> | <pre> | ||
void ofApp::setup(){ | void ofApp::setup(){ | ||
2014年11月18日 (火) 05:19時点における版
入手
https://github.com/vanderlin/ofxBox2d
- zipボタンをクリックし、ファイル一式をダンロード
- 作成された「ofxBox2D-master」フォルダーを「ofxBox2D」に名称変更し、addonsフォルダーに移動する。
- projectGeneratorでBox2Dアドオンを選択する。
- ofApp.h
ofxBox2d box2d; // Box2Dの世界 ofxBox2dCircle circle; // 円 vector <ofPtr <ofxBox2dCircle> > circles; // 円の配列 vector <ofPtr <ofxBox2dRect> > boxes; // 四角の配列
- ofApp.cpp
void ofApp::setup(){
ofSetFrameRate(60);
ofBackground(0);
// Box2Dの設定
box2d.init(); // 初期化
box2d.setGravity(0, 0); // 重力 (方向、強さ)
box2d.createBounds(); // 画面の周囲に壁を作成
box2d.setFPS(30.0); // box2DののFPS
box2d.registerGrabbing(); // 物体をつかめるようにする
circle.setPhysics(3.0, 0.53, 0.1); // 円の物理パラメータを設定
circle.setPhysics(2.0, 0.9, 1.5); // 円の物理パラメータを設定 (質量、反発係数、摩擦係数)
circle.setup(box2d.getWorld(), ofGetWidth() / 2.0, 100, 40); // 円を物理世界に追
}
//--------------------------------------------------------------
void ofApp::update(){
box2d.update(); // box2Dの更新
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(0, 127, 255);
circle.draw(); // 円の描画
for(int i=0; i<circles.size(); i++){
ofFill();
ofSetColor(0, 127, 255);
circles[i].get()->draw(); // 円の描画
}
for(int i=0; i<boxes.size(); i++){
ofFill();
ofSetColor(255, 127, 0);
boxes[i].get()->draw(); // 円の描画
}
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
float r = ofRandom(10,80);
float w = ofRandom(10,40);
float h = ofRandom(10,40);
if( r < 40 ){
circles.push_back(ofPtr<ofxBox2dCircle>(new ofxBox2dCircle));
circles.back().get()->setPhysics(3.0, 0.99, 0.1);
circles.back().get()->setup(box2d.getWorld(), mouseX, mouseY, r);
}else{
boxes.push_back(ofPtr<ofxBox2dRect>(new ofxBox2dRect));
boxes.back().get()->setPhysics(3.0, 0.99, 0.1);
boxes.back().get()->setup(box2d.getWorld(), mouseX, mouseY, w, h);
}
}