My code come from the example gettingStarted and i add this :
Code: Select all
//function Draw of class EntityModel
worldMatrix = transform *
Matrix.CreateTranslation(entity.centerPosition) *
entity.orientationMatrix;
....
Code: Select all
//Function handleInput of EntityModel
public void handleInput(KeyboardState currentKeyboardState)
{
// First, we want to check to see if the tank should turn. turnAmount will
// be an accumulation of all the different possible inputs.
float turnAmount = 0.0f;
if (currentKeyboardState.IsKeyDown(Keys.Left))
{
turnAmount += 1;
}
if (currentKeyboardState.IsKeyDown(Keys.Right))
{
turnAmount -= 1;
}
// clamp the turn amount between -1 and 1, and then use the finished
// value to turn the tank.
turnAmount = MathHelper.Clamp(turnAmount, -1, 1);
heroFacingDirection += turnAmount * heroTurnSpeed;
// Next, we want to move the tank forward or back. to do this,
// we'll create a Vector3 and modify use the user's input to modify the Z
// component, which corresponds to the forward direction.
Vector3 movement = Vector3.Zero;
if (currentKeyboardState.IsKeyDown(Keys.Up))
{
movement.Z = 1;
}
if (currentKeyboardState.IsKeyDown(Keys.Down))
{
movement.Z = -1;
}
// next, we'll create a rotation matrix from the direction the tank is
// facing, and use it to transform the vector.
orientation = Matrix.CreateRotationY(heroFacingDirection);
Vector3 velocity = Vector3.Transform(movement, orientation);
velocity *= heroVelocity;
// Now we know how much the user wants to move. We'll construct a temporary
// vector, newPosition, which will represent where the user wants to go. If
// that value is on the heightmap, we'll allow the move.
Vector3 newPosition = position + velocity;
orientation.Right = Vector3.Cross(orientation.Forward, orientation.Up);
orientation.Right = Vector3.Normalize(orientation.Right);
orientation.Forward = Vector3.Cross(orientation.Up, orientation.Right);
orientation.Forward = Vector3.Normalize(orientation.Forward);
entity.orientationMatrix = orientation;
// once we've finished all computations, we can set our position to the
// new position that we calculated.
position = newPosition * 0.02f;
entity.move(position);
}
Thank you!