Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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


}
share|improve this question
1  
I'm not sure what the issue is. You mention a loss in sensitivity, but that should not be caused by calibration. Can you give a concrete example of a problem you've run into with this method? –  Marcks Thomas Jun 13 '13 at 13:09

1 Answer 1

I think some of your problems might be caused by a naive calibration like:

calibratedAcceleration = currentAcceleration - initialAcceleration

When you're rotating your phone and not moving it then the acceleration returned will lie on a sphere with Earth G radius.

For example let's say your phone lies almost flat on the table - your acceleration X and Y values will be small and your method should work, but when you turn your phone 90 deg your acceleration X or Y will get its maximum value and your method will break. In practice, for rotations just around 15-30 deg you should start noticing some strange effects, like non-symmetrical steering in front-back directions.

To solve it you could store entire initial rotation, computed from initial acceleration vector and during the game compute actual rotation and a X/Y differences between them.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.