我已经在OpenGL中编写了一个3D环境,您可以向前/向后移动并环顾四周(向上/向下/向左/向右)但是我无法确定如何向右侧移动。
我正在使用gluLookAt,我的前进代码是
GLfloat v[] = {[self centerAtIndex:0] - [self eyeAtIndex:0],[self centerAtIndex:1] - [self eyeAtIndex:1], [self centerAtIndex:2] - [self eyeAtIndex:2]};
[self setEye:[self eyeAtIndex:0] + v[0] * SPEED_MOVE atIndex:0];
[self setEye:[self eyeAtIndex:2] + v[2] * SPEED_MOVE atIndex:2];
[self setCenter: [self centerAtIndex:0] + v[0] * SPEED_MOVE atIndex:0];
[self setCenter: [self centerAtIndex:2] + v[2] * SPEED_MOVE atIndex:2];
和
gluLookAt(eye[0], eye[1], eye[2],center[0], center[1], center[2], 0.0, 1, 0.0);
任何人都知道如何采取行动或看到一个例子?
解决方案
float up[3] = {0, 1, 0};
float forward[3] = { center[0] - eye[0],center[1] - eye[1],center[2] - eye[2] };
float left[3];
left[0] = forward[1] * up[2] - forward[2] * up[1];
left[1] = forward[2] * up[0] - forward[0] * up[2];
left[2] = forward[0] * up[1] - forward[1] * up[0];
// now translate your eye position
[self setEye:[self eyeAtIndex:0] - left[0] * SPEED_MOVE atIndex:0];
[self setEye:[self eyeAtIndex:2] - left[2] * SPEED_MOVE atIndex:2];
[self setCenter: [self centerAtIndex:0] - left[0] * SPEED_MOVE atIndex:0];
[self setCenter: [self centerAtIndex:2] - left[2] * SPEED_MOVE atIndex:2];
if (([self eyeAtIndex:2] >= MapSizeZ || [self eyeAtIndex:0] >= MapSizeX || [self eyeAtIndex:2] <= 1 || [self eyeAtIndex:0] <= 1) || [self checkCollisionWithPoint:CGPointMake([self eyeAtIndex:0] ,[self eyeAtIndex:2])] ){
[self setEye:[self eyeAtIndex:0] + left[0] * SPEED_MOVE atIndex:0];
[self setEye:[self eyeAtIndex:2] + left[2] * SPEED_MOVE atIndex:2];
[self setCenter: [self centerAtIndex:0] + left[0] * SPEED_MOVE atIndex:0];
[self setCenter: [self centerAtIndex:2] + left[2] * SPEED_MOVE atIndex:2];
}
答案 0 :(得分:1)
要向侧面移动,您只需沿着相机的 右侧 向量平移眼睛位置。
要找到正确的向量,请记住两个向量的叉积给出垂直向量。因此,前向向量和向上向量的叉积将给出正确的向量。
你的前向矢量是v [3],但为了清晰起见,我将在这里使用'forward'。
// find the Right vector. (forward cross product up)
float right[3];
right[0] = forward[1] * up[2] - forward[2] * up[1];
right[1] = forward[2] * up[0] - forward[0] * up[2];
right[2] = forward[0] * up[1] - forward[1] * up[0];
// now translate your eye position
[self setEye:[self eyeAtIndex:0] + right[0] * SPEED_MOVE atIndex:0];
[self setEye:[self eyeAtIndex:2] + right[2] * SPEED_MOVE atIndex:2];
您还应该注意,Glu库不适用于OpenGL ES,因此您必须重新实现lookat函数,这很简单,但也是另一个问题。