IOS use CMRotationRate to move an object on the screen

444 views Asked by At

I have a simple logic for my 2d game that is enabled on the iphone with the orientation locked to Landscape Left.

If the phone is rotate away from you, imagine holding it in landscape mode, I want my object to be moved up otherwise I want the object to be moved down.

-(void)outputRotationData:(CMRotationRate)rotation
{


    if (fabs(currentMaxRotY - rotation.y) < .3) {
        return;
    }
    if(rotation.y > 0 ){
        if (!(ship.position.y < 10)) {
            [ship runAction:actionMoveDown];
            NSLog(@"moving down");
        }
    }else{
        if (!(ship.position.y > screenHeight)) {
            [ship runAction:actionMoveUp];
            NSLog(@"moving up");

        }
    }

    currentMaxRotY = rotation.y;


}

The code above works. Just not as you would expect. Even when the phone is rotated away from you, sometimes the object moves down; however it correctly moves up most of the times. I assume due to the sensitivity which I thought would have been fixed by returning if our last rotation and current rotation don't differ by more than .3

1

There are 1 answers

0
Cripto On

As @LearnCocos2D mentioned in a comment, gyro is not what we want to use here.

The code below will place the object at a location based on a the angle.

I ended up not using this either and just use tap to move!

    -(void)outputAccelertionData:(CMAcceleration)acceleration
{

    double x, y, z, angle;
    x = roundThree(acceleration.x);
    y = roundThree(acceleration.y);
    z = roundThree(acceleration.z);
    angle = atan(x/z);
    angle = [self RadiansToDegrees:angle];
    if (z == 0) {
        angle = 90;
    }
    if (angle < 40) {

        [self runThisAction:angle];
    }else if (angle > 50){

                [self runThisAction:-angle/2];
    }

}