使用iOS中的触摸输入旋转3D对象

时间:2012-03-30 07:36:17

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

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

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

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

- (IBAction)handlePan:(UIPanGestureRecognizer *)识别器{

    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物体的一些旋转,但它并不像它应该的那样自然。任何帮助都感激不尽。谢谢。

2 个答案:

答案 0 :(得分:0)

嗯..我从未使用过UIPanGestureRecognizer,所以我不知道怎样以及为什么它不起作用但我用UITouch移动一个3d对象(我发现它更容易)

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

您第一次接触touchesBegan(UITouchPhaseBegan

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

你也可以选择在视图中移动对象:你点击两次然后移动手指(就像笔记本电脑的触控板一样)

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

很明显,你无法使用2d曲面移动所有3维的3d对象

了解UITouch class reference

的更多信息

我可能没有解释得很好......但是你得到了基本的想法

答案 1 :(得分:0)

我认为this可以帮到你。这是教程,有几种方法可以正确的方式实现