2011-07-16 83 views
2

我已經在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]; 
     } 
+0

我發現用v [2]切換v [0],反之亦然做竅門 – Burf2000

+0

我發現這不起作用 – Burf2000

回答

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]; 

你還應該注意的是,穀氨酸庫不提供OpenGL ES的,所以你將不得不重新實現LOOKAT功能,這很簡單,但也是另一個問題。

+0

你有任何代碼示例嗎?將是一個拯救生命的人! – Burf2000

+0

瞭解交叉產品是三維數學的重要組成部分。如果你知道它,它應該是非常直接的實施它。 –

+0

可悲的是,我還沒有,任何好的參考?如果這個作品,我會很高興:) – Burf2000