Commits

p2 committed c5294d6

Gravity rotation now references the orientation the device had when gravity rotation was activated

Comments (0)

Files changed (4)

Classes/EAGLView.m

 
 @synthesize animating;
 @dynamic animationFrameInterval;
-@dynamic useGravity;
+@synthesize useGravity;
 
 
 // You must implement this method
 
 
 #pragma mark KVC
-- (BOOL) useGravity
-{
-	return useGravity;
-}
-
 - (void) setUseGravity:(BOOL)flag
 {
 	if (flag != useGravity) {
 		useGravity = flag;
 		
+		// turn on gravity
 		if (useGravity) {
 			[self beginSamplingRotationData];
 		}
+		
+		// turn off gravity
 		else {
 			[self endSamplingRotationData];
+			
+			int does_autorot;
+			getProperties(NULL, NULL, NULL, &does_autorot, NULL, NULL, NULL, NULL, NULL);
+			if (does_autorot) {
+				setRotation(&QuaternionNull);
+			}
 		}
 	}
 }
 	if (useGravity) {
 		if (motionManager) {
 			CMAttitude *attitude = [[motionManager deviceMotion] attitude];
-			// TODO: Working here
-			//[attitude multiplyByInverseOfAttitude:referenceAttitude];
-			
-			CMQuaternion quat = [attitude quaternion];
-			Quaternion *qu = newQuat(quat.x, quat.y, quat.z, quat.w);
-			setRotation(qu);
-			free(qu);
+			if (attitude) {
+				if (!referenceAttitude) {
+					referenceAttitude = [attitude copy];
+				}
+				if (referenceAttitude) {
+					[attitude multiplyByInverseOfAttitude:referenceAttitude];
+				}
+				
+				CMQuaternion quat = [attitude quaternion];
+				Quaternion *qu = newQuat(quat.x, quat.y, quat.z, quat.w);
+				setRotation(qu);
+				free(qu);
+			}
 		}
-		else if (NULL != lastRotation) {
+		else if (lastRotation) {
 			setRotation(lastRotation);
 		}
 	}
 		referenceVector->x = 0;
 		referenceVector->y = -1;
 		referenceVector->z = 0;
+		normalizeVector(referenceVector);
 	}
 	
 	// start sampling
 	if ([motionManager isDeviceMotionAvailable]) {
 		[motionManager startDeviceMotionUpdates];
-		
-		referenceAttitude = [[[motionManager deviceMotion] attitude] retain];
+		// too early here to call [motionManager deviceMotion]
 	}
 	else {
 		[UIAccelerometer sharedAccelerometer].updateInterval = 1.0 / 15;
 
 - (void) endSamplingRotationData
 {
+	// shut down sampling
+	if ([motionManager isDeviceMotionActive]) {
+		[motionManager stopDeviceMotionUpdates];
+	}
+	else {
+		[UIAccelerometer sharedAccelerometer].delegate = nil;
+	}
+	
+	// clean up
 	if (NULL != referenceVector) {
 		free(referenceVector);
 		referenceVector = NULL;
 		[referenceAttitude release];
 		referenceAttitude = nil;
 	}
-	
-	// shut down sampling
-	if ([motionManager isDeviceMotionActive]) {
-		[motionManager stopDeviceMotionUpdates];
-	}
-	else {
-		[UIAccelerometer sharedAccelerometer].delegate = nil;
-	}
 }
 
 

Classes/glmatrix.h

 void draw_matrix();
 
 void setRotation(Quaternion *rotation);
-void setRotationImmediate(Quaternion *rotation);
+void setRotationImmediately(Quaternion *rotation);
 
 float setFloatProperty(char *property, float value);
 int setBoolProperty(char *property, int flag);

Classes/glmatrix.m

 	start_tracking();
 }
 
-void setRotationImmediate(Quaternion *rotation)
+void setRotationImmediately(Quaternion *rotation)
 {
 	m_config->currentRotation = *rotation;
 }
 		
 		m_config->targetRotation = QuaternionNull;
 		m_config->currentRotation = QuaternionNull;
