我已經在OpenGL中編寫了一個3D環境,您可以向前/向後移動並環視(上/下左/右)然而,我無法解決您可以如何一步到右邊。iPhone:在3D空間中移動(使用gluLookAt橫向移動)
我使用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];
}
我發現用v [2]切換v [0],反之亦然做竅門 – Burf2000
我發現這不起作用 – Burf2000