Now here’s a nifty piece: How to do 3D object transformations without OpenGL — all simply using CoreAnimation! It’s called Trackball and the details can be found here:
The idea is that you have a 2D viewport into a 3D scene, this view port has a width and height (i.e. the CGRect that defines the layers bounds). In this 3D world you construct an imaginary sphere with a radius of the minimum of height or width of your view port centered on the center of your scene. When the event begins (with a touchesBegan:withEvent:) you initialize the trackball with the touches location as the starting point. A vector is constructed from the center of the sphere to the touch (the depth dimension is calculated based on the radius of the sphere). As the user moves her finger around on the screen another vector is constructed from the center of the sphere to the current touch location (as received in touchesMoved:withEvent:). The cross product of these two vectors is the vector of rotation and the angle between them is the magnitude of the rotation.
If all that is a little bit overwhelming, there’s a followup post with a sample application – but an even better example can be found on github where some bright spark has put together a project called ‘Cubo’ which makes it really easy to visualize the transformations by use of a numbered cube, like this:
Cool stuff, indeed. We’d also like to point out that the originator of the Trackball code is the Bill Dudney that wrote the most excellent Core Animation for Mac OS X and the iPhone book, which if you don’t have you should click on that link and purchase immediately, and is coauthor on a new iPhone SDK Development book which will no doubt also be well worth your time to order!
[EDIT: And also check out tomorrow's post about applying Core Animation to Open GL transformations!]
h/t: iPhoneKicks!

