平移相机,使物体位于屏幕边缘

时间:2014-11-04 18:54:57

标签: math 3d

我试图编写一个公式来充分平移相机,以便在屏幕边缘只能看到对象的中心点。 因此,换句话说,如果对象偏离右侧,我会更改摄像机的x和y位置,以便对象位于屏幕的右边缘(不改变摄像机角度或z纵坐标)。 谁能给我任何提示怎么做?

1 个答案:

答案 0 :(得分:0)

我找到了一个符合我目的的解决方案,但我能做到的唯一方法就是修改相机高度(这不是我想要的):

// note: pos is the center of the object that is to appear at edge of screen
// m_position is the 3d position of the camera
// m_plane[] is array of left, right, etc. planes of camera fustrum

void Camera::PanTo(const Vector3D& pos) 
{
    int n;
    Vector3D vectorNearest;
    for (n = 0; n < NUM_PLANES; n++)
    {
        if (m_plane[n].GetDistance(pos) < 0)
        {
            m_plane[n].Normalize();
            vectorNearest += m_plane[n].GetVectorNearest(pos);
        }
    }
    m_posDesire.m[IX_X] = m_position.m[IX_X] + vectorNearest.m[IX_X];
    m_posDesire.m[IX_Y] = m_position.m[IX_Y] + vectorNearest.m[IX_Y];
    m_posDesire.m[IX_Z] = m_position.m[IX_Z] + vectorNearest.m[IX_Z];
}



// This is the definition of the Plane class:
class Plane  
{
public:
    void Normalize()
    {
        float lenInv = 1.0f/sqrtf(m_a*m_a + m_b*m_b + m_c*m_c);
        m_a *= lenInv;
        m_b *= lenInv;
        m_c *= lenInv;
        m_d *= lenInv;
    }
    float GetDistance(const Vector3D& pos) const
    {
        return m_a*pos.m[IX_X] + m_b*pos.m[IX_Y] + 
            m_c*pos.m[IX_Z] + m_d;
    }
    Vector3D GetVectorNearest(const Vector3D& pos) const
    {
        Vector3D normal(m_a, m_b, m_c);
        float posDotNormal = pos.dotProduct(normal);
        Vector3D nearest = normal*(m_d+posDotNormal);
        return nearest;
    }
    float m_a, m_b, m_c, m_d;
};
相关问题