<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Takepepe.com &#187; openFrameworks</title>
	<atom:link href="https://takepepe.com/category/openframeworks/feed/" rel="self" type="application/rss+xml" />
	<link>https://takepepe.com</link>
	<description>Designer::develop</description>
	<lastBuildDate>Sun, 16 Jun 2013 15:40:24 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>openFrameworks x Arduino #001</title>
		<link>https://takepepe.com/openframeworks-x-arduino-001-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=openframeworks-x-arduino-001-2</link>
		<comments>https://takepepe.com/openframeworks-x-arduino-001-2/#comments</comments>
		<pubDate>Fri, 15 Mar 2013 15:28:06 +0000</pubDate>
		<dc:creator>Takepepe</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[openFrameworks]]></category>
		<category><![CDATA[ofArduino]]></category>
		<category><![CDATA[ofNoise]]></category>
		<category><![CDATA[ofxSimpleGuiToo]]></category>
		<category><![CDATA[PerlinNoise]]></category>

		<guid isPermaLink="false">http://takepepe.com/?p=332</guid>
		<description><![CDATA[openFrameworks x Arduino #001 from Takepepe on Vimeo. そ [...]]]></description>
				<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/61887524" width="720" height="405" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<p><a href="http://vimeo.com/61887524">openFrameworks x Arduino #001</a> from <a href="http://vimeo.com/user16458913">Takepepe</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>そろそろこのアバターの格好で記事投稿するのが辛くなり始める今日この頃。<br />
皆様いかがお過ごしでしょうか。</p>
<p>今回はopenFrameworksをArduinoでコントロールするネタです。（Arduinoはほぼオマケですが…）<br />
動画ではひらひら揺らめく面と、再生している音楽が同期しています。<br />
赤外線センサーに手を近づけて、面の揺らめきと音楽のスピード・音量をコントロールします。</p>
<p>ArduinoはINとOUTを備えたマイコンボードの統合開発環境です。<br />
一般的なPCやガジェットには無いセンサーをコンテンツに取り込むだけでなく、<br />
LED、モーターや家電のコントロールも出来る素敵インターフェースです。</p>
<p><img src="/wp-content/uploads/2013/03/arduino.png" alt="Arduino" /></p>
<h6>Arduino　<a href="http://www.arduino.cc/" target="_blank">http://www.arduino.cc/</a></h6>
<p>Arduinoはボードにプログラムを書き込み、電源を供給すればスタンドアロンでも動かす事ができます。<br />
Processing・openFrameworksでも簡単に相互通信できるAPIが用意されています。<br />
とても面白いプラットフォームなのでいろんな記事を調べてみてください。</p>
<p>いつもの様に制作過程の流れで説明していきます。</p>
<ol>
<li>Arduinoボードの設定</li>
<li>openFrameworksとArduinoをつなぐ</li>
<li>ofxSimpleGuiTooを追加</li>
<li>3D空間にofCircleを描く</li>
<li>PerlinNoiseでゆらゆら</li>
<li>音楽を再生</li>
<li>Arduinoの値を摘要</li>
</ol>
<h2>1.Arduinoボードの設定</h2>
<p>今回のインスタレーションでは赤外線測距モジュール(GP2Y0A21)を使っています。<br />
参考というか、出版されている書籍に載っているものをそのままですw<br />
「5章 レシピ2：距離を測りたい」</p>
<h6>Prototyping Lab ―「作りながら考える」ためのArduino実践レシピ<br />
<a href="http://www.oreilly.co.jp/books/9784873114538/" target="_blank">http://www.oreilly.co.jp/books/9784873114538/</a></h6>
<p>まずは下記回路図の様にArduinoを組み立てます。</p>
<p><img src="/wp-content/uploads/2013/03/OA001_fzz.png" alt="Arduino" /></p>
<p>赤外線センサーから値が取れていることを確認します。<br />
以下のスケッチでは距離がcm単位でSerialに出力されます。</p>
<h3>OA001.ino</h3>
<pre class="brush: java; title: ; notranslate">


// アナログピン0番
const int sensorPin = 0;

// 閾値
const int threshold = 80;

void setup(){

	// 接続速度の設定
	Serial.begin(9600);

}

void loop(){

	int value = analogRead(sensorPin);
	if(value &gt; threshold){
		// cm単位に変換
		float range = (6787 / (value -3)) -4;
		Serial.println(range+&quot;cm&quot;);
	}

}
</pre>
<h2>2.openFrameworksとArduinoをつなぐ</h2>
<p>openFrameworksで値を取得する行程に入りますが、Arduinoでは先程のスケッチは使用しません。<br />
ArduinoボードにはStandardFirmataを書き込んでおきます。<br />
StandardFirmataは、色んなプラットフォームとArduinoを繋ぐものです。<br />
StandardFirmataのインストール方法とArduinoへの書き込み方法については割愛します。</p>
<p>openFrameworksのコードに移ります。
</p>
<h3>OA001.h</h3>
<pre class="brush: cpp; title: ; notranslate">
class OA001 : public ofBaseApp{

	public:
		void setup();
		void draw();

	private :
		ofArduino ard;
		float ardValue;
		float distance;
		void setupArduino(const int &amp; version);
		void analogPinChanged(const int &amp; pinNum);

}
</pre>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
// Arduinoとの接続速度
#define SPEED 57600

//--------------------------------------------------------------
void OA001::setup(){

	// Arduinoに接続
	ard.connect(&quot;/dev/cu.usbmodemfd321&quot;, SPEED);
	ofAddListener(ard.EInitialized, this, &amp;OA001::setupArduino);

}

//--------------------------------------------------------------
void OA001::setupArduino(const int &amp; version) {
    
	// イベントリスナ削除
	ofRemoveListener(ard.EInitialized, this, &amp;OA001::setupArduino);
	
	// アナログピン0番からのレポートを取得
	ard.sendAnalogPinReporting(0, ARD_ANALOG);
	ofAddListener(ard.EAnalogPinChanged, this, &amp;OA001::analogPinChanged);
	
	//cout &lt;&lt; ard.getFirmwareName() &lt;&lt; endl;
	//cout &lt;&lt; &quot;firmata v&quot; &lt;&lt; ard.getMajorFirmwareVersion() &lt;&lt; &quot;.&quot; &lt;&lt; ard.getMinorFirmwareVersion() &lt;&lt; endl;

}

//--------------------------------------------------------------
void OA001::analogPinChanged(const int &amp; pinNum) {

	int value = ard.getAnalog(pinNum);
	cout &lt;&lt; value &lt;&lt; endl;

}

//--------------------------------------------------------------
void OA001::update(){

	// Arduinoを更新
	ard.update();

}
</pre>
<p>コンソールにセンサー値が出力されたら接続成功です。<br />
このセンサー値は近くに何も無い場合、150以下で推移、最も手を近づけた時600強の値が出ます。<br />
純粋なセンサー値とは別に、この回で使用しているローパスフィルターをかけながら<br />
distanceに変換後の値を入れていきます。</p>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::analogPinChanged(const int &amp; pinNum) {

	// そのままのセンサー値を格納
	ardValue = ard.getAnalog(pinNum);

	// センサー値を調整して格納
	if(ardValue &lt; 100){
		distance = 100;
	}else if(ardValue &gt; 700){
		distance = 700;
	}else{
		// ローパスフィルターを摘要
		distance = distance*0.9+ardValue*0.1;
	}

}
</pre>
<p>ここまででArduinoについて分からない場合は田所先生の講義資料を見て勉強してみてください。<br />
本当に素晴らしい講義資料ばかりを公開していただいています。感謝！</p>
<h6>yoppa.org 「第９回：openFrameworksとArduinoを連携する」<br />
<a href="http://yoppa.org/ma2_11/3383.html">http://yoppa.org/ma2_11/3383.html</a></h6>
<h2>3.ofxSimpleGuiTooを追加</h2>
<p>開発を進めるにあたり、アドオンのofxSimpleGuiTooを使います。<br />
とても便利で、作品を作るにあたり開発効率が良くなるので早期に取り入れます。<br />
先程取得したセンサー値も、簡単に視覚化することが出来ます。<br />
筆者はmemoさんのバージョンを使っています。</p>
<h6>ofxSimpleGuiToo<br />
<a href="https://github.com/memo/ofxSimpleGuiToo">https://github.com/memo/ofxSimpleGuiToo</a></h6>
<h3>OA001.h</h3>
<pre class="brush: cpp; title: ; notranslate">
class OA001 : public ofBaseApp{

	public:
		void setup();
		void draw();

	private :
		ofxSimpleGuiToo gui;
		void setupGUI();

}
</pre>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">

void OA001::setup(){

	// ofxSimpleGuiTooをセットアップ
	setupGUI();

}

//--------------------------------------------------------------
void OA001::setupGUI() {

	gui.setup();
    
	// GUIの見た目を設定
	gui.config-&gt;textColor = 0xFFFFFF;
	gui.config-&gt;buttonHeight = 20;
	gui.config-&gt;sliderHeight = 10;
	gui.config-&gt;titleHeight = 20;
	gui.config-&gt;fullActiveColor = 0x00aec3;
    
	// Arduinoのセンサー値
	gui.addTitle(&quot;ARDUINO&quot;);
	gui.addSlider(&quot;ardValue&quot;, ardValue, 0.0f, 700.0f);
	gui.addSlider(&quot;distance&quot;, distance, 0.0f, 700.0f);
	
	// 前回終了した時の設定で起動
	gui.loadFromXML();
    
	// 最初から表示させる
	gui.show();
    
}

//--------------------------------------------------------------
void OA001::draw(){

	// ofxSimpleGuiTooを描画
	gui.draw();

}

</pre>
<h2>4.3D空間にofCircleを描く</h2>
<p>まずは縦横100個のofCircleを画面のセンターに表示させます。<br />
ofRotateYとかに適当に値を入れると、3D空間にofCircleが描かれているのが確認出来ます。</p>
<h3>OA001.h</h3>
<pre class="brush: cpp; title: ; notranslate">
class OA001 : public ofBaseApp{

	public:
		void setup();
		void draw();

	private :
		float rotateX;
		float rotateY;
		float rotateZ;

		int count;
		float margin;
		float radius;
		int color;
		int alpha;

}
</pre>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::setup(){

	// 画面全体の回転値を初期化
	rotateX = 0.0f;
	rotateY = 0.0f;
	rotateZ = 0.0f;

	// ofCircleで使用する値を初期化
	count = 100;
	margin = 25.0f;
	radius = 5.0f;
	color = 255;
	alpha = 255;

}

//--------------------------------------------------------------
void OA001::draw(){

	// この段階のMatrixを保持
	ofPushMatrix();

	// 画面センターに移動
	ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
    
	// 画面全体の回転値を摘要
	ofRotateX(rotateX);
	ofRotateY(rotateY);
	ofRotateZ(rotateZ);
    
	// ofCircleの描画
	int totalLen = (count-1)*margin;
	ofTranslate(totalLen/-2, totalLen/-2);
	ofSetColor(color, color, color, alpha);
	for(int i=0; i&lt;count; i++){
		for(int j=0; j&lt;count; j++){
			ofCircle(i*margin, j*margin, 0, radius);
		}
	}
    
	// 保持したMatrixに戻す
	ofPopMatrix();

}
</pre>
<h2>5.PerlinNoiseでゆらゆら</h2>
<p>ofNoiseはパターン化されたランダム値を取得する事が出来ます。<br />
ofNoiseからPerlinNoiseを取得するために以下記事を参考にしました。</p>
<h6>Noise &#8211; 【oF】openFrameworks<br />
<a href="https://sites.google.com/site/ofauckland/examples/noise">https://sites.google.com/site/ofauckland/examples/noise</a></h6>
<p>このPerlinNoiseで得たグレースケール値を、ofCircleの奥行きに摘要していきます。<br />
先程と同じ箇所を改変します。</p>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::draw(){

	// この段階のMatrixを保持
	ofPushMatrix();
    
	// 画面センターに移動
	ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
    
	// 画面全体の回転値を摘要
	ofRotateX(rotateX);
	ofRotateY(rotateY);
	ofRotateZ(rotateZ);
    
	// ofCircleの描画
	int totalLen = (count-1)*margin;
	ofTranslate(totalLen/-2, totalLen/-2);
	ofSetColor(color.r*255, color.g*255, color.b*255, alpha);
	for(int i=0; i&lt;count; i++){
		for(int j=0; j&lt;count; j++){
			float a = i * xForce;
			float b = j * yForce;
			float c = ofGetFrameNum() / 60.0;
			float noise = ofNoise(a,b,c) * zForce;
			ofCircle(i*margin, j*margin, noise, radius);
		}
	}
    
	// 保持したMatrixに戻す
	ofPopMatrix();

}
</pre>
<p>ここまでの様子は以下の様になります。<br />
ofxSimpleGuiTooで各パラメーターを操作して遊んでみます。<br />
画面収録している分フレームレートが落ちていますが、実際は60fpsで動きます。</p>
<p><iframe src="http://player.vimeo.com/video/61872549" width="720" height="405" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<p><a href="http://vimeo.com/61872549">openFrameworks x Arduino #001( Process )</a> from <a href="http://vimeo.com/user16458913">Takepepe</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h2>6.音楽を再生</h2>
<p>ofSoundPlayerで音楽を取り込みます。<br />
openFrameworksで音楽をコントロールする、恐らく最も簡単な方法です。<br />
ofSoundPlayerのボリュームをPerlinNoiseに係数として与えることで、<br />
音に併せて揺らめきが変わることが確認出来ます。</p>
<h3>OA001.h</h3>
<pre class="brush: cpp; title: ; notranslate">
class OA001 : public ofBaseApp{

	public:
		void setup();

	private :
		ofSoundPlayer mySound;
		float speed;
		float volume;
		void setupMusic();

}
</pre>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::setupMusic() {
    
	// 音楽を読み込んでループ再生
	mySound.loadSound(&quot;sound.wav&quot;);
	mySound.setLoop(true);
	mySound.play();
    
}

//--------------------------------------------------------------
void OA001::update(){

	// ofSoundPlayerを設定
	mySound.setSpeed(speed);
	mySound.setVolume(volume);

}

</pre>
<h2>7.Arduinoの値を摘要</h2>
<p>冒頭で取得したArduinoの値をofMapで音楽の音量・スピードに摘要すれば完成です！</p>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::update(){
    
	// Arduinoのセンサー値をofSoundPlayerの設定値に変換
	speed = ofMap(distance, 150, 600, 0.1f, 3.0f);
	volume = ofMap(distance, 150, 600, 0.1f, 1.0f);
    
	// ofSoundPlayerを設定
	mySound.setSpeed(speed);
	mySound.setVolume(volume);
    

}
</pre>
]]></content:encoded>
			<wfw:commentRss>https://takepepe.com/openframeworks-x-arduino-001-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>openFrameworks x iOS #001</title>
		<link>https://takepepe.com/openframeworks-x-ios-001/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=openframeworks-x-ios-001</link>
		<comments>https://takepepe.com/openframeworks-x-ios-001/#comments</comments>
		<pubDate>Sun, 10 Mar 2013 07:00:00 +0000</pubDate>
		<dc:creator>Takepepe</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[openFrameworks]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[OSC]]></category>

		<guid isPermaLink="false">http://takepepe.com/?p=292</guid>
		<description><![CDATA[openFrameworks x iOS #001 from Takepepe on Vimeo. 最近めっき [...]]]></description>
				<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/61449234" width="720" height="405" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<p><a href="http://vimeo.com/61449234">openFrameworks x iOS #001</a> from <a href="http://vimeo.com/user16458913">Takepepe</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>最近めっきり暖かくなりましたね。<br />
花粉が飛び始めたようですが、Takepepeまだ花粉症ではないのでどこ吹く風です。<br />
（花粉症の方すみません;）</p>
<p>今回は上の動画の様なものを作りました。<br />
openFrameworksで作成した複数のiOSアプリから、<br />
openFrameworksで作成したDesktopアプリケーションを操作します。<br />
通信には前回同様、OSCを使っています。</p>
<p>前回は１対１の一方通信でしたが、今回は１対複の通信になります。<br />
動画上では2台のiPhoneのタッチ座標を取得し、その座標をつなぐ線が描画されていますが、<br />
コード上はOSC接続している端末数に応じて点と線が増えていくものになっています。<br />
iPadやiOSシュミレーターでも確認できます。<br />
（引き出しに眠っているiPhone3GSが復活。捨てなくてよかった。）<br />
物理演算処理にはBox2Dを使用しています。</p>
<p>全体の流れを詳しく説明していきます。</p>
<ol>
<li>iOS端末からDesktopアプリケーションにOSC通信</li>
<li>iOS端末の数に応じたClientインスタンスの生成</li>
<li>Box2Dのセッティングと円を降らす処理</li>
<li>Clientメンバの座標間で線をひく</li>
</ol>
<h2>1.iOS端末からDesktopアプリケーションにOSC通信</h2>
<p>
まずはiOSアプリをopenFrameworksで作ります。<br />
アプリ単体で見ると、タッチした場所に円が描かれるだけの簡単なものになっています。
</p>
<h3>oscS.h (iOS端末のコード)</h3>
<pre class="brush: cpp; title: ; notranslate">

#define HOST &quot;192.168.10.191&quot;　// iMacのIPアドレス
#define PORT 8000 // 接続ポート

class oscS : public ofxiPhoneApp{
	
    public:
        void setup();
        void update();
        void draw();
        float getX(float mx);
        float getY(float my);
        void touchMoved(ofTouchEventArgs &amp; touch);
    
    private:
        ofxOscSender sender;
};
</pre>
<h3>oscS.mm (iOS端末のコード)</h3>
<pre class="brush: cpp; title: ; notranslate">

#include &quot;oscS.h&quot;
#include &quot;ofxOsc.h&quot;

//--------------------------------------------------------------
void oscS::setup(){
    
	ofxAccelerometer.setup();
    sender.setup( HOST, PORT );
	ofBackground(0, 0, 0);
    
}

//--------------------------------------------------------------
void oscS::update(){

}

//--------------------------------------------------------------
void oscS::draw(){
	ofSetColor(255, 255, 255);
    ofCircle(mouseX, mouseY, 10);
}

//--------------------------------------------------------------
float oscS::getX(float mx){
    int res = ofGetWidth();
    return mx/res;
}

//--------------------------------------------------------------
float oscS::getY(float my){
    int res = ofGetHeight();
    return my/res;
}

//--------------------------------------------------------------
void oscS::touchMoved(ofTouchEventArgs &amp; touch){
    ofxOscMessage m;
    m.setAddress( &quot;/mouse/position&quot; );
    m.addFloatArg( getX(touch.x) );
    m.addFloatArg( getY(touch.y) );
    sender.sendMessage( m );
}

</pre>
<h2>2.iOS端末の数に応じたClientインスタンスの生成</h2>
<p>
ここからはDesktopアプリケーションです。<br />
まずは「Client」クラスを作成します。<br />
このクラスはコンストラクタにiOS端末のipアドレスを引数で渡します。<br />
update関数が呼ばれるたび、タッチ座標を変換・更新します。
</p>
<h3>Client.h</h3>
<pre class="brush: cpp; title: ; notranslate">
#include &quot;ofxOsc.h&quot;
class Client {
    public:
        Client(std::string ip);
        void update(ofxOscMessage message);
        void draw();
        ofPoint pos;
        string getIp();
    
    private:
        string ip;
};
</pre>
<h3>Client.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
#include &quot;ofxOsc.h&quot;

//--------------------------------------------------------------
Client::Client(string _ip){
    ip = _ip;
}

//--------------------------------------------------------------
void Client::update(ofxOscMessage message){
    ofxOscMessage m = message;
    
    // Clientオブジェクトの更新
    if ( m.getAddress() == &quot;/mouse/position&quot; &amp;&amp; ip == m.getRemoteIp()){
        
        // 画面比率にあわせてタッチ座標を変換
        pos.x = ofGetWidth() * m.getArgAsFloat(0);
        pos.y = ofGetHeight() * m.getArgAsFloat(1);
    }
}

//--------------------------------------------------------------
void Client::draw(){
    ofCircle(pos.x, pos.y, 5);
}

//--------------------------------------------------------------
string Client::getIp(){
    return ip;
}
</pre>
<p>
メインスレッドでOSCメッセージを受信した際、そのメッセージが新しい端末からの場合、<br />
Clientインスタンスが生成されるようになっています。
</p>
<h3>oscR.h (抜粋)</h3>
<pre class="brush: cpp; title: ; notranslate">
#include &quot;Client.h&quot;
#define PORT 8000
class oscR : public ofBaseApp{
    public:
        void setup();
        void update();
        void draw();
    
    private:
        void updateClient();
        ofxOscReceiver  receiver;
        vector &lt;Client *&gt; clients;

}
</pre>
<h3>oscR.cpp (抜粋)</h3>
<pre class="brush: cpp; title: ; notranslate">
#include &quot;oscR.h&quot;
#include &quot;ofxOsc.h&quot;

//--------------------------------------------------------------
void oscR::setup(){

    receiver.setup( PORT );
    
}
//--------------------------------------------------------------
void oscR::update(){
    
    // OSCクライアントの更新
    updateClient();

}
//--------------------------------------------------------------
void oscR::updateClient(){
    while( receiver.hasWaitingMessages() ){
        ofxOscMessage m;
        receiver.getNextMessage( &amp;m );
        
        // 1つ目のOSCクライアント
        if(clients.size() == 0){
            clients.push_back(new Client(m.getRemoteIp()));
        }
        
        // 新しいOSCクライアントからのメッセージかどうか
        int flag = 1;
        for(int i=0 ; i&lt; clients.size() ; i++){
            if(clients[i]-&gt;getIp() == m.getRemoteIp()){
                flag = 0;
            }
        }
        
        // 新しいOSCクライアントだったら
        if(flag){
            clients.push_back(new Client(m.getRemoteIp()));
        }
        
        // Clientオブジェクトの更新
        int size = clients.size();
        for(int i=0 ; i&lt; size ; i++){
            clients[i]-&gt;update(m);
        }
    }
}
//--------------------------------------------------------------
void oscR::draw(){

    ofSetColor(50,200,255);
    ofFill();
    for(int i=0; i&lt;clients.size(); i++) {
        clients[i]-&gt;draw();
    }

}
</pre>
<h2>3.Box2Dのセッティングと円を降らす処理</h2>
<p>
次にBox2Dの実装です。<br />
updateでカウントアップし、circlesの数が100に達したら<br />
新しいofxBox2dCircleを作り、古いものは削除します。
</p>
<h3>oscR.h (抜粋)</h3>
<pre class="brush: cpp; title: ; notranslate">
#include &quot;ofxBox2d.h&quot;
class oscR : public ofBaseApp{
    public:
        void setup();
        void update();
        void draw();
    
    private:
        void updateCircles();
        int count;
    
        ofxBox2d box2d;
        vector &lt;ofxBox2dCircle *&gt; circles;
    
};

</pre>
<h3>oscR.cpp (抜粋)</h3>
<pre class="brush: cpp; title: ; notranslate">
#include &quot;ofxBox2d.h&quot;

//--------------------------------------------------------------
void oscR::setup(){

    count = 0;
    
    // Box2D設定
    box2d.init();
    box2d.setGravity(0, 0.1);
    box2d.createBounds(0, 0, ofGetWidth(), ofGetHeight());
    box2d.setFPS(10.0);
    box2d.setIterations(5, 5);
    
}

//--------------------------------------------------------------
void oscR::update(){
    
    // ofxBox2dCircleを更新
    updateCircles();
    box2d.update();

}

//--------------------------------------------------------------
void oscR::updateCircles(){
    count++;
    if(count &gt; 100){
        
        // ofxBox2dCircleの追加
        ofPoint pos;
        int r = ofRandom(10, 30);
        int x = ofRandom(r, ofGetWidth());
        pos.set(x,r);
        
        ofxBox2dCircle *c = new ofxBox2dCircle();
        c-&gt;setPhysics(1.0, 0.2, 0.2);
        c-&gt;setup(box2d.getWorld(),x,r,r);
        circles.push_back(c);
        
        // 100個以上になったら先頭のofxBox2dCircleを削除
        if(circles.size() &gt; 100){
            vector &lt;ofxBox2dCircle *&gt;::iterator it = circles.begin();
            (*it)-&gt;destroy();
            delete *it;
            it = circles.erase(it);
        }
        
        count = 0;
    }
}

//--------------------------------------------------------------
void oscR::draw(){

    ofNoFill();
    ofSetColor(255,255,255);
    for(int i=0; i&lt;circles.size(); i++) {
        circles[i]-&gt;draw();
    }

}
</pre>
<h2>4.Clientメンバの座標間で線をひく</h2>
<p>
最後にiOS端末から取得した座標でofxBox2dPolygonの線をひきます。<br />
ofPolylineに座標を追加し、ofxBox2dPolygonに代入します。<br />
画面上で線が見える様、draw関数ではofPolylineを描画しています。<span id="more-292"></span>
</p>
<h3>oscR.h (抜粋)</h3>
<pre class="brush: cpp; title: ; notranslate">
#include &quot;ofxBox2d.h&quot;

class oscR : public ofBaseApp{
    public:
        void setup();
        void update();
        void draw();
    
    private:
        void updatePolyLine();
        ofPolyline drawing;
        ofxBox2dPolygon polyLine;
    
};

</pre>
<h3>oscR.cpp (抜粋)</h3>
<pre class="brush: cpp; title: ; notranslate">
#include &quot;ofxBox2d.h&quot;

//--------------------------------------------------------------
void oscR::update(){
    
    // 複数のOSCクライアントでofxBox2dPolygonを更新
    updatePolyLine(); 
}

//--------------------------------------------------------------
void oscR::updatePolyLine(){
    int size = clients.size();
    
    // OSCクライアントが複数だったら
    if(size &gt;= 2){
        drawing.clear();
        for(int i=0 ; i&lt; size ; i++){
            // 座標を追加
            drawing.addVertex(clients[i]-&gt;pos.x, clients[i]-&gt;pos.y);
        }
        // 3点以上必要なので最後の座標を追加
        drawing.addVertex(clients[size-1]-&gt;pos.x, clients[size-1]-&gt;pos.y);
        
        // ofPolylineをofxBox2dPolygonに代入
        polyLine.clear();
        polyLine.setPhysics(0.0, 0.5, 0.5);
        polyLine.addVertexes(drawing);
        polyLine.create(box2d.getWorld());
        
    }
}

//--------------------------------------------------------------
void oscR::draw(){
 
    ofSetColor(50,100,255);
    if (drawing.size()){
        drawing.draw();
    }

}

</pre>
]]></content:encoded>
			<wfw:commentRss>https://takepepe.com/openframeworks-x-ios-001/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

 Served from: takepepe.com @ 2026-04-10 17:46:16 by W3 Total Cache -->