1
votes

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);
}
1
I think updating the DIR vector should be sufficient. The UP vector can be (0,1,0), it is automatically resolved in a direction perpendicular to the vector between the EYE and DIR.. still, correct me if I'm wrong.max
So you're saying I should keep the code as is, but remove the updates to the up vector? I'll go ahead and try it and let you know what happens. Thanks, Max.user1257724
@Max Thanks for the suggestion, but it didn't work. I updated it to this (I switched dir with center): view edit above.user1257724
Does the degrees parameter specify how much you want to turn relative to where you are currently?Vaughn Cato
Here, I have links explaining what I meant - you don't need to adjust the UP vector. stackoverflow.com/questions/3427379/… Also, google it and read the gamedev.net results. Hope its clear to you.max

1 Answers

0
votes

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