我试图让相机在第一人称游戏中以相同的方式移动,但它没有移动为什么
本文关键字:移动 方式 为什么 相机 第一人称 游戏 | 更新日期: 2023-09-27 18:36:34
这是我的Game1.cs代码,我标记了相机移动代码部分的区域:在我的 Game1 中添加了相机代码.cs我在这里使用了 riemers 教程相机代码:
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series4/Mouse_camera.php
我还尝试了user31911底部的代码,但相同的结果是相机指针/光标在中间跳舞/摇晃并且没有响应。
我尝试使用 Camera 类使用鼠标移动相机。
http://pastebin.com/SF3iiftq
在构造函数中,我有这一行:
viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
如果我稍后在代码中使用此行来分配 viewMatrix 变量,那么我根本看不到地形。
最大的主要问题是鼠标根本没有响应,我得到的是鼠标指针在中间跳舞/摇晃。唯一响应的是我的ProcessInput方法,我做的键正在工作,但是方法ProcessInputCamera的键和鼠标在移动相机时没有恢复,鼠标光标在中间摇晃/跳舞。
我不知道为什么会这样。
但是鼠标没有移动相机。
请编辑您的问题!有很多不必要的信息...
这是我的相机(第一人称)课
class Camera
{ // up in here normal needed vars
public Vector3 cameraPosition;
public float moveSpeed, rotateSpeed;
public bool playing = true;
public GraphicsDevice device;
public Matrix view, projection;
Matrix rotation;
float yaw = 0;
float pitch = 0;
int oldX, oldY;
public Camera(Vector3 cameraPosition, float moveSpeed, float rotateSpeed, float filedOfView, GraphicsDevice device, float PerspectiveFieldOfView)
{
this.cameraPosition = cameraPosition;
this.moveSpeed = moveSpeed;
this.rotateSpeed = rotateSpeed;
this.device = device;
view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(PerspectiveFieldOfView), device.Viewport.AspectRatio, 0.1f, filedOfView);
ResetMouseCursor();
}
public void Update()
{
KeyboardState kState = Keyboard.GetState(); // make is able to use your keys
Vector3 v = new Vector3(0, 0, -50) * moveSpeed; // let you permanent walk
move(v); // isnt essential could be deleted if you wont that
}
if (kState.IsKeyDown(Keys.W))
{
Vector3 v = new Vector3(0, 0, -100) * moveSpeed;
move(v);
}
if (kState.IsKeyDown(Keys.S))
{
Vector3 v = new Vector3(0, 0, 50) * moveSpeed;
move(v);
}
if (kState.IsKeyDown(Keys.A))
{
Vector3 v = new Vector3(-50, 0, 0) * moveSpeed;
move(v);
projection = Matrix.
}
if (kState.IsKeyDown(Keys.D))
{
Vector3 v = new Vector3(50, 0, 0) * moveSpeed;
move(v);
}
pitch = MathHelper.Clamp(pitch, -1.5f, 1.5f);
MouseState mState = Mouse.GetState();
int dx = mState.X - oldX; /* is for turning you objekt / camera
yaw -= rotateSpeed * dx; *
*
int dy = mState.Y - oldY; *
pitch -= rotateSpeed * dy; */
ResetMouseCursor(); // this makes your mouse "dancing" in the middle
UpdateMatrices();
}
private void ResetMouseCursor() // mouse settings for the camera
{
int centerX = device.Viewport.Width / 2;
int centerY = device.Viewport.Height / 2;
Mouse.SetPosition(centerX, centerY);
oldX = centerX;
oldY = centerY;
}
private void UpdateMatrices() //standart camera things
{
rotation = Matrix.CreateRotationY(yaw) * Matrix.CreateRotationX(pitch);
Vector3 transformedReference = Vector3.Transform(new Vector3(0, 0, -1), rotation);
Vector3 lookAt = cameraPosition + transformedReference;
view = Matrix.CreateLookAt(cameraPosition, lookAt, Vector3.Up);
}
public void move(Vector3 v) // is the self programmed method to let you move
{
Matrix yRotation = Matrix.CreateRotationY(yaw);
v = Vector3.Transform(v, yRotation);
cameraPosition += v;
}
}
这是相当标准的,但很好。在大多数情况下,此类是相机及其配置所需的全部内容。重写/修复它,就像您需要它一样...
Tipp:你也可以考虑一个弧球凸轮。