我正在尝试使用触摸在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;
}
答案 0 :(得分: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°轴旋转。 )
NB感谢@Marcelo Cantos澄清:)
答案 1 :(得分: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轴不一定你认为它在哪里。那是你关心的事吗?