Search code examples
iosrotationcore-animationtrigonometry

How do I calculate the height of the bottom edge when rotating a plane on the x-axis in 3d space?


In my iOS app, I have a view that I'm translating on the X axis in 3D space. The anchor point for the view is in the top center. If I rotate the view M_PI degrees, it will appear to go flat. I need X pixels between the bottom of the screen and the bottom edge of the rotated view (essentially, the bottom edge's height off the ground needs to be X px). How do I calculate this percentage of M_PI?

Here's an example. In this case the view is 100px high, and I need to rotate it some percentage of M_PI such that there's 40px from the bottom edge of the screen.

a image


Solution

  • This is fairly simple trigonometry. If you want 40 pixels between the bottom of your shape and the bottom of the screen, then you have 60 pixels between the top and bottom edges of your shape. You know that before rotation your shape was 100 pixels tall, so to find the angle, just take arccos(60/100) = 53.13º = .295167 * M_PI.