Java LWJGL仅在俯仰和偏航时向前移动

Java LWJGL仅在俯仰和偏航时向前移动,java,math,opengl,vector,lwjgl,Java,Math,Opengl,Vector,Lwjgl,所以基本上在我的游戏中,我设法添加了向右移动,然后应用了相反的逻辑向左移动,但我仍然无法向前或向后移动。 我只有一个俯仰和偏航的工作, 意思是我没有旋转X,Y或Z。 如果你能帮我计算这些,如果必要的话也会很酷。 这是我目前的代码: 公共级摄像机{ 专用最终浮动最大螺距=90.0F; 私有向量3f位置=新向量3f(0,0,0); 私人浮球; 私人浮球; 私人浮动偏航; 私人浮动汇率; 私人浮动辊; 专用浮球前进速度=7.0f; 私人浮动后退速度=10.0f; 专用浮动侧速度=反向速度; 专用浮动跳

所以基本上在我的游戏中,我设法添加了向右移动,然后应用了相反的逻辑向左移动,但我仍然无法向前或向后移动。 我只有一个俯仰和偏航的工作, 意思是我没有旋转X,Y或Z。 如果你能帮我计算这些,如果必要的话也会很酷。 这是我目前的代码:

公共级摄像机{
专用最终浮动最大螺距=90.0F;
私有向量3f位置=新向量3f(0,0,0);
私人浮球;
私人浮球;
私人浮动偏航;
私人浮动汇率;
私人浮动辊;
专用浮球前进速度=7.0f;
私人浮动后退速度=10.0f;
专用浮动侧速度=反向速度;
专用浮动跳线速度=8.0f;
私人浮动下降速度=7.0f;
私人浮动灵敏度=100;
公共摄像机(){
}
公共无效更新(){
final double deltaTime=Main.getGame().deltaTime;
最终双倍转发量=转发速度*延迟时间;
最终双回退量=回退速度*延迟时间;
最终双边金额=反向金额;
浮动节距=(浮动)数学toRadians(此节距);
浮动偏航=(浮动)数学toRadians(这是偏航);
浮点数xzLength=(浮点数)(-0.448073612);
浮点dx=(浮点)(xzLength*Math.cos(偏航));
float dz=(float)(xzLength*Math.sin(偏航));
if(KeyUtils.isForwardDown()){
}else if(KeyUtils.isbackardsdown()){
}
if(KeyUtils.isLeftDown()){
位置x+=dx*侧量;
位置z+=dz*侧量;
}else if(KeyUtils.isRightDown()){
位置x-=dx*侧量;
位置z-=dz*侧量;
}
if(KeyUtils.isJumping()){
位置y+=跳跃速度*增量时间;
}else if(KeyUtils.isSneaking()){
位置y-=下降速度*增量时间;
}
浮动旋转速度=(浮动)(50F*deltaTime*灵敏度);
this.yaw+=Mouse.getDX()/旋转速度;
如果(节距>最大节距){
螺距=90.0F;
返回;
}
this.pitch-=Mouse.getDY()/旋转速度;
}
公共向量3f getPosition(){
返回位置;
}
公共无效设置位置(矢量3F位置){
这个位置=位置;
}
公开募股{
回音节;
}
公共间距(浮动间距){
这个。螺距=螺距;
}
公共浮点数(){
返回偏航;
}
公共无效设置偏航(浮动偏航){
this.yaw=偏航;
}
公共浮动getRoll(){
回程辊;
}
公共无效设置辊(浮动辊){
this.roll=roll;
}
公共浮点数getLastPitch(){
回音音高;
}
公共浮点数getLastYaw(){
回程偏航;
}
}
我还有一个创建视图矩阵的方法,叫做每帧,就像camera.update()方法一样

公共静态矩阵x4f createViewMatrix(摄像头){
Matrix4f viewMatrix=新Matrix4f();
viewMatrix.setIdentity();
Matrix4f.rotate((float)Math.toRadians(camera.getPitch()),新矢量3f(1,0,0),viewMatrix,viewMatrix);
Matrix4f.rotate((float)Math.toRadians(camera.getYaw())、新矢量3f(0,1,0)、viewMatrix、viewMatrix);
Vector3f cameraPos=camera.getPosition();
Vector3f negativeCameraPos=新矢量3f(-cameraPos.x,-cameraPos.y,-cameraPos.z);
矩阵4f.翻译(negativeCameraPos、viewMatrix、viewMatrix);
返回视图矩阵;
}

我有几个报价给你。这是一段youtube视频,显示了基本的摄像头控制

下面是我游戏中使用的一些当前代码:

using SharpDX;
using System;

namespace VoidwalkerEngine.Framework.DirectX
{
    public enum CameraMode
    {
        FreeLook,
        Orbit
    }

    public class Camera
    {
        /// <summary>
        /// The name of this camera
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// The eye location of this camera
        /// </summary>
        public Vector3 Location { get; set; }
        /// <summary>
        /// The Pitch of this Camera, as Radians
        /// </summary>
        public float Pitch { get; set; }
        /// <summary>
        /// The Yaw of this Camera, as Radians
        /// </summary>
        public float Yaw { get; set; }
        /// <summary>
        /// The Roll of this Camera, as Radians
        /// </summary>
        public float Roll { get; set; }
        /// <summary>
        /// The NearZ of this Camera
        /// </summary>
        public float NearZ { get; set; }
        /// <summary>
        /// The FarZ of this Camera
        /// </summary>
        public float FarZ { get; set; }
        /// <summary>
        /// The Field of View of this Camera, value should be
        /// between 0.70 and 1.20
        /// </summary>
        public float FieldOfView { get; set; }
        public float AspectRatio { get; set; }
        public float LookSpeed { get; set; }
        public float MoveSpeed { get; set; }
        /// <summary>
        /// Determines if this Camera is currently accelerating.
        /// </summary>
        public bool IsAccelerating { get; set; }
        /// <summary>
        /// The acceleration speed multiplier of this Camera.
        /// </summary>
        public float AccelerationMultiplier { get; set; }
        public CameraMode Mode { get; set; }
        public float ViewportWidth;
        public float ViewportHeight;

        /// <summary>
        /// The BoundingSphere of this Camera
        /// </summary>
        public BoundingSphere Bounds
        {
            get
            {
                return new BoundingSphere()
                {
                    Center = this.Location,
                    Radius = 2.0f
                };
            }
        }

        /// <summary>
        /// The Target Vector of this Camera
        /// </summary>
        public Vector3 Target
        {
            get
            {
                return new Vector3(
                    (float)Math.Sin(this.Yaw),
                    (float)Math.Tan(this.Pitch),
                    (float)Math.Cos(this.Yaw));
            }
        }

        /// <summary>
        /// The Frustum of this Camera
        /// </summary>
        public BoundingFrustum Frustum
        {
            get
            {
                return new BoundingFrustum(this.ModelViewProjectionMatrix);
            }
        }

        public Matrix ModelViewMatrix
        {
            get
            {
                return Matrix.LookAtLH(this.Location, Location + Target, Up);
            }
        }

        public Matrix ProjectionMatrix
        {
            get
            {
                return Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, NearZ, FarZ);
            }
        }

        public Matrix ModelViewProjectionMatrix
        {
            get
            {
                return ModelViewMatrix * ProjectionMatrix;
            }
        }

        //public CardinalDirectionType Direction
        //{
        //    get
        //    {
        //        return VoidwalkerMath.GetCardinalDirection(VoidwalkerMath.ToDegrees(Yaw));
        //    }
        //}

        public Vector3 Forward
        {
            get
            {
                return new Vector3((float)Math.Cos(Pitch), 0, (float)Math.Sin(Pitch));
            }
        }

        public Vector3 Right
        {
            get
            {
                return new Vector3(Forward.X, 0, -Forward.X);
            }
        }

        public Vector3 Up
        {
            get
            {
                return new Vector3(-(float)Math.Sin(Roll), (float)Math.Cos(Roll), 0);
            }
        }

        public Camera()
        {

        }

        public Camera(string name)
            : this()
        {
            this.Name = name;
            this.Location = new Vector3();
        }

        public void ToOrigin()
        {
            Transform(Vector3.Zero, 0, 0, 0);
        }

        public void Transform(Vector3 location, float pitch, float yaw, float roll)
        {
            this.Location = location;
            this.Pitch = pitch;
            this.Yaw = yaw;
            this.Roll = roll;
        }

        public float GetCurrentMoveSpeed()
        {
            if (IsAccelerating)
            {
                return this.MoveSpeed * this.AccelerationMultiplier;
            }
            return this.MoveSpeed;
        }

        public void TranslateLeft(float deltaTime)
        {
            float moveSpeed = GetCurrentMoveSpeed();
            this.Location = new Vector3(
                Location.X - (float)Math.Sin(Yaw + MathUtil.PiOverTwo) * moveSpeed * deltaTime,
                Location.Y,
                Location.Z - (float)Math.Cos(Yaw + MathUtil.PiOverTwo) * moveSpeed * deltaTime);
        }

        public void TranslateRight(float deltaTime)
        {
            float moveSpeed = GetCurrentMoveSpeed();
            this.Location = new Vector3(
                Location.X + (float)Math.Sin(Yaw + MathUtil.PiOverTwo) * moveSpeed * deltaTime,
                Location.Y,
                Location.Z + (float)Math.Cos(Yaw + MathUtil.PiOverTwo) * moveSpeed * deltaTime);
        }

        public void TranslateForward(float deltaTime)
        {
            float degreesX = MathUtil.RadiansToDegrees(Pitch) * 0.01745329F; // X rotation
            float degreesY = MathUtil.RadiansToDegrees(Yaw) * 0.01745329F; // Y rotation
            float moveSpeed = GetCurrentMoveSpeed();
            this.Location = new Vector3(
                this.Location.X + (float)(moveSpeed * Math.Sin(degreesY) * Math.Cos(degreesX)) * deltaTime,
                this.Location.Y + (float)(moveSpeed * Math.Sin(degreesX)) * deltaTime,
                this.Location.Z + (float)(moveSpeed * Math.Cos(degreesY) * Math.Cos(degreesX)) * deltaTime);
        }

        public void TranslateBackward(float deltaTime)
        {

            float degreesX = MathUtil.RadiansToDegrees(Pitch) * 0.01745329F; // X rotation
            float degreesY = MathUtil.RadiansToDegrees(Yaw) * 0.01745329F; // Y rotation
            float moveSpeed = GetCurrentMoveSpeed();
            this.Location = new Vector3(
                this.Location.X - (float)(moveSpeed * Math.Sin(degreesY) * Math.Cos(degreesX)) * deltaTime,
                this.Location.Y - (float)(moveSpeed * Math.Sin(degreesX)) * deltaTime,
                this.Location.Z - (float)(moveSpeed * Math.Cos(degreesY) * Math.Cos(degreesX)) * deltaTime);
        }

        public void TransformYawPitch(float dx, float dy)
        {
            Yaw += dx * LookSpeed;
            Pitch -= dy * LookSpeed;
            const float pitchClamp = 1.56f;
            if (Pitch <= -pitchClamp)
            {
                Pitch = -pitchClamp;
            }
            if (Pitch >= pitchClamp)
            {
                Pitch = pitchClamp;
            }
        }

        public void TranslateUp(float deltaTime)
        {
            this.Location = new Vector3(
                this.Location.X,
                this.Location.Y + GetCurrentMoveSpeed() * deltaTime,
                this.Location.Z); // TODO implement up/down based upon roll orientation.
        }

        public void TranslateDown(float deltaTime)
        {
            this.Location = new Vector3(
                this.Location.X,
                this.Location.Y - GetCurrentMoveSpeed() * deltaTime,
                this.Location.Z);
        }

        public void LookAt(Vector3 location, float pitch, float yaw, float roll)
        {
            this.Location = location;
            this.Pitch = pitch;
            this.Yaw = yaw;
            this.Roll = roll;
        }

        public void SetAspectRatio(int width, int height)
        {
            this.ViewportWidth = width;
            this.ViewportHeight = height;
            this.AspectRatio = width / (float)height;
        }
    }
}
实际上,这是我的鼠标输入代码。你在网上找不到任何类似的东西,因为我实际上是在查询原始的鼠标输入。大多数人不会这样做,这就是为什么大多数例子会导致鼠标按下时“相机捕捉”非常糟糕的原因

