Good Vibrations - Making your iTGB Game Vibrate
by Dave Calabrese · 05/08/2009 (12:07 am) · 4 comments
When it comes to programming vibrations on the iPhone / iPod Touch, the first thing to realize is that it is unfortunately limited. At least as of the writing of this resource, Apple has not exposed the Vibration features much - all you can do is toggle a vibration that lasts for about 1 second, and you have no control over its intensity. Hopefully this will change with iPhone OS 3.0.
The code is actually very easy however - although interesting. The vibration function is part of the audio class! You read that right - if you want to make your device vibrate, you have to call it through the audio code. What we actually do (or at least what Apple allows us to do through their SDK) is cause a message notification vibration event. So, if the user has message notification vibrations disabled on their device, then this code will not work - but it won't cause any errors or anything - it just won't do anything!
The lesson there is to use vibrations on the iPhone / iPod Touch as an additional form of notification - don't make it the only one!
Okay... on to the code. Open up /platformiPhone/iPhoneAudio.mm and look at the top there, where we have the includes. Make the code look like this:
And then, from TorqueScript, whenever you want the device to vibrate you just call "doDeviceVibrate();"!
Although limiting, there is still a lot you can do with this. If anyone manages to discover a way to control the vibrations - or if OS 3.0 allows it and I don't update this resource - add some comments and we'll keep this resource alive and updated!
The code is actually very easy however - although interesting. The vibration function is part of the audio class! You read that right - if you want to make your device vibrate, you have to call it through the audio code. What we actually do (or at least what Apple allows us to do through their SDK) is cause a message notification vibration event. So, if the user has message notification vibrations disabled on their device, then this code will not work - but it won't cause any errors or anything - it just won't do anything!
The lesson there is to use vibrations on the iPhone / iPod Touch as an additional form of notification - don't make it the only one!
Okay... on to the code. Open up /platformiPhone/iPhoneAudio.mm and look at the top there, where we have the includes. Make the code look like this:
#include "platformiPhone/platformiPhone.h"
#include "platform/platformAL.h"
//------------------------------
// New Audio Vibration Code Starts Here
#import <AudioToolbox/AudioToolbox.h>
ConsoleFunction(doDeviceVibrate, void, 1, 1, "Makes the device do a quick vibration.")
{
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
// New Audio Vibration Code Ends Here
//------------------------------
namespace Audio
{And then, from TorqueScript, whenever you want the device to vibrate you just call "doDeviceVibrate();"!
Although limiting, there is still a lot you can do with this. If anyone manages to discover a way to control the vibrations - or if OS 3.0 allows it and I don't update this resource - add some comments and we'll keep this resource alive and updated!
#2
05/08/2009 (12:53 pm)
iPhone Touch doesn't vibrate, you have to check UIDevice or it will just beep with an annoying sound.
#3
05/08/2009 (1:07 pm)
@Afan: Whoops! Good catch there... my iPod Touch is currently in someone elses hands however I'll be getting it back in the next day or so. I'll modify the code and test it on that and update this resource as soon as I do. Thanks again!!
#4
Of the top of my head I think checking the device goes something like this:
05/08/2009 (4:07 pm)
Not a problem always glad to help :D.Of the top of my head I think checking the device goes something like this:
if (![[[UIDevice currentDevice] model] isEqualToString: @"iPod Touch"])
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
Torque 3D Owner Chris Jorgensen
Cascadia Games LLC