WiiFlash Tip #2 – Smoothing out the edges

This is tip #2 in my series on WiiFlash.

Once you have data coming in the from a Wii controller, the first thing you’ll notice is how precise it is. I don’t have it in front of me, but the precision goes about 15 places past the decimal (4.042584267432343…). If you just convert the data from radians to degrees, WiimoteObj.roll*(180 / Math.PI), and apply it to a sprite with an enter frame event, you’ll see it wobble all like mad.

There are two things that you must smooth out to best use the Wii controller – the roll, pitch and yaw of the controller, and the animation of the Wii mouse pointer.

1 – Smoothing Roll, Pitch and Yaw Data

I’m only going to demonstrate for the roll value – it’s the same for pitch and yaw. There are probably numerous ways to optimize this code, but it’s working me right now. My method involves comparing the current value to the last value and checking to see if it’s outside of a tolerance value. If so, then the value is updated, if not, then it’s ignored. I’ve found that 10 degrees works well for me, but you can change it depending on how precise you need it to be. This code assumes that you have an instance of the Wiimote class, WiimoteObj, and have the listeners set up to call the updateWiiMoteData function when the WiimoteEvent.UPDATE event occurs.

private var WiiMPitchDeg:int;
private var WiiMPitchDegPrev:int;
// to convert radians to degrees
private static const TO_DEG:Number = 180 / Math.PI;
// smoothes out roll, pitch and yaw values, smaller = more precision
private static const WIGGLE_TOL:int = 10;

private function updateWiiMoteData(pEvt:WiimoteEvent):void {
WiiMRollDeg = int(WiimoteObj.roll*TO_DEG);
var rDelta:Number = Math.abs(Math.abs(WiiMRollDegPrev) – Math.abs(WiiMRollDeg));
if(rDelta < WIGGLE_TOL) WiiMRollDeg = WiiMRollDegPrev;
}

As you can see it’s pretty simple and works well.

2 – Smoothing out the Wii mouse cursor

This one is very easy. All that you need to do is to slightly tween your cursor sprite from it’s current location to the location that the Wii more is pointing to. Here’s my function, same assumptions as above. wiiPoint is a Point object containing the location you calculated for where the controller is pointing to. I’m using Tweener for the animation.

 private function updateCursor():void {
var newx:int = 0;
var newy:int = 0;
var newr:int = 0;
try {
newx = wiiPoint.x;
newy = wiiPoint.y;
newr = WiimoteObj.roll;
} catch (e:*) {
// no good data – the ‘mote hasn’t seen the IR points
newx = 0;
newy = 0;
newr = 0;
}
Tweener.addTween(CursorSprite, {x:newx, y:newy, rotation:newr, time:.1, transition:”easeOutQuad” } );
}

For your VirtualMouse object, be sure to set it’s x and y properties to the location of the cursor sprite, not where your code says the Wii is pointing at.

WiiFlash Tip #1 – A virtual Wii-mouse

I’ve been doing a lot of really cool stuff with WiiFlash at the Bank – but I can’t really share any of the code. So, to help any aspiring WiiFlash developers out there, I’ll post a few tips over time with things that would have helped me out. If you don’t know anything about WiiFlash, take a look at their site, it’s a pretty cool hack. Now that I’ve got a good working class system, I just want to sit around and create mini-games.  I wish that I had the free time!

Tip #1 -Using the Wii Controller as a mouse in Flash

The toughest problem that I’ve come across so far: “how do you use the controller as a mouse (click on buttons) in a Flash movie without relying on WiiFlash’s crappy mousemode?”The “mousemode” feature takes over control of the system mouse pointer and uses the controller to point and click. I didn’t want this – I just wanted a nice Flash sprite moving around and still allow the user to click on things with their real mouse. But I wasn’t quite sure how to have this sprite fire off mouse events.

Solution: Senocular’s VirtualMouse class.

Once you’ve figured out how to read the IR points from a sensor bar (a future tip), and have a pointer moving around the screen, just set your virtual mouse instance’s X and Y values to the same as the pointer sprite. Trap the A button press event to fire off a virtual mouse CLICK event and that’s it! Well, I guess there are a few details to fill in still – but you get the general idea. Get it working properly and you can interact with your movie/game with the mouse and the Wii controller.