XNA-Moving camera (view) rotated relative to the world

So I tried to get this to work, but so far no luck, hope you can help. The fact is that I have a camera in my project that the user can freely move with the mouse and buttons. Currently: move = new Vector3(0, 0, -1) * moveSpeed; move = new Vector3(0, 0, 1) * moveSpeed; ... move = new Vector3(0, 0, -1) * moveSpeed; move = new Vector3(0, 0, 1) * moveSpeed; ... move = new Vector3(0, 0, -1) * moveSpeed; move = new Vector3(0, 0, 1) * moveSpeed; ... And then I just add a move vector to the camera vector: cameraPos += move

Then the problem is that if I turn the camera and then try to move, for example, down, it will not move directly down, but at a certain angle. I assume this is due to movement along the local axis. But what I want to do is move along the world axis. Is something like this possible, or do I need to somehow calculate the angle, and then move to more than one axis?

Yours faithfully!

EDIT: I rotate the camera, where cameraPos is the current camera position and rotation is the current camera rotation. And this is the code to rotate the camera:

 void Update() { ... if(pressed) { int newY = currentY - oldY; pitch -= rotSpeed * newY; } Rotate(); } void Rotate() { rotation = Matrix.CreateRotationX(pitch); Vector3 transformedReference = Vector3.Transform(cameraPos, rotation); Vector3 lookAt = cameraPos + transformedReference; view = Matrix.CreateLookAt(cameraPos, lookAt, Vector3.Up); oldY = currentY; } 

Ihope is more readable.

+4
source share
2 answers

I managed to solve this problem using:

 Vector3 v; if (state.IsKeyDown(Keys.Up)) v = new Vector3(0, 0, 1) * moveSpeed; ... //Other code for moving down,left,right if (state.IsKeyDown(Keys.V)) view *= Matrix.CreateRotationX(MathHelper.ToRadians(-5f) * rotSpeed); //Multiplying view Matrix to create rotation view *= Matrix.CreateTranslation(v); //Multiplying view Matrix to create movement by Vector3 v 
+2
source

I believe that you are already keeping the direction you are looking in Vector3 . Replace the method as follows:

 direction.Normalize(); var move = direction * moveSpeed; cameraPos += move; 
0
source

Source: https://habr.com/ru/post/1444615/


All Articles