首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >平移相机,使对象位于屏幕边缘

平移相机,使对象位于屏幕边缘
EN

Stack Overflow用户
提问于 2014-11-05 02:54:57
回答 1查看 46关注 0票数 0

我正在尝试写一个公式来足够平移相机,使对象的中心点仅在屏幕边缘可见。因此,换句话说,如果物体不在视线的右边,我会改变相机的x和y位置,这样物体就在屏幕的右边(不改变相机角度或z坐标)。有没有人能给我一些提示怎么做?

EN

回答 1

Stack Overflow用户

发布于 2014-11-13 00:21:16

我想出了一个符合我的目的的解决方案,但我唯一能做到的方法是修改摄像头的高度(这并不完全是我想要的):

代码语言:javascript
复制
// 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;
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26742841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档