[英]IOS openGL best way to rotate Sphere with touchesMoved
I drew Globe object using OpenGL and i can rotate it with finger touch , but it doesn't work well in some cases because i am rotating using the difference between x and y 我使用OpenGL绘制了Globe对象,并且可以用手指触摸来旋转它,但是在某些情况下它无法正常工作,因为我使用x和y之间的差值进行旋转
Rotation3D rot = sphere.currentRotation;
rot.x += diffX ;
rot.y += diffY ;
rot.z += 10 ;
sphere.currentRotation = rot;
when you move your finger from Top Right to bottom Left it isn't work good. 当您将手指从右上移到左下时,效果不好。
Any ideas ? 有任何想法吗 ?
Thanks Peter Gabra 谢谢彼得·加布拉
To arbitrarily rotate objects, it's easiest to store their current orientation as a transformation matrix and manipulate the elements. 要任意旋转对象,最简单的方法是将它们的当前方向存储为变换矩阵并操纵这些元素。 I explain this in detail here .
我在这里详细解释。
The only difference is that in that other question, the OP wanted to apply rotations from two controls (horizontal and vertical), whereas you are after drag-based rotation. 唯一的不同是,在另一个问题中,OP希望应用来自两个控件(水平和垂直)的旋转,而您是基于拖动的旋转。 The technique is basically the same, but instead of rotating around either the X or Y axis, you need to compute an arbitrary axis of rotation from the touch's delta vector as follows:
该技术基本上是相同的,但是除了绕X轴或Y轴旋转之外,您还需要根据触摸的增量矢量计算任意旋转轴,如下所示:
axis = [0, 0, 1] ⨯ [diffX, diffY, 0]
( ⨯
= "cross product") (
⨯
=“叉积”)
Then you rotate the U, V and W vectors (as described in my other answer) around the axis by some angle in proportion to the length of the delta vector: 然后,将U,V和W向量(如我的其他答案所述)围绕轴旋转一定角度,与增量矢量的长度成比例:
M = rotation(k * length([diffX, diffY, 0]), axis)
U = M * U
V = M * V
W = M * W
If you find the object rotating in the opposite direction to what you expect, there are three possibilities: 如果发现对象以与预期相反的方向旋转,则有三种可能性:
diffY
. diffY
。 This is a common mistake I make due to inconsistencies between OpenGL and UIKit coordinate systems. diffX
, no one uses left-to-right X-coordinates.) diffX
,没有人使用从左到右的X坐标。) In case you're using Euler angles: I recommend not using Euler angles to model rotations. 如果您使用的是欧拉角: 建议不要使用欧拉角来模拟旋转。 Use Quaternions instead.
请改用四元数。 It might seem like it makes your code more complicated, but rotations work well when you use Quaternions.
看起来这使您的代码更加复杂,但是当您使用四元数时,轮换效果很好。 Here's some advantages:
这里有一些优点:
Note that Apple give you a Quaternion type to use: GLKQuaternion . 请注意,Apple为您提供了要使用的四元数类型: GLKQuaternion 。 No need to write your own Quaternion class.
无需编写自己的Quaternion类。
See also: 也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.