简体   繁体   English

跟随相机XNA 4.0 C#

[英]Follow camera XNA 4.0 C#

How to change target camera into follow? 如何将目标相机变成跟随相机?

Target: 目标:

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

}

I do have a clue to use this line of code with some modification plus add to it the length how far camera should be behind target. 我确实有线索使用此代码行进行了一些修改,并增加了相机应位于目标后面多远的长度。 This line of code makes my ship ove arround the screen now i need to make sure the camera moves with the ship. 这行代码使我的飞船绕过屏幕,现在我需要确保摄像头随飞船移动。 Help please 请帮助

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

You have to attach the camera to your ship, calculate the camera position and target with its position 您必须将摄像头安装到船上,计算摄像头位置并以其位置为目标

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

If you consider the forward vector of the ship and interpolate this values with a easing function, the movement can be very cool and smooth. 如果考虑船舶的前向矢量,并使用缓动函数对该值进行插值,则运动可能会非常凉爽且平滑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM