Smoothing the Wiimote values

advertisement
Intro to Games
July 29, 2010
Smoothing the Wiimote values
We want to smooth the Wiimote values, by using an average of the past 10 wiimote values instead of just the last one.
How we will do this is to use a Queue to store the 10 wiimote values. A queue works well because when we have 10
values we can take get rid of the first value in the line and then add the latest wiimote value to the end of the line.
Declare:
Queue<float> wiimoteList = new Queue<float>();
float previousWiimoteX = 0.0f;
Replace your code for calculating the cannon rotation with below:
float averagedX = averageWiimoteX(wiimoteComponent.X);
cannon.rotation = -MathHelper.PiOver2 + averagedX * MathHelper.PiOver2;
previousWiimoteX = wiimoteComponent.X;
And then you can use my method for averaging the wiimote values:
private float averageWiimoteX(float x)
{
if (wiimoteList.Count < 10)
{
wiimoteList.Enqueue(x);
return x;
}
else
{
wiimoteList.Dequeue();
wiimoteList.Enqueue(x);
}
float averageX = 0;
foreach (float element in wiimoteList)
{
averageX += element;
}
return (averageX / 10);
}
Download