Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我试着让相机在第一人称游戏中以同样的方式移动,但它不动为什么?_C#_Xna_Xna 4.0 - Fatal编程技术网

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

C# 我试着让相机在第一人称游戏中以同样的方式移动,但它不动为什么?,c#,xna,xna-4.0,C#,Xna,Xna 4.0,这是我的Game1.cs代码,我标记了摄像头移动代码部分的区域: 在my Game1.cs中添加了摄像头代码。我在此处使用了riemers教程摄像头代码: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series4/Mouse_camera.php 我也用USER 31911尝试了底部的代码,但是同样的结果,相机指针/光标在中间跳舞和摇晃,没有响应。 我正在尝试使用Camera类使用鼠标移动相机 http://pastebin.com/SF3

这是我的Game1.cs代码,我标记了摄像头移动代码部分的区域: 在my Game1.cs中添加了摄像头代码。我在此处使用了riemers教程摄像头代码:

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series4/Mouse_camera.php

我也用USER 31911尝试了底部的代码,但是同样的结果,相机指针/光标在中间跳舞和摇晃,没有响应。 我正在尝试使用Camera类使用鼠标移动相机

http://pastebin.com/SF3iiftq
在构造函数中,我有一行:

viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
如果我使用这一行,而不是在代码后面指定viewMatrix变量,那么我根本看不到地形

最大的问题是鼠标根本没有反应,我得到的是鼠标指针在中间摇动/摇晃。 唯一的反应是我的过程输入方法,我做了键,但工作过程中输入摄像机,KKEY和鼠标,当我移动相机时,没有重新定位,鼠标光标在中间摇动/跳舞。 我不明白为什么会这样


但是鼠标没有移动相机。

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

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

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


Tipp:你也可以考虑弧线球凸轮。

用户31911ok,我添加了这个类,并添加到我的GAME1变量中,称为“摄像机”。然后在构造器中我做了:cameraPosition=newvector3(0.0f,50.0f,5000.0f);摄像机=新摄像机(摄像机位置,10,10,20,设备,20);在更新方法中我做到了:camera.update();但它没有移动,我需要添加/更改任何其他内容?当然!!这句话很重要。。。但是如果你还不懂这门课。。。你应该读一本关于它的教程。。。如果你是德国人,我会给你找一个很好的。继续努力,克服它。。。我用我自己的这个类,并改变它,因为我需要它:P。别忘了投票赞成/同意我的答案;)使用可理解的var pls。。。这已经是正确的:cameraPosition=新矢量3(0.0f、50.0f、5000.0f);摄像机=新摄像机(摄像机位置,10,10,20,设备,20);。。。但是camera.update();在大多数情况下,这是不够的。。。或者mb你的世界和你一起移动,因为它也使用相机,我必须看到它来帮助我在代码中添加更多的注释。。。希望它能帮助你:3。。。现在应该足够了……;)我很高兴我帮助了你;I’别忘了投票支持我的答案!!:)这是我完整项目的链接,我把它添加到一个rar文件中,它没有那么大。也许有人可以看看。我还是没能去上班。
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; 
} 

}