问题描述
在我的应用中,我使用加速度计来控制游戏中的角色。现在,我只允许纵向显示,因此用户必须向右或向左倾斜设备才能移动角色。到目前为止,一切正常。我现在要完成的工作是校准加速度计,以说明用户正在播放的当前倾斜度。假设用户站在他们的一边,由于不考虑起始点,因此值会偏斜,因此他们将无法控制角色。因此,我尝试考虑该初始倾斜量,然后在游戏过程中考虑该倾斜量,但是它不起作用。
In my app I use the accelerometer to control the character in my game. Right now I only allow the portrait orientation so the user has to tilt the device to the right or left in order to move the character. That is working fine so far. What I am trying to accomplish now is to 'calibrate' the accelerometer to account for the current tilt the user is playing on. Lets say the user is laying on their side, the values will be skewed since it didn't account for that starting point so they won't be able to control the character. So I am trying to account for that initial tilt amount and then account for it during the game however it is not working.
所以我在非游戏中有一个按钮视图以校准加速度计,这是它执行的代码:
So I have a button in my non-game view to 'calibrate' the accelerometer and this is the code that it executes:
这在IBAction中
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0f/30.0f];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
这是加速度计委托调用
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
[[NSUserDefaults standardUserDefaults] setFloat:acceleration.x forKey:@"X-Calibrate"];
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}
然后在初始化的游戏视图中,将iVar CalibrationFloat设置为该值我在NSUserDefaults中设置的。然后,我还有一个kFilteringFactor来消除小抖动。因此,这是游戏加速度计委托方法,该方法将rollingX设置为等于所有值,然后根据rollingX移动角色:
Then in my game view in the init I set the iVar calibrationFloat to the value which I set in NSUserDefaults. Then I also have a kFilteringFactor to get rid of the little jitters. So this is the game accelerometer delegate method which sets rollingX equal to everything and then I move the character based upon rollingX:
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
rollingX = ((acceleration.x - calibrationFloat) * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
}
但是,这不起作用,校准似乎没有任何作用,我不知道为什么。有人设置为什么这行不通吗?
However this is not working, the calibration doesn't seem to do anything and I am not sure why. Does anyone set why this wouldn't be working?
推荐答案
我可以给你一些指导。 RollingX的计算不正确,它将给出超出-1-+1范围的值,您需要一种检测到它是否达到极限并进行适当调整的方法。同样,仅偏移一个轴也将不起作用。加速度计可在3D空间中工作。为了使计算正确进行,电话在z平面中必须始终为0。用户旋转电话到z平面越深,设备在x平面上输出的读数越小。加速度计感应重力,因此飞机相对于重力是固定的。
I can give you some pointers. The calculation for rollingX is incorrect, it will give out values outside the range -1 - +1, you need a way to detect it hitting the limit and adjust it appropriately. Also simply offsetting one axis will not work. The accelerometer works in 3D space. In order for your calculation to work correctly the phone would always have to be 0 in the z-plane. The further into the z-plane the user rotates the phone, the smaller the readings the device will output on the x-plane. Accelerometers sense gravity, so the planes are fixed relative to this.
为了进行有效的校准,您将需要对所有3轴应用连续矩阵变换,因为您需要加权偏移量(例如,随着z的增加,多关注y而不是x等)。
In order to carry out an effective calibration you will need to apply a continuous matrix transformation to all 3-axis, as you will need weighted offsets (eg as z increases, pay more attention to y rather than x etc).
这篇关于校准UIAccelerometer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!