简体   繁体   English

加速度计倾斜问题

[英]Accelerometer Tilt Issue

So this is my first practice game I'm developing and first time using accelerometer so apologizes in advance if the answer is obvious. 因此,这是我正在开发的第一款练习游戏,并且是首次使用加速度计,因此如果答案显而易见,请提前道歉。 Would also like answered with code and explanation. 还要提供代码和解释。

So part of game I have a ball being rolled around no the screen. 因此,在游戏的一部分中,我有一个球没有在屏幕上滚动。 The ball moves according to the tilt. 球根据倾斜度移动。 The device orientation for this app is only landscape right. 此应用程序的设备方向仅适用于横向。 When I tilt the phone up and down while holding the phone landscape right the ball moves accordingly. 当我握住手机横向并向上和向下倾斜手机时,球会相应移动。 (tilt the top end down the ball rolls up, tilt the bottom end down the ball rolls down). (将顶端向下倾斜,使球向上滚动,将底端向下倾斜,使球向下滚动)。 So those 2 tilts are fine. 因此,这2个倾斜都很好。 Now when I tilt the screen left side down the ball rolls right and vice versa. 现在,当我将屏幕向左下方倾斜时,球会向右滚动,反之亦然。 What I want is the screen to tilt left and the ball to go left and tilt the screen right and the ball goes right. 我想要的是屏幕向左倾斜,球向左倾斜,屏幕向右倾斜,球向右倾斜。 How is this fixed with my code below. 我的以下代码如何解决此问题。 I know its as simple as changing two lines probably. 我知道它很简单,只需更改两行即可。

//delegate method
-(void)outputAccelertionData:(CMAcceleration)acceleration
{
    currentMaxAccelX = 0;
    currentMaxAccelY = 0;


    if(fabs(acceleration.x) > fabs(currentMaxAccelX))
    {
        // this needs to be currentMaxAccelY not currentMaxAccelX for those of you thinking this is the solution
        currentMaxAccelY = acceleration.x; 
    }
    if(fabs(acceleration.y) > fabs(currentMaxAccelY))
    {
        // this needs to be currentMaxAccelX not currentMaxAccelY for those of you thinking this is the solution
        currentMaxAccelX = acceleration.y;
    }
}

-(void)update:(CFTimeInterval)currentTime {

    /* Called before each frame is rendered */


    float maxY = 480;
    float minY = 0;


    float maxX = 320;
    float minX = 0;

    float newY = 0;
    float newX = 0;

    //Im pretty sure the problem is in this if statement as this is what deals with the left and right tilt
    if(currentMaxAccelX > 0.05){
        newX = currentMaxAccelX * 10;
    }
    else if(currentMaxAccelX < -0.05){
        newX = currentMaxAccelX*10;
    }
    else{
        newX = currentMaxAccelX*10;
    }

    newY = currentMaxAccelY *10;

    newX = MIN(MAX(newX+self.ball.position.x,minY),maxY);
    newY = MIN(MAX(newY+self.ball.position.y,minX),maxX);


    self.ball.position = CGPointMake(newX, newY);

}

So I solved the problem. 所以我解决了这个问题。 The issue was with my math. 问题出在我的数学上。 Instead of multiplying by 10 I should have been multiplying by negative 10 to get in the inverse effect. 我应该乘以负10而不是乘以10以获得相反的效果。

if(currentMaxAccelX > 0.05){
        newX = currentMaxAccelX * -10;
    }
    else if(currentMaxAccelX < -0.05){
        newX = currentMaxAccelX*-10;
    }
    else{
        newX = currentMaxAccelX*-10;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM