此代码用于加速度计 方法
它使用名为playerVelocity的CGPoint变量。
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
//controls how quickly the velocity decelerates
float deceleration = 0.4f;
//determines how sensitive the accelerometer reacts
float sensitivity = 6.0f;
//how fast the velocity can be at most
float maxVelocity = 100;
playerVelocity.x = playerVelocity.x *deceleration + acceleration.x *sensitivity;
if (playerVelocity.x < -maxVelocity)
{
playerVelocity.x = -maxVelocity;
}
else if (playerVelocity.x > maxVelocity)
{
playerVelocity.x = maxVelocity;
}
}
现在我知道playerVelocity
变量是CGPoint,所以我把它想象成X,Y图。
我假设playerVelocity
变量在哪里休息(假设为150,0),无论何时收到加速度计输入(这是由iPhone倾斜),它首先将任何坐标乘以0.4,然后再加上accelerometer.x
乘以playerVelocity
变量乘以6.0。这是对的吗?
稍后在另一种方法中,通过
将其添加到我的其他对象位置CGPoint pos = playerObject.position;
pos.x+= playerVelocity.x;
playerObject.position = pos;
我感到困惑的是幕后究竟发生了什么。我的假设是正确的吗?
当playerVelocity为150,0并乘以0.4时,playerVelocity变量的X坐标是否逐渐减小,即150,0,145,0,130,0等。?</ p>
如果我弄清楚这一点,我就会知道我的playerObject
是如何移动的。
答案 0 :(得分:1)
看起来你有一个恒定的减速度(.4),它是在你通过加速度计接收的加速度减去当前行驶的任何方向上的反向运动,乘以一个常数。然后将该值添加到当前速度。因此,您实际上是为每次计算添加了(加速度计的加速度 - 恒定减速度)与当前速度的差值。