使用加速度计使 iPhone 倾斜时球滚动

发布于 2024-11-05 18:45:42 字数 952 浏览 0 评论 0原文

我正在制作一个 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;

I am making an iphone app where a ball will roll around the screen based on how the user tilts the device. If the device is lies flat on the table theoretically the ball would not move. If the device is tilted standing completely upward the I want the ball to roll straight down at maximum speed. The speed depends on how far from the flat position the device is tilted. Also, it also works for if the user tilts right or left or up or combinations of the four. I am using the accelerometer right now and the ball moves and it works okay, I am just not real familiar with physics. If someone has any suggestions on how to get this to work smoothly please let me know.

Thanks!

- (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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2024-11-12 18:45:42

如果您想让它看起来更真实并利用现有的东西,请查看一些现有的物理引擎和 2d 框架、Box2d 和 Cocos2d,但还有许多其他引擎。

If you want to make it look more realistic and leverage existing stuff, look at some of the existing physics engines and 2d frameworks, Box2d and Cocos2d, but there are many others.

痴梦一场 2024-11-12 18:45:42

我认为你在这里搞乱的关键是加速度和速度之间的差异。您希望“倾斜量”作为加速度。每帧球的速度应根据加速度而变化,然后球的位置应根据球的速度而变化。

所以在 X 中它应该是这样的:

float accelX = acceleration.x;

mVel.x += accelX;  \\mVel is a member variable you have to store

ball.center.x += mVel.x;

---更复杂的版本

现在我想得越多,它可能不是你想要的加速度的“倾斜量”。您可能希望倾斜量为“目标速度”。但你仍然想使用加速来到达那里。

mTargetVel.x = acceleration.x;

//Now apply an acceleration to the velocity to move towards the Target Velocity
if(mVel.x < mTargetVel.x) {
   mVel.x += ACCEL_X;  //ACCEL_X is just a constant value that works well for you
} 
else if(mVel.x > mTargetVel.x) {
   mVel.x -= ACCEL_X;  
} 

//Now update the position based on the new velocity
ball.center.x += mVel.x;

I think the key thing you are messing here is the difference between acceleration and velocity. You want the 'amount of tilt' to work as an acceleration. Each frame the balls Velocity should change by the acceleration, then the balls position should change by the balls velocity.

So just in X it should be something like:

float accelX = acceleration.x;

mVel.x += accelX;  \\mVel is a member variable you have to store

ball.center.x += mVel.x;

---More Complex version

Now the more I think about it, it might not be the 'amount of tilt' you want to be the acceleration. You might want the amount of tilt to be the 'Target Velocity.' But you still want to use an acceleration to get there.

mTargetVel.x = acceleration.x;

//Now apply an acceleration to the velocity to move towards the Target Velocity
if(mVel.x < mTargetVel.x) {
   mVel.x += ACCEL_X;  //ACCEL_X is just a constant value that works well for you
} 
else if(mVel.x > mTargetVel.x) {
   mVel.x -= ACCEL_X;  
} 

//Now update the position based on the new velocity
ball.center.x += mVel.x;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文