【问题标题】:Rotate a 3D Object using Touch inputs in iOS在 iOS 中使用触摸输入旋转 3D 对象
【发布时间】:2012-03-30 07:36:17
【问题描述】:

我正在开发一个 ios 应用程序,我需要通过在其上滑动手指来旋转 3d 矩阵(3D 对象)。我用于旋转的方法是

setRotationMatrix(float angle, float x, float y, float z, float *matrix);

但是我不确定如何使用触摸的 x 和 y 位置值来正确旋转 3D 矩阵。这是我正在使用的代码

-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

    if(recognizer.state == UIGestureRecognizerStateBegan)
    {
        panStartPoint = [recognizer locationInView:self];
        previousPoint.x = 0;
        previousPoint.y = 0;
        currentPoint.x = panStartPoint.x;
        currentPoint.y = panStartPoint.y;
    }
    else if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        panEndPoint = [recognizer locationInView:self];
    }
    else if (recognizer.state == UIGestureRecognizerStateChanged)
    {
        previousPoint.x = currentPoint.x;
        previousPoint.y = currentPoint.y;
        CGPoint instanteneousPoint = [recognizer locationInView:self];
        currentPoint.x = instanteneousPoint.x;
        currentPoint.y = instanteneousPoint.y;

        int xdifference = currentPoint.x - previousPoint.x;
        int ydifference = currentPoint.y - previousPoint.y;



        if((xdifference <= rotationThreshold *-1) || xdifference >= rotationThreshold)  //rotationThreshold = 3;
        {
            if(xdifference <= rotationThreshold *-1)
            {
                rotationY = -1.0;
            }
            else
            {
                rotationY = 1.0;
            }

            if( (ydifference >= rotationThreshold *-1) && ydifference <= rotationThreshold )
            {
                rotationX = 0.0;
            }
        }

        if((ydifference <= rotationThreshold *-1) || ydifference >= rotationThreshold)
        {
          if((ydifference <= rotationThreshold *-1))
          {
            rotationX = -1.0;
          }
          else
          {
              rotationX = 1.0;
          }
            if( (xdifference >= rotationThreshold *-1) && xdifference <= rotationThreshold )
            {
                rotationY = 0.0;
            }

        }
        if(rotationX != 0.0 || rotationY != 0.0)
        {
            rotationDegree += 5.0f;
            if(rotationDegree >= 360.0f)
            {
                rotationDegree = 0.0f;
            }
            ShaderUtils::rotatePoseMatrix(rotationDegree, rotationX, rotationY, rotationZ, &modelViewMatrix.data[0]); //rotationZ = 0;
        }}}

这为我提供了 3D 对象的一些旋转,但它并不像应有的那样自然。任何帮助都感激不尽。谢谢。

【问题讨论】:

    标签: objective-c xcode 3d rotational-matrices


    【解决方案1】:

    嗯..我从未使用过 UIPanGestureRecognizer,所以我不知道它是如何工作的以及为什么它不起作用,但我使用 UITouch 来移动 3d 对象(我发现它更容易)

    好的..所以这就是我所做的(我没有代码了,所以我不能给你......对不起):

    第一次接触 touchesBegan (UITouchPhaseBegan)

    然后,如果 touchesMoved 如果触摸在 y 上移动,则在 x 轴上旋转对象,如果在 x 上移动,则在 y 轴上旋转对象 ..以及 2 的任意组合(您必须自己设置灵敏度)

    您还可以选择像这样在视图中移动对象:轻按两次,然后移动手指(如笔记本电脑的触控板)

    要在 z 轴上移动它,只需使用 UIPinchGestureRecognizer

    很明显,您不能使用 2d 表面在所有 3 个维度上移动 3d 对象

    更多信息请关注UITouch class reference

    我可能解释得不是很好......但你明白了基本的想法

    【讨论】:

    • 谢谢skytz。平移手势或多或少与 UITOUCH 相同。如我的代码中所述,我遵循与您描述的完全相同的逻辑。它也会旋转,但旋转看起来并不那么自然。在网上搜索了无数个小时后,我有了一个想法,即为了用 2D 手势旋转 3d 对象,首先必须将该矩阵转换为其原始位置,然后找到它的四元数(无论是什么)并将它们相乘一起。或类似的东西。
    【解决方案2】:

    我认为this 应该可以帮助您。这是一个教程,有几种方法可以以正确的方式制作它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      相关资源
      最近更新 更多