0

如何将目标相机更改为跟随?

目标:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Flight
{
    public class FollowCamera : CCamera
    {

        public Vector3 Target { get; set; }

        public FollowCamera(Vector3 Position, Vector3 Target,
                    GraphicsDevice graphicsDevice)
            : base(graphicsDevice)
        {
            this.Position = Position;
            this.Target = Target;
        }

        public override void Update()
        {
            //Missing lines of code used to determine 
            //the up vector
            Vector3 forward = Target - Position;
            Vector3 right = Vector3.Cross(forward, Vector3.Up);
            Vector3 up = Vector3.Cross(right, forward);

            this.View = Matrix.CreateLookAt(Position,
                    Target, up);
        }
    }

}

我确实有线索可以使用这行代码进行一些修改,并添加相机应该在目标后面多远的长度。这行代码让我的船绕着屏幕飞过去,现在我需要确保相机随船一起移动。请帮忙

 this.local = Matrix.CreateFromYawPitchRoll(orientation.Y, orientation.X, orientation.Z) * Matrix.CreateTranslation(position.X, position.Y, position.Z) * this.local;
4

1 回答 1

0

您必须将相机连接到您的船上,计算相机位置并使用其位置进行目标

interface IPositionable { Vector3 Position }


class FollowCamera{

    public IPositionable ObjectToFollow;

    public FollowCamera(Vector3 RelativePosition)
    {
        this.RelativePosition = relativePosition
    }

    public override void Update()
    {
        //Missing lines of code used to determine 
        //the up vector
        Vector3 forward = Target - Position;
        Vector3 right = Vector3.Cross(forward, Vector3.Up);
        Vector3 up = Vector3.Cross(right, forward);

        Position = ObjectToFollow.Position - RelativePosition;
        Target = ObjectToFollow.Position;

        this.View = Matrix.CreateLookAt(Position,
                Target, up);
    }
}

如果您考虑船的前向矢量并使用缓动函数插入此值,则运动可以非常酷且平滑。

于 2012-11-21T16:12:15.667 回答