开发者

How do I determine the rotation angle of a 3-D object from a swipe gesture?

I am creating an OpenGL ES 2.0 application. I wish to implement touch controls so that I can rotate my 3D object while I perform gestures (like a swipe) on the screen.

I have functions for both rotating the camera and rotating the object. My problem is that I need to know what rotation angles, in X and Y, must be passed into the rotation function.

If a user performs a swipe in a particular direction, how do I calculate 开发者_StackOverflow社区the angles in the X and Y directions, so that they can be passed to the rotation function?


There are a couple of ways that you can approach the task of translating touch movement into rotation of a 3-D object: set up a trackball, or use scaled pixel displacements to map to rotation.

I do the latter in this OpenGL ES 2.0 sample application, where I perform rotation of a cube in response to the displacement of your finger in X and Y. In this case, I use the touch point coordinate differences to map directly to rotation, using code like the following:

    GLfloat totalRotation = sqrt(xRotation*xRotation + yRotation*yRotation);

    CATransform3D temporaryMatrix = CATransform3DRotate(currentCalculatedMatrix, totalRotation * M_PI / 180.0, 
                                                        ((xRotation/totalRotation) * currentCalculatedMatrix.m12 + (yRotation/totalRotation) * currentCalculatedMatrix.m11),
                                                        ((xRotation/totalRotation) * currentCalculatedMatrix.m22 + (yRotation/totalRotation) * currentCalculatedMatrix.m21),
                                                        ((xRotation/totalRotation) * currentCalculatedMatrix.m32 + (yRotation/totalRotation) * currentCalculatedMatrix.m31));

This roughly treats a movement of one point on the screen as one degree, which seemed to work well for my needs. You can scale this up or down to adjust the sensitivity of rotation.

The CATransform3D I use here is converted to an OpenGL model view matrix through a helper function and passed into my shaders as a matrix uniform.

A trackball is another way of handling this rotation, and Bill Dudney supplies an example of that in his Core Animation sample here. You could lift his trackball implementation and do the same CATransform3D conversion I do in my application to generate the appropriate model view matrix from that code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