Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A mistake about orbit camera #11

Open
smile-zyk opened this issue Apr 14, 2022 · 1 comment
Open

A mistake about orbit camera #11

smile-zyk opened this issue Apr 14, 2022 · 1 comment

Comments

@smile-zyk
Copy link

function calculate_pan in camera.c:

static vec3_t calculate_pan(vec3_t from_camera, motion_t motion) {
    vec3_t forward = vec3_normalize(from_camera);
    vec3_t left = vec3_cross(UP, forward);
    vec3_t up = vec3_cross(forward, left);

    float distance = vec3_length(from_camera);
    float factor = distance * (float)tan(FOVY / 2) * 2;
    vec3_t delta_x = vec3_mul(left, motion.pan.x * factor);
    vec3_t delta_y = vec3_mul(up, motion.pan.y * factor);
    return vec3_add(delta_x, delta_y);
}

vector left and vector up are not normalize.
This error causes the camera's motion speed to shrink as the Angle between the forward vector and the UP vector changes.
Specifically, the less close the angle between the forward vector and the UP vector is to 90 degrees, the slower the camera moves and will not move at all in extreme cases (when forward is on the same line as UP).
The correct code should look like this:

static vec3_t calculate_pan(vec3_t from_camera, motion_t motion) {
    vec3_t forward = vec3_normalize(from_camera);
    vec3_t left = vec3_cross(UP, forward);
    left = vec3_normalize(left);
    vec3_t up = vec3_cross(forward, left);
    up = vec3_normalize(up);

    float distance = vec3_length(from_camera);
    float factor = distance * (float)tan(FOVY / 2) * 2;
    vec3_t delta_x = vec3_mul(left, motion.pan.x * factor);
    vec3_t delta_y = vec3_mul(up, motion.pan.y * factor);
    return vec3_add(delta_x, delta_y);
}
@zauonlok
Copy link
Owner

You are right. I will fix this issue later.

@zauonlok zauonlok reopened this Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants