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.

I'm trying to make a vertical shooter, using Cocos2D and a few tutorials. Unfortunately, most tutorials only show how to make an horizontal shooter.

Still, I tried to adapt the code from a horizontal shooter to a vertical one, but am having a few problems. Mainly, my ship keeps moving right, even when the iPhone is sitting perfectly still.

Seeing as I'm a beginner, I'm having trouble pinpointing the exact issue and I'd love if anyone could help me.

Here's my accelerometer method:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

    define kFilteringFactor 0.1

    define kRestAccelX -0.6

    define kShipMaxPointsPerSec (winSize.width*0.5)  

    define kMaxDiffX 0.2

    UIAccelerationValue rollingX;

    rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));

    float accelX = acceleration.x - rollingX;

    CGSize winSize = [CCDirector sharedDirector].winSize;

    float accelDiff = accelX - kRestAccelX;
    float accelFraction = accelDiff / kMaxDiffX;
    float pointsPerSec = kShipMaxPointsPerSec * accelFraction;

    _shipPointsPerSecX = pointsPerSec;


}

And my update method:

-(void)update:(ccTime)dt {

    CGSize winSize = [CCDirector sharedDirector].winSize;
    float maxX = winSize.width - _ship.contentSize.width/2;
    float minX = _ship.contentSize.width/2;

    float newX = _ship.position.x + (_shipPointsPerSecX * dt);
    newX = MIN(MAX(newX, minX), maxX);
    _ship.position = ccp(newX, _ship.position.y);

}

What could be causing this?

share|improve this question
    
Did you notice that your rollingX variable is read while not initialized? –  Chris Burt-Brown Mar 4 '12 at 8:51
add comment

1 Answer

up vote 3 down vote accepted

You don't really need this much code to do sliding across the ground.

Try simply:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
     _shipPointsPerSecX = acceleration.x * 10;
}

To get the ship to accelerate and decelerate you could try

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
     float targetSpeed = acceleration.x * 10;
     _shipPointsPerSecX = (_shipPointsPerSecX * .8f) + (targetSpeed * .2f);
}

But this isn't really accurate, a more realistic response might involve sliding towards the target value:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    float targetSpeed = acceleration.x * 10;
    if(_shipPointsPerSecX < targetSpeed) {
        _shipPointsPerSecX = MIN(_shipPointsPerSecX + 2, targetSpeed);
    } else {
        _shipPointsPerSecX = MAX(_shipPointsPerSecX - 2, targetSpeed);
    }
}
share|improve this answer
    
Thanks! Managed to tweak your code in order to fit my game and it's all working perfectly now! –  Henrique Sousa Mar 5 '12 at 1:41
    
@HenriqueSousa: if this answer helped you, mark it as correct (tick mark it from left) –  Adil Soomro May 29 '12 at 8:41
add comment

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.