• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

We're also offering banner ads on our site from just \$5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

#ActualNull_Reference_Exception

Posted 14 December 2013 - 12:37 PM

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


#2Null_Reference_Exception

Posted 14 December 2013 - 12:36 PM

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


#1Null_Reference_Exception

Posted 14 December 2013 - 12:35 PM

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


PARTNERS