Added logic for adjusting to screen orientation and removed final negative z

This commit is contained in:
BastiaanOlij 2016-11-19 23:04:06 +11:00
parent ee98e06952
commit efa9ded5f9
3 changed files with 34 additions and 11 deletions

View file

@ -34,12 +34,9 @@
// 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, UIAccelerometerDelegate, GLViewDelegate> {
@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;

View file

@ -196,7 +196,6 @@ static int frame_count = 0;
}; break; // no fallthrough
default: {
if (OSIPhone::get_singleton()) {
// OSIPhone::get_singleton()->update_accelerometer(accel[0], accel[1], accel[2]);
if (motionInitialised) {
@ -207,14 +206,42 @@ static int frame_count = 0;
// 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;
OSIPhone::get_singleton()->update_accelerometer(acceleration.x + gravity.x, acceleration.y + gravity.y, acceleration.z + gravity.z);
///@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;
OSIPhone::get_singleton()->update_magnetometer(magnetic.x, magnetic.y, magnetic.z);
///@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;
OSIPhone::get_singleton()->update_gyroscope(rotation.x, rotation.y, rotation.z);
// 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?
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIDeviceOrientationLandscapeLeft: {
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_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_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_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();

View file

@ -325,8 +325,8 @@ static const float ACCEL_RANGE = 1;
void OSIPhone::update_accelerometer(float p_x, float p_y, float p_z) {
///@TODO I've made the Z negative like the original accelerometer code, this probably needs more work
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) {
@ -366,8 +366,7 @@ 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) {
///@TODO I've made the Z negative like the original accelerometer code, this probably needs more work
input->set_magnetometer(Vector3(p_x, p_y, -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) {