Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Camera not moving sideways based on direction it is looking in XNA

$
0
0

@JeroenT wrote:

The camera needs to move in the direction it is moving. I can move forward and backwards but I don't know how to move left or right. The idea is also that the camera moves forward, backwards, left and right while the y axis is fixed when it is moving. The code is written in C# and in monogame. The code first shown is the way in which we create our camera. Thanks in advance.

public Vector3 position = new Vector3(0, 0.5f, 4);

private float rotation;
Vector2 mouseMovement;
private Point screen;
public Vector3 lookAt;
private Vector3 baseCameraReference = new Vector3(0, 0.5f, 4);
public Vector3 cameraForward;

public Camera(Vector3 position, float rotation, GraphicsDevice graphicsDevice, float nearClip, float farClip)
{
    Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, graphicsDevice.Viewport.AspectRatio, nearClip, farClip);
    screen.X = graphicsDevice.Viewport.Width /2;
    screen.Y = graphicsDevice.Viewport.Height /2;
    cameraForward = baseCameraReference;
    //MoveTo(position, rotation);
}
#endregion

#region Helper Methods
private void UpdateLookAt()
{
    Vector3 normal = Vector3.Cross(cameraForward, Vector3.Up);
    cameraForward += mouseMovement.X * 0.01f * normal;
    cameraForward -= mouseMovement.Y * 0.01f * Vector3.Up;
    cameraForward.Normalize();
    lookAt = position + cameraForward;
    Matrix rotationMatrix = Matrix.CreateRotationY(rotation);
    needViewResync = true;
}

public void Update(GameTime gameTime)
{
    currentMouseState = Mouse.GetState();
    Mouse.SetPosition(screen.X, screen.Y);
    mouseMovement.X = (currentMouseState.X -screen.X);
    mouseMovement.Y = (currentMouseState.Y -screen.Y);
   // prevMouseState = currentMouseState;
    UpdateLookAt();
}

And this is the way in which we try to move it:

if (keyState.IsKeyDown(Keys.W))
{
    camera.position += (camera.cameraForward - new Vector3(0, camera.cameraForward.Y,0)) *0.05f;
}

if(keyState.IsKeyDown(Keys.S))
{
    camera.position -= (camera.cameraForward - new Vector3(0, camera.cameraForward.Y, 0)) * 0.05f;
}

if (keyState.IsKeyDown(Keys.D))
{
    float newx = camera.cameraForward.X;
    float newz = -camera.cameraForward.Z;

    camera.cameraForward.X = newz;
    camera.cameraForward.Z = newx;

    camera.position += (camera.cameraForward - new Vector3(0, camera.cameraForward.Y, 0))*0.05f
}

I have tried turning around the values but that just seems to make the camera change direction and bounce back to original direction.

Thanks in advance for any help.
Jeromer

Posts: 11

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles