Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:56:21+00:00 2026-06-12T02:56:21+00:00

I am trying to make an easy demo where I have a cube in

  • 0

I am trying to make an easy demo where I have a cube in the space and a first person camera that moves around. I made similar thing in the past in c++ & opengl.

My problem is:

  • I can position my cube in any place in the space with no problem
  • I can create the camera and move
  • But when I create both at the same time, the cube is always in the same position relative to camera.

I suspect the problem is with world matrix. Camera is manipulating matrix and affecting cube. In OpenGL I used glPushMatrix and glPopMatrix so Matrix were stored and not affected.

//Draw Cube in openGL
    glPushMatrix();
        glTranslatef(1000,0,1000);
        glRotatef(90,0,1,0);
        DrawCube();
    glPopMatrix();
  • How can I achieve similar funcionality to Push/Pop Matrix in XNA?

  • Do you know any similar demo (first camera + object) I can look? (I am using MSDN Microsoft tutorials but most of links are broken 🙁 )

You can see what I have tried so far. I have pasted relevant methods.

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
        ProcessInput(timeDifference);

        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        //Text
        DrawText();

        //CUBE
        // Set the World matrix which defines the position of the cube
        cubeEffect.World = Matrix.CreateTranslation(modelPosition);
        // Set the View matrix which defines the camera and what it's looking at
        cubeEffect.View = Matrix.CreateLookAt(cameraPositionCube, modelPosition, Vector3.Up);
        // Set the Projection matrix which defines how we see the scene (Field of view)
        cubeEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1.0f, 1000.0f);
        // Enable textures on the Cube Effect. this is necessary to texture the model
        cubeEffect.TextureEnabled = true;
        cubeEffect.Texture = cubeTexture;
        // Enable some pretty lights
        cubeEffect.EnableDefaultLighting();
        // apply the effect and render the cube
        foreach (EffectPass pass in cubeEffect.CurrentTechnique.Passes)
        {
            pass.Apply();
            cubeToDraw.RenderToDevice(GraphicsDevice);
        }

        base.Draw(gameTime);
    }

    private void UpdateViewMatrix()
    {
        Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);

        Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
        Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
        Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;

        Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
        Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);

        viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
    }

    private void ProcessInput(float amount)
    {
        MouseState currentMouseState = Mouse.GetState();
        if (currentMouseState != originalMouseState)
        {
            float xDifference = currentMouseState.X - originalMouseState.X;
            float yDifference = currentMouseState.Y - originalMouseState.Y;
            leftrightRot -= rotationSpeed * xDifference * amount;
            updownRot -= rotationSpeed * yDifference * amount;
            Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);
            UpdateViewMatrix();
        }

        Vector3 moveVector = new Vector3(0, 0, 0);
        KeyboardState keyState = Keyboard.GetState();
        if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
            moveVector += new Vector3(0, 0, -1);
        if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
            moveVector += new Vector3(0, 0, 1);
        if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
            moveVector += new Vector3(1, 0, 0);
        if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
            moveVector += new Vector3(-1, 0, 0);
        if (keyState.IsKeyDown(Keys.Q))
            moveVector += new Vector3(0, 1, 0);
        if (keyState.IsKeyDown(Keys.Z))
            moveVector += new Vector3(0, -1, 0);
        AddToCameraPosition(moveVector * amount);
    }

    private void AddToCameraPosition(Vector3 vectorToAdd)
    {
        Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
        Vector3 rotatedVector = Vector3.Transform(vectorToAdd, cameraRotation);
        cameraPosition += moveSpeed * rotatedVector;
        UpdateViewMatrix();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:56:23+00:00Added an answer on June 12, 2026 at 2:56 am

    Your UpdateViewMatrix() creates a matrix, which is never used. Use this matrix as the effect’s view matrix instead of creating a new one in Draw().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a little ball that rolls around based on the
EDIT following Ben's answer I'm trying to make something that should be very easy
I'm trying to make it really easy to logrotate some of my apps that
I’m trying to make a button that displays an image, that’s easy, but the
Trying to make it so that the glob function displays only the first 10
I am trying to make a function that will allow me to easy display
So im trying to make an easy script, which will hold a video... And
I'm having difficulty with Javascript instance variables. I'm trying to make an easy way
Greetings, With javascript, I am trying to make a very easy animation, an image
I'm trying make an entity with doctrine that has three associations with other entities

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.