Twist, Touch & TravelTouch, Twist, and Travel
Harnessing the iPhone's Unique Features
Glenda AdamsMaverick Software
www.mavericksoftwaregames.com
Your Mission:Stand out among 22,000 games
Feel like an iPhone app!
Take advantage of the hardware
Unique control schemes
Don’t just port!
iPhone Hardware
Accelerometer
Multi-touch display
Location awareness
Compass (3GS only)
Accelerometer
Detect device orientation
Game input (steer, swing, balance)
Alternate input (shake, flip)
Accelerometer Interface
Provides X,Y,Z axis values
Updates via delegate callback, you set the frequency (up to 100 Hz)
Hardware is very sensitive, you must filter the data
Provide your own calibration
Accelerometer Setup:
- (void) setupAccelerometer
{
UIAccelerometer* accel = [UIAccelerometer sharedAccelerometer];
accel.updateInterval = 1.0f / kFrequency;
accel.delegate = self;
}
Delegate callback:(void)accelerometer:(UIAccelerometer*)accelerometer
didAccelerate:(UIAcceleration*)acceleration
{
// Get axis values
UIAccelerationValue x, y, z;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
}
Simple low-pass filter to track gravity vector
static BOOL init = NO;static UIAccelerationValue xValue = 0.0,
yValue = 0.0, zValue = 0.0;
if(!init) { // Get the initial value
gravityX = acceleration.x;gravityY = acceleration.y;init = YES;
}else { // Modify the intial value with the current one
gravityX = (acceleration.x * 0.1) + (gravityX * 0.9);gravityY = (acceleration.y * 0.1) + (gravityY * 0.9);
}
** Not accurate enough for a game controller! **
Accelerometer Tips For tilt controls, calibrate a “home”
position at game start, pause/resume, start of a level, etc.
Delegate callbacks are not guaranteed exact timing.
View based orientation support:
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation
Multi-touch display
Direct manipulation
Support multiple simultaneous touches
Gestures
Single touch sequence
Touch Began Touch Moved Touch Ended
Multi-touch sequence
Touch1 Began Touch1 Moved Touch2 Began
Touch2 Moved Touch2 Ended Touch1 Ended
UIResponder Touch-related Methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event;- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event;- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event;- (void)touchesCancelled:(NSSet *)touches withEvent:
(UIEvent*)event;
UITouch properties
UITouchPhase phase;NSUInteger tapCount;NSTimeInterval timestamp;UIView* view;UIWindow* window;
Finding a touch location
UITouch* touch = [touches anyObject];CGPoint where = [touch locationInView:myView];
Touch tips No API for gesture support
Size of touch area is 44x44, design UI accordingly
UITouch objects live across calls to TouchesBegan, Moved, etc.
Make sure to handle TouchesCancelled!
Handle multiple touches for complex multi-button virtual controls
Location Awareness
Accessed via the Core Location framework
User can opt-out
All devices, not just GPS enabled.
GPS, <40 m
Wifi, <100 m
Cell Towers, 1km-3km
Data from Core Location
Latitude
Longitude
Altitude
Accuracy
Properties:BOOL locationServicesEnabled;
CLLocationAccuracy desiredAccuracy;
CLLocation * location;
Methods:- (void) startUpdatingLocation;
- (void) stopUpdatingLocation;
Delegate Methods:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
Core Location tips Limit time between
startUpdatingLocation and stopUpdatingLocation
Handle loss/denial of location services
GPS drains battery fast!
Compass
Currently only on iPhone 3GS
Same x,y,z axis as accelerometer
Magnetic north vs True north
Part of Core Location framework
Compass specific Core Location Properties:BOOL headingAvailable;
CLLocationDegrees headingFilter;
CLHeading propertiesCLLocationDirection magneticHeading;
CLLocationDirection trueHeading;
CLLocationDirection headingAccuracy;
CLHeadingComponentValue x,y,z;
Methods:- (void) startUpdatingHeading;
- (void) stopUpdatingHeading;
Delegate Methods:
- (void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
Compass tips Interference
Recalibration
True north only available if location is known
Recap
Use hardware features to make your game feel natural on the iPhone
Look at Apple sample code
Think Different
Twist, Touch & TravelTouch, Twist, and Travel
Harnessing the iPhone's Unique Features
Glenda AdamsMaverick Software
Q&A