2014-11-04 63 views
0

我想寫一個公式來平移相機足夠的,以便對象的中心點只是在屏幕邊緣可見。 因此,換句話說,如果物體不在右邊,我會改變相機的x和y位置,以便物體恰好在屏幕的右邊緣(不改變相機角度或z co -ordinate)。 任何人都可以給我任何提示如何做到這一點?平移相機所以對象是在屏幕邊緣

回答

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