【发布时间】:2012-01-23 08:38:15
【问题描述】:
我正在尝试使用触摸在 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;
}
【问题讨论】:
-
不确定如何在 GLK 中执行,但您是否在旋转之前将其转换为原点(然后再返回)?
-
不,我不认为我正在这样做。物体并没有从屏幕上移开,它在旋转,只是不是我想要的。我应该这样做吗?
-
在 OpenGL 中,您首先将对象平移(=滑动)到原点 (0,0,0),然后旋转它,然后将其平移回它的假定位置。 OpenGL 总是围绕原点旋转,如果你不将它滑到那里,你的对象会“绕”原点“旋转”,但不会像预期的那样围绕自身旋转。
-
注意:如果你只想绕 Y 轴旋转,将其转换为 Y = 0 等就足够了。希望我能帮助你。
-
我实际上已经将它翻译成原点。 GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -3.5f);
标签: ios opengl-es touch rotation glkit