what i have here works exactly how i want it. My question is that it seems overly complex, is there a faster/better way of doing the pitchAngleRadians calculations i am showing below?
the code is shown... (along with two images)
the first image is how the atan2 calculation comes out of the iPad naturally, "pitchAngleRadians = atan2(accel[0], accel2);" (what the accelerometer puts out normally)
what i want is to shift "Zero" to a new degree angle, in the second image i've shifted it a -135 degrees as an example. (everything in code is radians) so for instance the variable "pitchNeutralAngle" in this example is the radian equivalent of (-135) degrees
this code produces exactly what i want for the variable "pitchAngleRadians", which is the second image, but seems too long, and this code is in a tight run loop, so it would speed up if i can get this cleaner/faster.
i go through an intermediate step of "ranging" the degrees between 0 and 360, so that crossing the boarder of 180 degrees does not mess up the calculation, however i still have to get it back to the 0 to 180 form, so that is what the next calculation is.
i've tested this code a little bit, and it does appear to work.
//------------------------------------------------------------------------------------
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
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);
pitchAngleRadians = (atan2(accel[0], accel[2]) - pitchNeutralAngle);
pitchAngleRadians = pitchAngleRadians - floorf(pitchAngleRadians/6.28318f)*6.28318f;
if (pitchAngleRadians > 3.14159) pitchAngleRadians = -6.28318f + pitchAngleRadians;
//float a = atan2(accel[0], accel[2]);
//NSLog(@"a = %f", a);
}