0
votes

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
actions are not suitable for frequent changes and you may even run multiple move actions simultaneously, update rotation property directly. See 2nd half of this artcile from the cocos2d developer guide: makegameswith.us/docs/#!/cocos2d/1.3/concepts/cocos2d-actions - LearnCocos2D
also, perhaps gyroscope rotation is expressed not in absolute rotation but in rotation difference? Check that. Perhaps you actually meant to use the accelerometer, not the gyro? - LearnCocos2D

1 Answers

0
votes

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];
    }

}