OpenGL:gluLookAt:Sidestepping

时间:2011-07-20 08:03:07

标签: iphone opengl-es

在我的游戏中,你可以向前和向后移动,但我不知道如何向左或向右移动

GLfloat v[] = {[self centerAtIndex:0] - [self eyeAtIndex:0],[self centerAtIndex:1] - [self eyeAtIndex:1], [self centerAtIndex:2] - [self eyeAtIndex:2]}; 

//forward
[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);

任何人都可以给我实际代码来让相机横向移动。我试过切换v [2]和v [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];  
        }

1 个答案:

答案 0 :(得分:3)

通过计算向上矢量(此处为[0 1 0])和“外观”矢量(此处为center - eye)的叉积,可以得到一个横向指向的矢量。从那里你可以得到一个指向右侧或左侧的矢量,具体取决于顺序。然后只需将该矢量加/减到当前摄像机位置(眼睛,中心)即可进行扫描。

自己编码应该相当容易,cross product是一个简单的公式。互联网上有许多完全实现的摄像机类。