LoginSignup
2
0

More than 5 years have passed since last update.

openFrameworks(0.10.0)でofxBox2Dを動かす

Last updated at Posted at 2018-05-11

ofxBox2Dを動かしてみた。

環境
openFrameworks : 0.10.0
ofxBox2D : ofxBox2d/master (https://github.com/vanderlin/ofxBox2d)
VisualStudio : 2017
Windows 10

1. ofxBox2Dのインストール

ほかのサイトにもさんざん書かれているので省略しますが、GitHubからhttps://github.com/vanderlin/ofxBox2d 
のマスターをダウンロードします。(openFrameworksのversionによってダウンロードするブランチが違うようなので注意)
その後、インストール済みのopenFrameworksのaddonフォルダに格納して準備完了。

2. ビルド

新規プロジェクトを作成して、適当なコードを書いてビルドをしてみる。
今回は、マウスクリックしたところに白いボールを生成して、cキーを押すとクリアするコードを書いた。
書いたコードは以下。

ofApp.h
#pragma once

#include "ofMain.h"
#include "ofxBox2d.h"
#include <vector>

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 mouseEntered(int x, int y);
    void mouseExited(int x, int y);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);

    ofxBox2d box2d; // box2dのインスタンス変数
    std::vector<ofxBox2dCircle*> circles;

};
ofApp.cpp
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup() {

    ofBackground(0, 0, 0);

    box2d.init();

    box2d.setGravity(0, 9.8);

    box2d.createBounds();

    box2d.setFPS(30);
}

//--------------------------------------------------------------
void ofApp::update() {
    box2d.update();
}

//--------------------------------------------------------------
void ofApp::draw() {

    for (auto it = circles.begin(); it != circles.end(); ++it) {

        ofSetColor(ofColor(255, 255, 255));
        (*it)->draw();
    }
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key) {

    if (key == 'c')
    {
        for (auto it = circles.begin(); it != circles.end();) {
            (*it)->destroy();
            delete *it;
            it = circles.erase(it);
        }
    }
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {

    ofxBox2dCircle* circle = new ofxBox2dCircle();
    circle->setPhysics(10.0, 0.8, 0);
    circle->setup(box2d.getWorld(), x, y, 20);
    circles.push_back(circle);
}

すると、以下のような旨のエラーがでる。

  1. ofDefaultVertexTypeが定義されてない
  2. ofRotateDegが定義されてない

いろいろ調べたんですが、あまり参考になる記事がなく、結局コードを漁ってくと、
どうにも、宣言している型名が変わってる模様。
以下のようにofDefaultVertexType ,ofRotateDeg が書かれてる部分をすべて修正したところ無事動いた。

  1. ofDefaultVertexType を ofPoint に修正
  2. ofRotateDeg を ofRotate に修正

動作するとこんな感じ。
無題.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0