Skip to content

A mistake about orbit camera #11

@smile-zyk

Description

@smile-zyk

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions