diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-01-15 00:08:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-15 00:08:46 +0100 |
commit | d4eb8ec884c0519dc02de4451e249f6f5cca962a (patch) | |
tree | f0fe3497f7bbba49de5e938000ca463101b6c50c /platform | |
parent | dab73c701a9785be443977a613e57600d1e136c8 (diff) | |
parent | 5e4dcb3b7f93cfe19a5c12700df5a7c82045f37c (diff) |
Merge pull request #7127 from BastiaanOlij/ios_meters
Core motion implementation for iPhone (Accelerometer/Gyro/Magnetometer support)
Diffstat (limited to 'platform')
-rw-r--r-- | platform/iphone/app_delegate.h | 8 | ||||
-rw-r--r-- | platform/iphone/app_delegate.mm | 83 | ||||
-rw-r--r-- | platform/iphone/detect.py | 3 | ||||
-rw-r--r-- | platform/iphone/os_iphone.cpp | 13 | ||||
-rw-r--r-- | platform/iphone/os_iphone.h | 3 |
5 files changed, 104 insertions, 6 deletions
diff --git a/platform/iphone/app_delegate.h b/platform/iphone/app_delegate.h index 09c401b329..6883692b15 100644 --- a/platform/iphone/app_delegate.h +++ b/platform/iphone/app_delegate.h @@ -30,11 +30,13 @@ #import "gl_view.h" #import "view_controller.h" -@interface AppDelegate : NSObject <UIApplicationDelegate, UIAccelerometerDelegate, GLViewDelegate> { +// Old accelerometer approach deprecated since IOS 7.0 +// Include coremotion for accelerometer, gyroscope and magnetometer access, available since IOS 4.0 but some functionality won't work for anything before IOS 5.0 +#import <CoreMotion/CoreMotion.h> + +@interface AppDelegate : NSObject <UIApplicationDelegate, GLViewDelegate> { //@property (strong, nonatomic) UIWindow *window; ViewController* view_controller; - UIAccelerationValue accel[3]; - UIAccelerationValue last_accel[3]; }; @property (strong, nonatomic) UIWindow *window; diff --git a/platform/iphone/app_delegate.mm b/platform/iphone/app_delegate.mm index feb87e742f..664683ad64 100644 --- a/platform/iphone/app_delegate.mm +++ b/platform/iphone/app_delegate.mm @@ -84,6 +84,9 @@ extern char** gargv; extern int iphone_main(int, int, int, char**); extern void iphone_finish(); +CMMotionManager *motionManager; +bool motionInitialised; + static ViewController* mainViewController = nil; + (ViewController*) getViewController { @@ -193,9 +196,58 @@ static int frame_count = 0; }; break; // no fallthrough default: { - if (OSIPhone::get_singleton()) { - OSIPhone::get_singleton()->update_accelerometer(accel[0], accel[1], accel[2]); +// OSIPhone::get_singleton()->update_accelerometer(accel[0], accel[1], accel[2]); + if (motionInitialised) { + // Just using polling approach for now, we can set this up so it sends data to us in intervals, might be better. + // See Apple reference pages for more details: + // https://developer.apple.com/reference/coremotion/cmmotionmanager?language=objc + + // Apple splits our accelerometer date into a gravity and user movement component. We add them back together + CMAcceleration gravity = motionManager.deviceMotion.gravity; + CMAcceleration acceleration = motionManager.deviceMotion.userAcceleration; + + ///@TODO We don't seem to be getting data here, is my device broken or is this code incorrect? + CMMagneticField magnetic = motionManager.deviceMotion.magneticField.field; + + ///@TODO we can access rotationRate as a CMRotationRate variable (processed date) or CMGyroData (raw data), have to see what works best + CMRotationRate rotation = motionManager.deviceMotion.rotationRate; + + // Adjust for screen orientation. + // [[UIDevice currentDevice] orientation] changes even if we've fixed our orientation which is not + // a good thing when you're trying to get your user to move the screen in all directions and want consistent output + + ///@TODO Using [[UIApplication sharedApplication] statusBarOrientation] is a bit of a hack. Godot obviously knows the orientation so maybe we + // can use that instead? (note that left and right seem swapped) + + switch ([[UIApplication sharedApplication] statusBarOrientation]) { + case UIDeviceOrientationLandscapeLeft: { + OSIPhone::get_singleton()->update_gravity(-gravity.y, gravity.x, gravity.z); + OSIPhone::get_singleton()->update_accelerometer(-(acceleration.y + gravity.y), (acceleration.x + gravity.x), acceleration.z + gravity.z); + OSIPhone::get_singleton()->update_magnetometer(-magnetic.y, magnetic.x, magnetic.z); + OSIPhone::get_singleton()->update_gyroscope(-rotation.y, rotation.x, rotation.z); + }; break; + case UIDeviceOrientationLandscapeRight: { + OSIPhone::get_singleton()->update_gravity(gravity.y, -gravity.x, gravity.z); + OSIPhone::get_singleton()->update_accelerometer((acceleration.y + gravity.y), -(acceleration.x + gravity.x), acceleration.z + gravity.z); + OSIPhone::get_singleton()->update_magnetometer(magnetic.y, -magnetic.x, magnetic.z); + OSIPhone::get_singleton()->update_gyroscope(rotation.y, -rotation.x, rotation.z); + }; break; + case UIDeviceOrientationPortraitUpsideDown: { + OSIPhone::get_singleton()->update_gravity(-gravity.x, gravity.y, gravity.z); + OSIPhone::get_singleton()->update_accelerometer(-(acceleration.x + gravity.x), (acceleration.y + gravity.y), acceleration.z + gravity.z); + OSIPhone::get_singleton()->update_magnetometer(-magnetic.x, magnetic.y, magnetic.z); + OSIPhone::get_singleton()->update_gyroscope(-rotation.x, rotation.y, rotation.z); + }; break; + default: { // assume portrait + OSIPhone::get_singleton()->update_gravity(gravity.x, gravity.y, gravity.z); + OSIPhone::get_singleton()->update_accelerometer(acceleration.x + gravity.x, acceleration.y + gravity.y, acceleration.z + gravity.z); + OSIPhone::get_singleton()->update_magnetometer(magnetic.x, magnetic.y, magnetic.z); + OSIPhone::get_singleton()->update_gyroscope(rotation.x, rotation.y, rotation.z); + }; break; + }; + } + bool quit_request = OSIPhone::get_singleton()->iterate(); }; @@ -253,11 +305,24 @@ static int frame_count = 0; [window makeKeyAndVisible]; //Configure and start accelerometer +/* + Old accelerometer approach deprecated since IOS 7.0 + last_accel[0] = 0; last_accel[1] = 0; last_accel[2] = 0; [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)]; [[UIAccelerometer sharedAccelerometer] setDelegate:self]; +*/ + + if (!motionInitialised) { + motionManager = [[CMMotionManager alloc] init]; + if (motionManager.deviceMotionAvailable) { + motionManager.deviceMotionUpdateInterval = 1.0/70.0; + [motionManager startDeviceMotionUpdates]; + motionInitialised = YES; + }; + }; //OSIPhone::screen_width = rect.size.width - rect.origin.x; //OSIPhone::screen_height = rect.size.height - rect.origin.y; @@ -297,12 +362,23 @@ static int frame_count = 0; - (void)applicationWillTerminate:(UIApplication*)application { printf("********************* will terminate\n"); + + if (motionInitialised) { + ///@TODO is this the right place to clean this up? + [motionManager stopDeviceMotionUpdates]; + [motionManager release]; + motionManager = nil; + motionInitialised = NO; + }; + iphone_finish(); }; - (void)applicationDidEnterBackground:(UIApplication *)application { printf("********************* did enter background\n"); + ///@TODO maybe add pause motionManager? and where would we unpause it? + if (OS::get_singleton()->get_main_loop()) OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT); [view_controller.view stopAnimation]; @@ -340,12 +416,15 @@ static int frame_count = 0; }; } +/* + Depricated since IOS 7.0 - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration { //Use a basic low-pass filter to only keep the gravity in the accelerometer values accel[0] = acceleration.x; // * kFilteringFactor + accel[0] * (1.0 - kFilteringFactor); accel[1] = acceleration.y; // * kFilteringFactor + accel[1] * (1.0 - kFilteringFactor); accel[2] = acceleration.z; // * kFilteringFactor + accel[2] * (1.0 - kFilteringFactor); } +*/ - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { #ifdef MODULE_FACEBOOKSCORER_IOS_ENABLED diff --git a/platform/iphone/detect.py b/platform/iphone/detect.py index b92b64e9f1..88ec83ef7a 100644 --- a/platform/iphone/detect.py +++ b/platform/iphone/detect.py @@ -83,6 +83,7 @@ def configure(env): '-framework', 'CoreAudio', '-framework', 'CoreGraphics', '-framework', 'CoreMedia', + '-framework', 'CoreMotion', '-framework', 'Foundation', '-framework', 'Security', '-framework', 'UIKit', @@ -109,6 +110,7 @@ def configure(env): '-framework', 'MediaPlayer', '-framework', 'AVFoundation', '-framework', 'CoreMedia', + '-framework', 'CoreMotion', ]) else: env.Append(LINKFLAGS=['-arch', 'armv7', '-Wl,-dead_strip', '-miphoneos-version-min=5.1.1', @@ -126,6 +128,7 @@ def configure(env): '-framework', 'MediaPlayer', '-framework', 'AVFoundation', '-framework', 'CoreMedia', + '-framework', 'CoreMotion', ]) if env['game_center'] == 'yes': diff --git a/platform/iphone/os_iphone.cpp b/platform/iphone/os_iphone.cpp index 1d12501aea..051c562b6a 100644 --- a/platform/iphone/os_iphone.cpp +++ b/platform/iphone/os_iphone.cpp @@ -323,9 +323,14 @@ void OSIPhone::touches_cancelled() { static const float ACCEL_RANGE = 1; +void OSIPhone::update_gravity(float p_x, float p_y, float p_z) { + input->set_gravity(Vector3(p_x, p_y, p_z)); +}; + void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) { - input->set_accelerometer(Vector3(p_x / (float)ACCEL_RANGE, p_y / (float)ACCEL_RANGE, -p_z / (float)ACCEL_RANGE)); + // Found out the Z should not be negated! Pass as is! + input->set_accelerometer(Vector3(p_x / (float)ACCEL_RANGE, p_y / (float)ACCEL_RANGE, p_z / (float)ACCEL_RANGE)); /* if (p_x != last_accel.x) { @@ -364,7 +369,13 @@ void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) { */ }; +void OSIPhone::update_magnetometer(float p_x, float p_y, float p_z) { + input->set_magnetometer(Vector3(p_x, p_y, p_z)); +}; +void OSIPhone::update_gyroscope(float p_x, float p_y, float p_z) { + input->set_gyroscope(Vector3(p_x, p_y, p_z)); +}; void OSIPhone::delete_main_loop() { diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index 331b20afbf..ab976edcba 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -152,7 +152,10 @@ public: int set_base_framebuffer(int p_fb); + void update_gravity(float p_x, float p_y, float p_z); void update_accelerometer(float p_x, float p_y, float p_z); + void update_magnetometer(float p_x, float p_y, float p_z); + void update_gyroscope(float p_x, float p_y, float p_z); static OSIPhone* get_singleton(); |