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 和鼠标没有响应。

我不知道为什么会这样。

但是鼠标没有移动相机。

4

1 回答 1

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

} 

这是相当标准的,但很好。在大多数情况下,这个类是相机及其配置所需的全部内容。重写/修复它,就像你需要它一样......

Tipp:您也可以考虑使用 Arc-Ball-Cam。

于 2013-10-23T08:27:30.443 回答