Followers 0

# Matrix Maths and reseting position

## 2 posts in this topic

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) *
Matrix.CreateRotationY(shipColBox.AngularVelocity.X);

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

// Re-normalize orientation vectors
Direction.Normalize();
Up.Normalize();

// 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;
}

else
{
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
0

##### 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.

0

##### 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
0

## Create an account

Register a new account