1

我正在尝试使用触摸在 iOS 中旋转 OpenGL 对象,但遇到了一些麻烦。我在这里抓住用户的触摸:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self.view];
    }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
   {
   UITouch *touch = [touches anyObject];
   CGPoint point = [touch locationInView:self.view];
   dx = point.y - startPoint.y;
   dy = point.x - startPoint.x;
   startPoint = point;
   }

我在我的更新函数中使用它来执行旋转。现在,当我从左到右触摸时,我只是尝试从左到右旋转,然后在上下触摸时从前到后旋转。我得到了一个奇怪的组合轮换。这是代码:

- (void)update
   {    
   float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
   GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);

   self.effect.transform.projectionMatrix = projectionMatrix;

   GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -3.5f);
   modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, -1, startPoint.x, startPoint.y, 0.0f);
   dx = dy =0;
   self.effect.transform.modelviewMatrix = modelViewMatrix;
   }
4

2 回答 2

2

因为你告诉它在 x 和 y 中旋转 :)

试试这个 :

modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, startPoint.x, 1.0f, 0.0f, 0.0f);

这将围绕 x 轴旋转 startPoint.x 弧度。

您可以通过更改最后 3 个参数来围绕您想要的任何轴旋转(即 0,1,0 将围绕 y 轴旋转,1,1,0 将围绕 x 和 y 之间的 45° 轴旋转。)

注意感谢@Marcelo Cantos 的澄清:)

于 2012-01-05T14:48:30.610 回答
1

根据 deanWombourne 的说法,您使用GLKMatrix4Rotate不正确。当你执行:

GLKMatrix4Rotate(modelViewMatrix, -1, startPoint.x, startPoint.y, 0.0f);

您围绕轴旋转 -1 弧度(startPoint.x、startPoint.y、0.0f)。这听起来更像是您想围绕 (1, 0, 0) 旋转 startPoint.x 弧度,并围绕 (0, 1, 0) 旋转 startPoint.y 弧度。因此,例如:

modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, startPoint.x, 1.0f, 0.0f 0.0f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, startPoint.y, 0.0f, 1.0f 0.0f);

或者您可能想要划分 startPoint.x 和 startPoint.y,因为这将对触摸产生超强响应。

它也会有一些万向节锁定问题——本质上是因为如果你先绕 x 旋转,那么 y 轴不一定是你认为的位置,如果你先绕 y 旋转,那么 x 轴不一定是你认为的位置它是。这是你关心的事情吗?

于 2012-01-05T17:20:28.220 回答