-		m_config->track_duration = 0.12;				// seconds to animate between two quaternions
+		m_config->track_duration = 0.08;				// seconds to animate between two quaternions
 		m_config->auto_track_duration = 8.0;			// 8 seconds to animate from one to another orientation
 		m_config->auto_track_timeout = 20;				// 20 seconds pause on current orientation
 		
 
 void getProperties(float *its_speed, float *its_density, int *allows_rotate, int *does_autorotate, int *does_fog, int *does_waves, int *does_texture, int *does_clock, char **its_mode)
 {
-	*its_speed = speed;
-	*its_density = density;
-	*allows_rotate = allow_rotate;
-	*does_autorotate = do_autorotate;
-	*does_fog = do_fog;
-	*does_waves = do_waves;
-	*does_texture = do_texture;
-	*does_clock = do_clock;
-	*its_mode = mode;
+	if (its_speed) {
+		*its_speed = speed;
+	}
+	if (its_density) {
+		*its_density = density;
+	}
+	if (allows_rotate) {
+		*allows_rotate = allow_rotate;
+	}
+	if (does_autorotate) {
+		*does_autorotate = do_autorotate;
+	}
+	if (does_fog) {
+		*does_fog = do_fog;
+	}
+	if (does_waves) {
+		*does_waves = do_waves;
+	}
+	if (does_texture) {
+		*does_texture = do_texture;
+	}
+	if (does_clock) {
+		*does_clock = do_clock;
+	}
+	if (its_mode) {
+		*its_mode = mode;
+	}
 }
 
 

MatrixCode.xcodeproj/project.pbxproj

 		EE17BDB811B570D400D30886 /* quaternion.c in Sources */ = {isa = PBXBuildFile; fileRef = EE17BDB711B570D400D30886 /* quaternion.c */; };
 		EE17C59A11BD977B00D30886 /* vector3.c in Sources */ = {isa = PBXBuildFile; fileRef = EE17C59911BD977B00D30886 /* vector3.c */; };
 		EE96754311C19AA0009D3D74 /* matrix.pvrtc in Resources */ = {isa = PBXBuildFile; fileRef = EE96754211C19AA0009D3D74 /* matrix.pvrtc */; };
+		EED1447C13934D24006CA8BE /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EED1447B13934D24006CA8BE /* CoreMotion.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
 		EEF6937C11B2BCFF0027A853 /* glmatrix.m in Sources */ = {isa = PBXBuildFile; fileRef = EE85E0B7110102EF001F04EE /* glmatrix.m */; };
 		EEF6957111B2F4560027A853 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = EEF6957011B2F4560027A853 /* MainWindow.xib */; };
 		EEF6957911B2F50D0027A853 /* ES1Renderer.m in Sources */ = {isa = PBXBuildFile; fileRef = EEF6957511B2F50D0027A853 /* ES1Renderer.m */; };
 		EE96754211C19AA0009D3D74 /* matrix.pvrtc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = matrix.pvrtc; sourceTree = "<group>"; };
 		EEB677CE1122231E00C58E5C /* helpers.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = helpers.c; sourceTree = "<group>"; };
 		EEB677D2112223F200C58E5C /* helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = helpers.h; sourceTree = "<group>"; };
+		EED1447B13934D24006CA8BE /* CoreMotion.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMotion.framework; path = System/Library/Frameworks/CoreMotion.framework; sourceTree = SDKROOT; };
 		EEF6938711B2BD460027A853 /* glmatrix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = glmatrix.h; sourceTree = "<group>"; };
 		EEF6957011B2F4560027A853 /* MainWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MainWindow.xib; sourceTree = "<group>"; };
 		EEF6957411B2F50D0027A853 /* ES1Renderer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ES1Renderer.h; sourceTree = "<group>"; };
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				EED1447C13934D24006CA8BE /* CoreMotion.framework in Frameworks */,
 				1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */,
 				1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */,
 				28FD15000DC6FC520079059D /* OpenGLES.framework in Frameworks */,
 		29B97314FDCFA39411CA2CEA /* CustomTemplate */ = {
 			isa = PBXGroup;
 			children = (
+				EED1447B13934D24006CA8BE /* CoreMotion.framework */,
 				080E96DDFE201D6D7F000001 /* Classes */,
 				29B97315FDCFA39411CA2CEA /* Other Sources */,
 				29B97317FDCFA39411CA2CEA /* Resources */,