2011-12-15 88 views
0

我需要第三人称摄像机的非语言特定算法。如何将相机保持在物体的后面并且保持相机的旋转与物体旋转相同,我即将遇到麻烦。有任何想法吗?三角学3d数学第三人称摄像机算法

例子:

UpdateCamera(camera, target){ 
    offset = vector(0,height,distance); 
    cameraPos = targetPos+offset; 
    camAngle = ?????; 
} 

我知道这不会是简单的但是我相信你会得到它的要点。

+0

第三人称相机在StackOverflow答案中完全解释相当复杂 - 您可能想要获得名为“3D游戏:实时渲染和软件技术”和“3D游戏:动画和高级实时渲染“,但我记得其中一个包含如何做到这一点的体面描述。 – 2011-12-15 00:53:54

+0

请参阅:http://www.amazon.co.uk/Games-Real-Time-Rendering-Technology-Real-time/dp/0201619210和http://www.amazon.co.uk/3D-Games-动画 - 实时渲染/ dp/0201787067 – 2011-12-15 00:55:26

回答

0

其绝对简单的,假设你有一个简单的NUV相机类是这样的:现在

/// <summary> 
/// An instance of this class represents a camera for a 3D view. 
/// Cameras are defined with a position and three mutually-orthogonal 
/// axes, namely N (points in the direction faced by the camera), 
/// U (points to the left of the camera) and V (points to the top 
/// of the camera). 
/// </summary> 
sealed class Camera 
{ 
    //#################### PRIVATE VARIABLES #################### 
    #region 

    private Vector3 m_n; 
    private Vector3 m_position; 
    private Vector3 m_u; 
    private Vector3 m_v; 

    #endregion 

    //#################### PROPERTIES #################### 
    #region 

    /// <summary> 
    /// The position of the camera. 
    /// </summary> 
    public Vector3 Position { get { return m_position; } } 

    /// <summary> 
    /// A vector pointing in the direction faced by the camera. 
    /// </summary> 
    public Vector3 N { get { return m_n; } } 

    /// <summary> 
    /// A vector pointing to the left of the camera. 
    /// </summary> 
    public Vector3 U { get { return m_u; } } 

    /// <summary> 
    /// A vector pointing to the top of the camera. 
    /// </summary> 
    public Vector3 V { get { return m_v; } } 

    #endregion 

    //#################### CONSTRUCTORS #################### 
    #region 

    /// <summary> 
    /// Constructs a new camera. 
    /// </summary> 
    /// <param name="position">The position of the camera.</param> 
    /// <param name="look">A vector pointing in the direction faced by the camera.</param> 
    /// <param name="up">The "up" direction for the camera.</param> 
    public Camera(Vector3 position, Vector3 look, Vector3 up) 
    { 
     m_position = position; 

     m_n = look; 
     m_n.Normalize(); 

     m_v = up; 
     m_v.Normalize(); 

     m_u = Vector3.Cross(m_v, m_n); 
     m_u.Normalize(); 
    } 

    #endregion 

    //#################### PUBLIC METHODS #################### 
    #region 

    /// <summary> 
    /// Moves the camera by the specified displacement in the n direction. 
    /// </summary> 
    /// <param name="delta">The displacement by which to move.</param> 
    public void MoveN(float delta) 
    { 
     m_position += delta * m_n; 
    } 

    /// <summary> 
    /// Moves the camera by the specified displacement in the u direction. 
    /// </summary> 
    /// <param name="delta">The displacement by which to move.</param> 
    public void MoveU(float delta) 
    { 
     m_position += delta * m_u; 
    } 

    /// <summary> 
    /// Moves the camera by the specified displacement in the v direction. 
    /// </summary> 
    /// <param name="delta">The displacement by which to move.</param> 
    public void MoveV(float delta) 
    { 
     m_position += delta * m_v; 
    } 

    /// <summary> 
    /// Rotates the camera anticlockwise by the specified angle about the specified axis. 
    /// </summary> 
    /// <param name="axis">The axis about which to rotate.</param> 
    /// <param name="angle">The angle by which to rotate (in radians).</param> 
    public void Rotate(Vector3 axis, float angle) 
    { 
     // Note: We try and optimise things a little by observing that there's no point rotating an axis about itself. 
     if(axis != m_n) m_n = MathUtil.RotateAboutAxis(m_n, angle, axis); 
     if(axis != m_u) m_u = MathUtil.RotateAboutAxis(m_u, angle, axis); 
     if(axis != m_v) m_v = MathUtil.RotateAboutAxis(m_v, angle, axis); 
    } 

    #endregion 
} 

,每一帧:

1)获取的位置pp和玩家的方向pdir
2)重新创建您的相机,位置= pp - offset * pdir,外观= pdir或更高=合理的(例如(0,0,1))。