user1257724
user1257724

Reputation:

gluLookAt vectors and FPS-style camera

I am attempting to implemented an FPS-style camera by updating three vectors: EYE, DIR, UP. These vectors are the same that are used by gluLookAt (since gluLookAt is specified by the position of the camera, the direction it is looking at, and an up vector).

I have already implemented the left-right and up-down strafing movements, but I'm having a lot of trouble understanding the math behind making the camera look-around while remaining stationary. In this case, the EYE vector remains the same, while I must update DIR and UP.

Below is the code I tried, but it doesn't seem to work properly. Any suggestions?

void Transform::left(float degrees, vec3& dir, vec3& up) {
    vec3 axis;
    axis = glm::normalize(up);
    mat3 R = rotate(-degrees, axis);
    dir = R*dir;
    up = R*up;
};

void Transform::up(float degrees, vec3& dir, vec3& up) {
    vec3 axis;
    axis=glm::normalize(glm::cross(dir,up));
    mat3 R = rotate(-degrees, axis);
    dir = R*dir;
    up = R*up;
};

The rotate method creates a rotation matrix that rotates an an amount degrees around axis.

--

EDIT: I edited it to this (switching 'dir' to 'center', as well), but it still didn't work. When I try to rotate left/right, nothing happens. When I try to rotate up/down, the object disappears.

void Transform::left(float degrees, vec3& center, vec3& up) {
    center = center*rotate(-degrees,glm::normalize(up));
}
void Transform::up(float degrees, vec3& center, vec3& up) {
    vec3 axis = glm::normalize(glm::cross(center,up));
    center = center*rotate(-degrees, axis);
}

Upvotes: 1

Views: 1662

Answers (1)

Hossam El-Rewaidy
Hossam El-Rewaidy

Reputation: 16

the rotate() method is not defined, It seems that you forgot to implement it. And it seems to be rotation about an arbitrary axis, you can implement it from the equation http://upload.wikimedia.org/math/f/b/a/fbaee547c3c65ad3d48112502363378a.png
(Rotation matrix from axis and angle) form Wiki http://en.wikipedia.org/wiki/Rotation_matrix

Code:

mat3 Transform::rotate(const float degrees, const vec3& axis) { 
glm::mat3 m3;
glm::mat3 I(1.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,1.0f);
glm::mat3 T(axis.x*axis.x, axis.x*axis.y, axis.x*axis.z,axis.y*axis.x, axis.y*axis.y, axis.y*axis.z,axis.z*axis.x, axis.z*axis.y, axis.z*axis.z);
glm::mat3 A(0.0f,-axis.z, axis.y, axis.z, 0.0f, -axis.x,-axis.y,axis.x,0.0f);
m3 = glm::cos(degrees)*I + (1.0f-glm::cos(degrees))*T + glm::sin(degrees)*A;  return m3;}

Upvotes: 0

Related Questions