Jump to content
  • Advertisement
Sign in to follow this  

Matrix Maths and reseting position

This topic is 2133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Maths has never been a strong point of mine.

My problem is that I have a player model thats starts off in the game world and rotates left,right, up, down just fine when stationary. but as soon as the player moves away from the starting position the rotations go all wrong as if the martrixs arent being recalculated. 


This is how I am currently calculating the matices in my update method. Any with better maths skills see where the error is?

public void Update(GameTime gameTime)
            KeyboardState keyboardState = Keyboard.GetState();

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (keyboardState.IsKeyDown(Keys.Left))
                shipColBox.AngularVelocity = Vector3.Up;                

            if (keyboardState.IsKeyDown(Keys.Right))
                shipColBox.AngularVelocity = Vector3.Down;                 

            if (keyboardState.IsKeyDown(Keys.Up))
                shipColBox.AngularVelocity = Vector3.Left;

            if (keyboardState.IsKeyDown(Keys.Down))
                shipColBox.AngularVelocity = Vector3.Right;

            // Scale rotation amount to radians per second
            shipColBox.AngularVelocity = shipColBox.AngularVelocity * RotationRate * elapsed;  

            //Create rotation matrix
            Matrix rotationMatrix =
                Matrix.CreateFromAxisAngle(Right, shipColBox.AngularVelocity.Y) *

            //Rotate orientation vectors
            Direction = Vector3.TransformNormal(Direction, rotationMatrix);
            Up = Vector3.TransformNormal(Up, rotationMatrix);

            // Re-normalize orientation vectors            

            // Re-calculate Right
            right = Vector3.Cross(Direction, Up);
            // re-calculate with a cross product to ensure orthagonality
            Up = Vector3.Cross(Right, Direction);

            // Determine thrust amount from input

            if (keyboardState.IsKeyDown(Keys.W))
                thrustAmount = 5.0f;            

                thrustAmount = 0;
                shipColBox.LinearVelocity = new Vector3(0, 0, 0f);

            // Calculate force from thrust amount
            Vector3 force = shipColBox.OrientationMatrix.Forward * thrustAmount * ThrustForce;

            // Apply acceleration
            Vector3 acceleration = force / shipColBox.Mass;
            Velocity += acceleration * elapsed;

            //Apply psuedo drag
            Velocity *= DragFactor;

            //Apply velocity
            shipColBox.Position += Velocity * elapsed;

            // Reconstruct the ship's world matrix
            world = Matrix.Identity;
            world.Forward = Direction;
            world.Up = Up;
            world.Right = right;
            world.Translation = Position;
Edited by Null_Reference_Exception

Share this post

Link to post
Share on other sites

If it's wrong when you move from (0, 0) then you should try to swap multiplication order: first rotation and then translation.


I gave the a bash still has the same result, 

I added a short youtube video to demonstrate what exactly is happeining.


At the start I am using the up and down arrows to move the player, but when i turn to the right and try to move up and down again you can see the result 

Edited by Null_Reference_Exception

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!