How to program smooth movement with the accelerometer like a labyrinth game on iPhone OS?

别等时光非礼了梦想. 提交于 2019-11-30 22:42:54
Paul R

Is there some reason why you can not use the smoothing filter provided in response to your previous question: How do you use a moving average to filter out accelerometer values in iPhone OS ?

You need to calculate the running average of the values. To do this you need to store the last n values in an array, and then push and pop values off the array when ever you read the accelerometer data. Here is some pseudocode:

const SIZE = 10;
float[] xVals = new float[SIZE];

float xAvg = 0;

function runAverage(float newX){
  xAvg += newX/SIZE;
  xVals.push(newX);
  if(xVals.length > SIZE){
    xAvg -= xVals.pop()/SIZE;
  }
}

You need to do this for all three axis. Play around with the value of SIZE; the larger it is, the smoother the value, but the slower things will seem to respond. It really depends on how often you read the accelerometer value. If it is read 10 times per second, then SIZE = 10 might be too large.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!