From 6b9c8afd0e4a49efffd504378f1db34264928250 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Wed, 17 Aug 2011 20:29:16 -0400 Subject: Improved jumping gesture detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, when determining if the player had swiped upward for a jump in the Jump game mode, the game checked that the player had touched and moved upward a certain amount, with less than a certain amount of variance in horizontal position. However, this did not work well with the deeply-ingrained rotation aspect of the game, so the game was programmed to widen this allowed horizontal variance when the device was rotated. This really did not work too well, so now the game calculates an "expected" angle from the bottom of the screen based on rotation that it determines to point upward and also the angle of the player's swipe from the bottom of the screen. If the swipe angle falls within 45 degrees (π/4 radians) of the expected angle, the cart jumps. Really quite awesome if you ask me. :P Refs #204 --- Classes/JumpGameMode.h | 2 +- Classes/JumpGameMode.m | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Classes/JumpGameMode.h b/Classes/JumpGameMode.h index 7b7dff4..9753720 100644 --- a/Classes/JumpGameMode.h +++ b/Classes/JumpGameMode.h @@ -15,7 +15,7 @@ CGPoint gestureStartPoint; int jumpTick; BOOL jump; - float swipeVarianceModifer; + float expectedAngle; } @end diff --git a/Classes/JumpGameMode.m b/Classes/JumpGameMode.m index e8c8c7e..e30be6a 100644 --- a/Classes/JumpGameMode.m +++ b/Classes/JumpGameMode.m @@ -8,8 +8,7 @@ #import "JumpGameMode.h" -#define kMinimumGestureLength 25 -#define kMaximumVariance 5 +#define kMinimumGestureLength 50 @implementation JumpGameMode @@ -91,10 +90,10 @@ { UITouch* touch = [touches anyObject]; CGPoint gestureCurrentPosition = [touch locationInView:nil]; - CGFloat deltaX = fabsf(gestureStartPoint.x - gestureCurrentPosition.x); - CGFloat deltaY = fabsf(gestureStartPoint.y - gestureCurrentPosition.y); + CGFloat angle = atan2f(gestureCurrentPosition.y - gestureStartPoint.y, gestureCurrentPosition.x - gestureStartPoint.x); + CGFloat distance = sqrt(powf((gestureCurrentPosition.x - gestureStartPoint.x),2) + powf((gestureCurrentPosition.y - gestureStartPoint.y),2)); - if ((deltaX >= kMinimumGestureLength) && (deltaY <= kMaximumVariance*swipeVarianceModifer) && (cart.sprite.position.y == 22)) + if ((distance >= kMinimumGestureLength) && (angle >= expectedAngle - M_PI_4) && (angle <= expectedAngle + M_PI_4) && (cart.sprite.position.y == 22)) { jump = YES; } @@ -104,7 +103,7 @@ { [super accelerometer:accelerometer didAccelerate:acceleration]; - swipeVarianceModifer = MAX(1,(fabsf(acceleration.y) * 480)/kMaximumVariance); + expectedAngle = acceleration.y*M_PI_2; } @end -- cgit 1.4.1