Burf2000
Burf2000

Reputation: 5193

iPhone : Moving in 3D Space (stepping sideways using gluLookAt)

I have written a 3D environment in OpenGL where you can move forward/backwards and look around (up/down left/right) However I cant work out how you can side step to the left of right.

I am using gluLookAt and my moving forward code is

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

and

gluLookAt(eye[0], eye[1], eye[2],center[0],  center[1], center[2], 0.0, 1, 0.0);

Anyone got any idea how to side step or seen an example?

Solution

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

Upvotes: 2

Views: 471

Answers (1)

Kitt Basch
Kitt Basch

Reputation: 125

To move sideways you simply have to translate the eye position along the camera's right vector.

To find the right vector, remember that the cross product of two vectors gives a perpendicular vector. So, the cross product of your forward vector and your up vector will give the right vector.

Your forward vector is v[3], but I will use 'forward' here for clarity.

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

You should also note that the glu library is not available for OpenGL ES, so you will have to reimplement the lookat function, which is straightforward, but also another question.

Upvotes: 1

Related Questions