1

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

setRotationMatrix(浮动角度,浮动x,浮动y,浮动z,浮动*矩阵);

但是我不确定如何使用触摸的 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 对象的一些旋转,但它并不像应有的那样自然。任何帮助都感激不尽。谢谢。

4

2 回答 2

0

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

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

你第一次接触到 touchesBegan ( UITouchPhaseBegan)

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

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

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

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

更多查看UITouch 类参考

我可能没有很好地解释......但你明白了基本的想法

于 2012-03-30T15:40:14.430 回答
0

我认为应该对你有所帮助。这是教程,有几种方法可以以正确的方式制作它

于 2013-06-01T05:33:17.770 回答