private void OnMouseMove(object sender, MouseEventArgs args)
        {
            this.CurrentMouseLocation = new Point(args.X, args.Y);
            if (IsMouseLookEnabled)
            {
                MouseUpdate[] updates = Mouse.GetBufferedData();
                if (updates != null && updates.Length > 0)
                {
                    int xAccumulation = 0;
                    int yAccumulation = 0;
                    if (updates != null && updates.Length > 0)
                    {
                        for (int i = 0; i < updates.Length; i++)
                        {
                            if (updates[i].IsButton)
                            {
                                // Reject Buffered Data
                                return;
                            }
                            if (updates[i].Offset == MouseOffset.X)
                            {
                                xAccumulation += updates[i].Value;
                            }
                            if (updates[i].Offset == MouseOffset.Y)
                            {
                                yAccumulation += updates[i].Value;
                            }
                        }
                        float dx = MathUtil.DegreesToRadians(xAccumulation) * 0.25F;
                        float dy = MathUtil.DegreesToRadians(yAccumulation) * 0.25F;
                        Camera.TransformYawPitch(dx, dy);
                    }
                }
                Cursor.Position = new System.Drawing.Point(_mouseLockLocation.X, _mouseLockLocation.Y);
                this.CurrentMouseLocation = _mouseLockLocation;
            }
        }
