WiiFlash Tip #2 – Smoothing out the edges, part 2

.crux. added a comment about a different way to smooth out the data from the Wii controller – create an array of the last 10 values and average them. I’ve modified my code to try out this idea, and it’s not too bad. The data does wobble a little, but it’s more precise than my method. If you increase the size of the array, it gets smoother, but will also increase the cpu power required. Partial code below.

 private var WiiRollDegAry:Array = new Array();

public function get roll():int {
return averageIntArray(WiiRollDegAry);
}

private function updateData(pEvt:WiimoteEvent):void {
WiiRollDegAry = addValueToLimitedArry(WiiRollDegAry, int(TheWiimote.roll * TO_DEG));
}

private function addValueToLimitedArry(a:Array, v:int):Array{
a.push(v);
// increase to smooth out data
if (a.length > 10) a.shift();
return a;
}

private function averageIntArray(a:Array):int {
var len:int = a.length;
var c:int = 0;
for (var i:int = 0; i < len; i++) {
c += int(a[i]);
}
return int(c / len);
}

7 Comments

  1. Hiho,
    i read your comment and think about a faster way for handle the “vibration” problem.

    Here the code for free implement:

    if(counter == 10)
    {
    this.sumRoll *= 0.9
    this.sumRoll = (this.wii.roll Math.PI * 0.5)/Math.PI * this.width
    this.irSpot.x = this.sumRoll * 0.1

    this.sumPitch *= 0.9
    this.sumPitch = (this.wii.pitch Math.PI * 0.5)/Math.PI * this.height
    this.irSpot.y = this.sumPitch * 0.1
    }else
    {
    counter = 1
    this.sumRoll = (this.wii.roll Math.PI * 0.5)/Math.PI * this.width
    this.sumPitch = (this.wii.pitch Math.PI * 0.5)/Math.PI * this.height

    }

    looks a little bit curious, but it works.

    kind regards
    .crux.

    Reply

  2. the blog system kills the increase line of the counter. Could you change the comment and write in the ‘else’ statement counter = 1

    thx

    Reply

  3. Arrgh, the comment section hide the ” ” (plus). I want increment the counter … “counter ” or “counter =1”

    Reply

  4. ok … test failed.
    “counter plus plus” or “counter plus =1” or “conter increment”.

    sorry 4 spam

    Reply

  5. @Jeff – I saw your demo yesterday – that’s really cool!

    I am using Tweener in my code to animate the mouse cursor and it works very well.

    One of my goals was also to have smooth data accessible via a variable – you can’t get that with a Tween since it may not represent the accurate position of the data.

    Reply

Leave a Reply to .crux. Cancel reply