Note that there are some explanatory texts on larger screens.

plurals
  1. PORotating Accelerometer output to new "neutral" for iOS, (and others).
    text
    copied!<p>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?</p> <p>the code is shown... (along with two images)</p> <p>the first image is how the atan2 calculation comes out of the iPad naturally, "pitchAngleRadians = atan2(accel[0], accel<a href="https://i.stack.imgur.com/Cij73.png" rel="nofollow noreferrer">2</a>);" (what the accelerometer puts out normally) </p> <p>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</p> <p>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.</p> <p>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.</p> <p>i've tested this code a little bit, and it does appear to work.</p> <pre><code>//------------------------------------------------------------------------------------ - (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 &gt; 3.14159) pitchAngleRadians = -6.28318f + pitchAngleRadians; //float a = atan2(accel[0], accel[2]); //NSLog(@"a = %f", a); } </code></pre> <p><img src="https://i.stack.imgur.com/jDHYD.png" alt="naturally comes out of the accelerometer"></p> <p><img src="https://i.stack.imgur.com/Cij73.png" alt="after re-&quot;neutralizing&quot; zero angle"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload