我试图让相机在第一人称游戏中以相同的方式移动,但它为什么不动?

时间:2013-10-23 08:10:44

标签: c# xna xna-4.0

这是我的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 kkeys和鼠标没有重新映射当我移动相机鼠标光标在中间摇动/跳舞时。

我无法弄清楚为什么会这样。

但鼠标没有移动相机。

1 个答案:

答案 0 :(得分:1)

请编辑您的问题!有许多不必要的信息...

这是我的相机(第一人称)课程

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

} 

这很标准,但很好。 在大多数情况下,这个类就是Camera和它的配置所需要的。 重写/修复它,就像你需要它一样......

Tipp:你也可以考虑一个Arc-Ball-Cam ..