Skip to content
Alexander Scheurer edited this page Feb 14, 2014 · 4 revisions

Structure: Player

The Player class is derived for GameEntity, the additional functionallity is needs is: reacting on input and, since we have the camera following the player, providing the camera matrix for all render operations.

Variables

private float3 _rotation;
private float3 _rotationSpeed = new float3(1, 1, 1);
private float _speed;
private float _speedModifier = 1;
private float SpeedMax = 30f;

Variables for our movement, where as rotation will not describe the current orientation but the amount of rotation that will be applied this frame. Same with speed, but since speed will be gradually in-/decreased it can also be seen as the current speed.

Methodes

public void Move()
{
   [..]

   Position *= float4x4.CreateTranslation(-Position.Row3.xyz) *
               float4x4.CreateFromAxisAngle(float3.Normalize(Position.Row1.xyz), -_rotation.x) *
               float4x4.CreateFromAxisAngle(float3.Normalize(Position.Row0.xyz), -_rotation.y) *
               float4x4.CreateFromAxisAngle(float3.Normalize(Position.Row2.xyz), -_rotation.z) *
               float4x4.CreateTranslation(Position.Row3.xyz) *
               float4x4.CreateTranslation(float3.Normalize(Position.Row2.xyz) * -_speed);

  [..]
}

The Move() method is quit a bunch of code, but most of it is based on "press button -> do stuff". This means we are setting the rotation vector and in-/decreasing the value of speed.

The interesting part is the matrix calculation, which is a basic principle of computer graphics but never the less interesting. In order of operations: translate the object back to the coordinate systems origin, rotate it around each axis, translate it back to where it was, translate again respective to the current speed.

What are those e.g. float3.Normalize(Position.Row1.xyz), they are normalized rotation vectors, they alow to calculate back and forth between local and global coordinates.

public float4x4 GetCamMatrix()
{
   return float4x4.LookAt(Position.M41 + (float3.Normalize(Position.Row2.xyz).x),
                          Position.M42 + (float3.Normalize(Position.Row2.xyz).y),
                          Position.M43 + (float3.Normalize(Position.Row2.xyz).z),
                          Position.M41,
                          Position.M42,
                          Position.M43,
                          Position.M21,
                          Position.M22,
                          Position.M23)
                          * float4x4.CreateTranslation(0, -300, -1000);
}

The camera follows the player, this means our player object has to provide the camera matrix. this is the same LookAt() we use since Lesson 1 but in this case we calculate its value from our player position and finaly translate back-up over the player.

[> Lesson 07 - ShaderEffect](Lesson 07)