Skip to content

Wii Pointing in OpenFrameworks

2010 November 10
by Tedb0t

Here’s how to get a basic pointing scenario working using a Wiimote, “sensor bar” (just two IR LEDs or candles or other bright IR source, 12″ apart), OSCulator and OpenFrameworks.  First, identify your Wiimote in OSCulator and check IR(x,y), then set the “Event type” of the two IR parameters to “OSC Routing.”  In the OSC Routing parameters window, choose the “localhost:9000” commonly-used host option.

Here’s the OF code:

testApp.h:

#include "ofMain.h"
#include "ofxOsc.h"
#define PORT 9000
...
int w, h;
ofxOscReceiver	receiver;
float		wiiX, wiiY;
...

testApp.cpp:

void testApp::setup(){
	ofSetCircleResolution(50);
	ofSetFrameRate(60);
	ofSetWindowShape(1024, 768);
	ofBackground(0, 0, 0);
	ofSetVerticalSync(true);
	ofEnableSmoothing();
	ofSetFullscreen(true);

	w = 0;
	h = 0;	// set in update()

	// listen on the given port
	cout << "listening for osc messages on port " << PORT << "\n";
	receiver.setup( PORT );
}

//--------------------------------------------------------------
void testApp::update(){
	// check for waiting messages
	while( receiver.hasWaitingMessages() )
	{
		if(w == 0 || h == 0){
			w = ofGetWidth();
			h = ofGetHeight();
		}
		// get the next message
		ofxOscMessage m;
		float x,y;
		receiver.getNextMessage( &m );

		if ( m.getAddress() == "/wii/2/ir/0" )
		{
			x = m.getArgAsFloat( 0 );
			wiiX = x * w;
			cout << "x: " << wiiX << " y: " << wiiY << "\n";
		}
		else if ( m.getAddress() == "/wii/2/ir/1" )
		{
			y = 1 - m.getArgAsFloat( 0 );
			wiiY = y * h;
			cout << "x: " << wiiX << " y: " << wiiY << "\n";
		}
		else
		{
			cout << "unrecognized message: " << m.getAddress() << "\n";
		}
	}
}

//--------------------------------------------------------------
void testApp::draw(){
	ofSetColor(255, 130, 0);
	float radius = 50;
	ofFill();
	ofCircle(wiiX, wiiY, radius);
}

Related Posts:


Comments are closed.