我正在制作一个iphone应用程序,其中一个球将围绕屏幕滚动,这取决于用户如何倾斜设备。理论上,如果设备平躺在桌子上,球不会移动。如果设备倾斜完全向上,我希望球以最大速度直线向下滚动。速度取决于设备倾斜的平面位置距离多远。此外,它也适用于用户向右或向左或向上倾斜或四者的组合。我现在正在使用加速度计,并且球移动并且工作正常,我对物理学不太了解。如果有人对如何让这个工作顺利进行,请让我知道。使用加速度计倾斜iPhone的滚球
谢谢!
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float xx = -[acceleration x];
float yy = [acceleration y];
float z = -[acceleration z];
z = 1 - z;
NSString * zaxis = [NSString stringWithFormat:@"%f", z];
lblz.text = zaxis;
lbly.text = [NSString stringWithFormat:@"%f", yy];
lblx.text = [NSString stringWithFormat:@"%f", xx];
CGFloat newx;
CGFloat newy;
if (yy > 0)
{
newy = ball.center.y - ((1 - yy) * z);
}
else
{
newy = ball.center.y + ((1 - yy) * z);
}
if (xx > 0)
{
newx = ball.center.x - ((1 - xx) * z);
}
else
{
newx = ball.center.x + ((1 - xx) * z);
}
CGPoint newPoint = CGPointMake(newx, newy);
ball.center = newPoint;