mouseMove上的私有void(对象发送方,MouseEventArgs参数)
{
this.CurrentMouseLocation=新点(args.X,args.Y);
如果(IsMouseLookEnabled)
{
MouseUpdate[]updates=Mouse.GetBufferedData();
if(updates!=null&&updates.Length>0)
{
int xAccumulation=0;
积分累加=0;
if(updates!=null&&updates.Length>0)
{
for(int i=0;i
我有几个报价给你。这是一段youtube视频,显示了基本的摄像头控制

下面是我游戏中使用的一些当前代码:

using SharpDX;
using System;

namespace VoidwalkerEngine.Framework.DirectX
{
    public enum CameraMode
    {
        FreeLook,
        Orbit
    }

    public class Camera
    {
        /// <summary>
        /// The name of this camera
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// The eye location of this camera
        /// </summary>
        public Vector3 Location { get; set; }
        /// <summary>
        /// The Pitch of this Camera, as Radians
        /// </summary>
        public float Pitch { get; set; }
        /// <summary>
        /// The Yaw of this Camera, as Radians
        /// </summary>
        public float Yaw { get; set; }
        /// <summary>
        /// The Roll of this Camera, as Radians
        /// </summary>
        public float Roll { get; set; }
        /// <summary>
        /// The NearZ of this Camera
        /// </summary>
        public float NearZ { get; set; }
        /// <summary>
        /// The FarZ of this Camera
        /// </summary>
        public float FarZ { get; set; }
        /// <summary>
        /// The Field of View of this Camera, value should be
        /// between 0.70 and 1.20
        /// </summary>
        public float FieldOfView { get; set; }
        public float AspectRatio { get; set; }
        public float LookSpeed { get; set; }
        public float MoveSpeed { get; set; }
        /// <summary>
        /// Determines if this Camera is currently accelerating.
        /// </summary>
        public bool IsAccelerating { get; set; }
        /// <summary>
        /// The acceleration speed multiplier of this Camera.
        /// </summary>
        public float AccelerationMultiplier { get; set; }
        public CameraMode Mode { get; set; }
        public float ViewportWidth;
        public float ViewportHeight;

        /// <summary>
        /// The BoundingSphere of this Camera
        /// </summary>
        public BoundingSphere Bounds
        {
            get
            {
                return new BoundingSphere()
                {
                    Center = this.Location,
                    Radius = 2.0f
                };
            }
        }

        /// <summary>
        /// The Target Vector of this Camera
        /// </summary>
        public Vector3 Target
        {
            get
            {
                return new Vector3(
                    (float)Math.Sin(this.Yaw),
                    (float)Math.Tan(this.Pitch),
                    (float)Math.Cos(this.Yaw));
            }
        }

        /// <summary>
        /// The Frustum of this Camera
        /// </summary>
        public BoundingFrustum Frustum
        {
            get
            {
                return new BoundingFrustum(this.ModelViewProjectionMatrix);
            }
        }

        public Matrix ModelViewMatrix
        {
            get
            {
                return Matrix.LookAtLH(this.Location, Location + Target, Up);
            }
        }

        public Matrix ProjectionMatrix
        {
            get
            {
                return Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, NearZ, FarZ);
            }
        }

        public Matrix ModelViewProjectionMatrix
        {
            get
            {
                return ModelViewMatrix * ProjectionMatrix;
            }
        }

        //public CardinalDirectionType Direction
        //{
        //    get
        //    {
        //        return VoidwalkerMath.GetCardinalDirection(VoidwalkerMath.ToDegrees(Yaw));
        //    }
        //}

        public Vector3 Forward
        {
            get
            {
                return new Vector3((float)Math.Cos(Pitch), 0, (float)Math.Sin(Pitch));
            }
        }

        public Vector3 Right
        {
            get
            {
                return new Vector3(Forward.X, 0, -Forward.X);
            }
        }

        public Vector3 Up
        {
            get
            {
                return new Vector3(-(float)Math.Sin(Roll), (float)Math.Cos(Roll), 0);
            }
        }

        public Camera()
        {

        }

        public Camera(string name)
            : this()
        {
            this.Name = name;
            this.Location = new Vector3();
        }

        public void ToOrigin()
        {
            Transform(Vector3.Zero, 0, 0, 0);
        }

        public void Transform(Vector3 location, float pitch, float yaw, float roll)
        {
            this.Location = location;
            this.Pitch = pitch;
            this.Yaw = yaw;
            this.Roll = roll;
        }

        public float GetCurrentMoveSpeed()
        {
            if (IsAccelerating)
            {
                return this.MoveSpeed * this.AccelerationMultiplier;
            }
            return this.MoveSpeed;
        }

        public void TranslateLeft(float deltaTime)
        {
            float moveSpeed = GetCurrentMoveSpeed();
            this.Location = new Vector3(
                Location.X - (float)Math.Sin(Yaw + MathUtil.PiOverTwo) * moveSpeed * deltaTime,
                Location.Y,
                Location.Z - (float)Math.Cos(Yaw + MathUtil.PiOverTwo) * moveSpeed * deltaTime);
        }

        public void TranslateRight(float deltaTime)
        {
            float moveSpeed = GetCurrentMoveSpeed();
            this.Location = new Vector3(
                Location.X + (float)Math.Sin(Yaw + MathUtil.PiOverTwo) * moveSpeed * deltaTime,
                Location.Y,
                Location.Z + (float)Math.Cos(Yaw + MathUtil.PiOverTwo) * moveSpeed * deltaTime);
        }

        public void TranslateForward(float deltaTime)
        {
            float degreesX = MathUtil.RadiansToDegrees(Pitch) * 0.01745329F; // X rotation
            float degreesY = MathUtil.RadiansToDegrees(Yaw) * 0.01745329F; // Y rotation
            float moveSpeed = GetCurrentMoveSpeed();
            this.Location = new Vector3(
                this.Location.X + (float)(moveSpeed * Math.Sin(degreesY) * Math.Cos(degreesX)) * deltaTime,
                this.Location.Y + (float)(moveSpeed * Math.Sin(degreesX)) * deltaTime,
                this.Location.Z + (float)(moveSpeed * Math.Cos(degreesY) * Math.Cos(degreesX)) * deltaTime);
        }

        public void TranslateBackward(float deltaTime)
        {

            float degreesX = MathUtil.RadiansToDegrees(Pitch) * 0.01745329F; // X rotation
            float degreesY = MathUtil.RadiansToDegrees(Yaw) * 0.01745329F; // Y rotation
            float moveSpeed = GetCurrentMoveSpeed();
            this.Location = new Vector3(
                this.Location.X - (float)(moveSpeed * Math.Sin(degreesY) * Math.Cos(degreesX)) * deltaTime,
                this.Location.Y - (float)(moveSpeed * Math.Sin(degreesX)) * deltaTime,
                this.Location.Z - (float)(moveSpeed * Math.Cos(degreesY) * Math.Cos(degreesX)) * deltaTime);
        }

        public void TransformYawPitch(float dx, float dy)
        {
            Yaw += dx * LookSpeed;
            Pitch -= dy * LookSpeed;
            const float pitchClamp = 1.56f;
            if (Pitch <= -pitchClamp)
            {
                Pitch = -pitchClamp;
            }
            if (Pitch >= pitchClamp)
            {
                Pitch = pitchClamp;
            }
        }

        public void TranslateUp(float deltaTime)
        {
            this.Location = new Vector3(
                this.Location.X,
                this.Location.Y + GetCurrentMoveSpeed() * deltaTime,
                this.Location.Z); // TODO implement up/down based upon roll orientation.
        }

        public void TranslateDown(float deltaTime)
        {
            this.Location = new Vector3(
                this.Location.X,
                this.Location.Y - GetCurrentMoveSpeed() * deltaTime,
                this.Location.Z);
        }

        public void LookAt(Vector3 location, float pitch, float yaw, float roll)
        {
            this.Location = location;
            this.Pitch = pitch;
            this.Yaw = yaw;
            this.Roll = roll;
        }

        public void SetAspectRatio(int width, int height)
        {
            this.ViewportWidth = width;
            this.ViewportHeight = height;
            this.AspectRatio = width / (float)height;
        }
    }
}
实际上,这是我的鼠标输入代码。你在网上找不到任何东西