Algorithm for optimal control on space ship using accelerometer input data

Posted by mm24 on Game Development See other posts from Game Development or by mm24
Published on 2013-06-12T17:29:35Z Indexed on 2014/05/27 16:05 UTC
Read the original article Hit count: 257

Does someone have a good algorithm for controlling a space ship in a vertical shooter game using acceleration data?

I have done a simple algorithm, but works very badly. I save an initial acceleration value (used to calibrate the movement according to the user's initial position) and I do subtract it from the current acceleration so I get a "calibrated" value. The problem is that basing the movement solely on relative acceleration has an effect of loss of sensitivity: certain movements are independent from the initial position.

Would anyone be able to share a a better solution? I am wondering if I should use/integrate also inputs from gyroscope hardware.

Here is my sample of code for a Cocos2d iOS game:

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    if (calibrationLayer.visible){
        [self evaluateCalibration:acceleration];
        initialAccelleration=acceleration;
        return;
    }

    if([self evaluatePause]){
        return;
    }

    ShooterScene * shooterScene = (ShooterScene *) [self parent];
    ShipEntity *playerSprite = [shooterScene playerShip];

    float accellerationtSensitivity = 0.5f;

    UIAccelerationValue xAccelleration = acceleration.x - initialAccelleration.x;
    UIAccelerationValue yAccelleration = acceleration.y - initialAccelleration.y;


    if(xAccelleration > 0.05 || xAccelleration < -0.05) {
        [playerSprite setPosition:CGPointMake(playerSprite.position.x +  xAccelleration * 80, playerSprite.position.y + yAccelleration * 80)];
    }
    else if(yAccelleration > 0.05 || yAccelleration < -0.05)
    {
        [playerSprite setPosition:CGPointMake(playerSprite.position.x +  xAccelleration * 80, playerSprite.position.y + yAccelleration * 80)];
    }


}

© Game Development or respective owner

Related posts about game-mechanics

Related posts about control