物理エンジン Box2D
提供:kuhalaboWiki
(版間での差分)
(→入手) |
(→動的配列でマウスクリックで生成) |
||
| 81行: | 81行: | ||
circle.setPhysics(3.0, 0.53, 0.1); // 円の物理パラメータを設定 | circle.setPhysics(3.0, 0.53, 0.1); // 円の物理パラメータを設定 | ||
circle.setPhysics(2.0, 0.9, 1.5); // 円の物理パラメータを設定 (質量、反発係数、摩擦係数) | circle.setPhysics(2.0, 0.9, 1.5); // 円の物理パラメータを設定 (質量、反発係数、摩擦係数) | ||
| − | circle.setup(box2d.getWorld(), ofGetWidth() / 2.0, 100, 40); // | + | circle.setup(box2d.getWorld(), ofGetWidth() / 2.0, 100, 40); // 円を物理世界に追く |
} | } | ||
2014年11月18日 (火) 07:09時点における版
目次 |
入手
https://github.com/vanderlin/ofxBox2d
- zipボタンをクリックし、ファイル一式をダンロード
- 作成された「ofxBox2D-master」フォルダーを「ofxBox2D」に名称変更し、addonsフォルダーに移動する。
- projectGeneratorでBox2Dアドオンを選択する。
単体の円と四角
- ofApp.h
ofxBox2d box2d; // Box2Dの世界 ofxBox2dCircle circle; // 円 ofxBox2dRect box; // 四角 ofxBox2dRect wall; // 壁
- ofApp.cpp
void ofApp::setup(){
ofSetFrameRate(60);
ofBackground(0);
// Box2Dの世界の設定
box2d.init(); // 初期化
box2d.setGravity(0, 1); // 重力 (方向、強さ)
box2d.createBounds(); // 画面の周囲に壁を作成
box2d.setFPS(30.0); // box2Dの世界でのFPS
box2d.registerGrabbing(); // 物体をつかめるようにする
circle.setPhysics(2.0, 0.9, 1.5); // 円の物理パラメータを設定 (重さ、反発係数、摩擦係数)
circle.setup(box2d.getWorld(), ofGetWidth() / 2.0, 100, 40); // 円を物理世界に追く
box.setPhysics(2.0, 0.9, 1.5); // 四角の物理パラメータを設定 (重さ、反発係数、摩擦係数)
box.setup(box2d.getWorld(), ofGetWidth() / 3.0, 200, 100, 40); // 四角を物理世界に追く
wall.setup(box2d.getWorld(), ofGetWidth() / 2.0, 500, 300, 10); // 壁を物理世界に追く。物理パラメータはない
}
//--------------------------------------------------------------
void ofApp::update(){
box2d.update(); // box2Dの更新
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(0, 127, 255);
circle.draw(); // 円の描画
ofSetColor(255, 127, 0);
box.draw(); // 四角の描画
ofSetColor(127, 255, 0);
wall.draw(); // 壁の描画
}
setPhysics()で力学属性を設定するが、これを省略すると、場所が固定された障壁のような働きをする。
動的配列でマウスクリックで生成
- ofApp.h
- Box2Dでは、動的配列にvectorではなく、vector <ofPtr >を使う
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);
}
}