Skip to content

Instantly share code, notes, and snippets.

@nathanclark
Created December 15, 2011 20:03
Show Gist options
  • Select an option

  • Save nathanclark/1482630 to your computer and use it in GitHub Desktop.

Select an option

Save nathanclark/1482630 to your computer and use it in GitHub Desktop.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
/* Basic setup to translate the tile to gravity */
b2Vec2 gravity(-acceleration.y * 15, acceleration.x *15);
_world->SetGravity(gravity);
/* First lets figure out the angle to which the iPad is tilted*/
float angle = atan2(acceleration.x, -acceleration.y);
/* If tilted left then flick the eagle to tilt left */
if (-angle < 1.2 && !flickLeft) {
/* We dont want to do this everytime, only once per tilt.*/
flickLeft = YES;
/* FLICK THAT EAGLE */
eagleBody->ApplyAngularImpulse(-1500);
NSLog(@"LEFT TURN");
/* We dont want to level out yet. */
levelOut = NO;
/* If tilted right then flick the eagle to tilt right */
}else if (-angle > 1.8 && !flickRight) {
/* We dont want to do this everytime, only once per tilt.*/
flickRight = YES;
/* We dont want to level out yet. */
levelOut = NO;
/* FLICK THAT EAGLE */
eagleBody->ApplyAngularImpulse(1500);
NSLog(@"RIGHT TURN");
/* Did the user level out */
}else if (-angle < 1.6 && -angle > 1.5) {
/* These are some flags we will use later in the tick: method */
levelOut = YES;
if (flickLeft) {
bankedLeft=YES;
}else if (flickRight){
bankedRight = YES;
}
flickRight = NO;
flickLeft = NO;
}
NSLog(@"Eagle angle %f iPad angle %f", eagleBody->GetAngle(), angle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment