0

I'm trying to implement a camera that follows a moving object. I've implemented these functions:

void Camera::espheric_yaw(float degrees, glm::vec3 center_point)
{

    float lim_yaw = glm::radians(89.0f);
    float radians = glm::radians(degrees);
    absoluteYaw += radians;

    ... clamp absoluteYaw

    float radius = 10.0f;
    float camX = cos(absoluteYaw) * cos(absoluteRoll) * radius;
    float camY = sin(absoluteRoll)* radius;
    float camZ = sin(absoluteYaw) * cos(absoluteRoll) * radius;
    eyes.x = camX;
    eyes.y = camY;
    eyes.z = camZ;
    lookAt = center_point;
    view = glm::normalize(lookAt - eyes);
    up = glm::vec3(0, 1, 0);
    right = glm::normalize(glm::cross(view, up));

}

I want to use this function (and the pitch version) for a camera that follows a moving 3d model. Right now, it works when the center_point is the (0,1,0). I think i'm getting the position right but the up vector is clearly not always (0,1,0).

How can I get my up, view and right vector for the camera? And then, if I update the eyes position of the camera this way, how will my camera move when the other object (centered at center_position parameter) moves?

The idea is to update this each time I have mouse input with centered_value = center of the moving object. Then use gluLookAt with view, eyes and up values of my camera (and lookAt which will be eyes+view).

genpfault
  • 51,148
  • 11
  • 85
  • 139
ero.rom
  • 43
  • 5

1 Answers1

0

Following a moving object is matter of pointing the camera to that object. This is what typical lookAt function does. See the maths here and then use glm::lookAt().

The 'Arcball' technic is for rotating with the mouse. See some maths here.

The idea is to get two vectors (first, second) from positions on screen. For each vector, X,Y are taking depending on pixels "travelled" by mouse and the size of the window. Z is calculated by 'trackball' maths. With these two vectors (after normalizing them), its cross product gives the axis of rotation in camera coordinates, and its dot product gives the angle. Now, you can rotate the camera by glm::rotate()

If you go another route (e.g. calculating camera matrix on your own), then the "up" direction of the camera must be updated by yourself. Remember it's perpendicular to the other two axis of the camera.

Ripi2
  • 7,031
  • 1
  • 17
  • 33