I have implemented a space shooter game project with Cocos2d 2.0 and a UIAccelleration controller. The UIAccelleration as some drawbacks:
I have found some tutorial on how to use Gyroscope instead (I am aware that is only on latest iPhones/IPods but as I am using already Cocos2d 2.0 -> OpenGL 2.0 ES it shouldn't matter).
Are there any test projects where I can try the Gyroscope? It is still not 100% clear to me how it works and if this will bring a real change on my code.
That's my UIAcceleration based controller:
#define kHeroMovementAction 1
#define kPlayerSpeed 100
- (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)];
}
}
At the end of the day I used the Gyroscope as main input source for moving my characters. The reason is that the Accellerometer provides as value the accelleration expressed in G-force and the accelleration alone dosn't give you any information on whether or not the player is holding the device in the correct angle. Using Accellerometer I had to calibrate the device in respect to the initial accelleration at every new GameScene. The use of Gyroscope instead allows to have constant values in respect to the absolute position of the device and in this way I can skip the calibration phase (that might be annoying to the user) and leave only the option to calibrate the sensitivity (and not to set an initial playing position at each GameScene).
As @LearnCocos2D said Gyroscope is not a subsitute for Accellerometer, so, I could now easily integrate Accelleration detection to trigger special moves/shoots or other fancy things.