简体   繁体   English

iOS openGL触摸触摸旋转球体的最佳方法

[英]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: 如果发现对象以与预期相反的方向旋转,则有三种可能性:

  1. If it's only the vertical rotation that goes the wrong way, you need to negate diffY . 如果只是垂直旋转的方向错误,则需要否定diffY This is a common mistake I make due to inconsistencies between OpenGL and UIKit coordinate systems. 由于OpenGL和UIKit坐标系之间的不一致,这是我经常犯的错误。
  2. If it's all rotation, you can either swap the arguments in the cross-product or use [0, 0, -1]. 如果全部旋转,则可以在叉积中交换参数,也可以使用[0,0,-1]。 This is usually because of confusion between left- and right-handed coordinate systems. 这通常是因为左手坐标系和右手坐标系之间存在混淆。
  3. If it's just the horizontal rotation, make both adjustments. 如果只是水平旋转,请进行两个调整。 (Don't negate 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: 这里有一些优点:

  • it's very straightforward to apply user interaction to current rotation state 将用户交互应用于当前轮播状态非常简单
  • no gimbal lock problems 没有万向节锁定问题
  • no need for matrix drift adjustments after repeated rotations 重复旋转后无需调整矩阵漂移
  • you can interpolate rotations easily 您可以轻松地插入旋转

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM