-
Notifications
You must be signed in to change notification settings - Fork 2
Lesson 02
Now we want to control stuff in our scene, for simplicity for now we want the cube to rotate when we move the mouse around. Also the cube should move in the xy-plane via the arrow keys. Since control means we have to remember our values from frame to frame, we have to introduce variables.
private float _cubeXPos, _cubeYPos, _cubeXRot, _cubeYRot;
One variable for each axis: rotation of the cube around the x and y axis, position of the camera on the x and y axis.
First lets look at the mouse input:
_cubeYRot += Input.Instance.GetAxis(InputAxis.MouseX);
_cubeXRot -= Input.Instance.GetAxis(InputAxis.MouseY);
This just adds/subtracts up the mouse input to its respective variable.
And the keyboard input:
if (Input.Instance.IsKey(KeyCodes.Right))
_cubeXPos += 1;
if (Input.Instance.IsKey(KeyCodes.Left))
_cubeXPos -= 1;
if (Input.Instance.IsKey(KeyCodes.Up))
_cubeYPos += 1;
if (Input.Instance.IsKey(KeyCodes.Down))
_cubeYPos -= 1;
Also quit straight forward, if a key is pressed the respecive variable is modified.
If you are coding along and run the code you will notice that nothing is different to the prior lesson. Well, we haven't told RC what it should do with those variables. For convenience we'll split this part up into a part where we define the matrices and the part where we hand the matrices over ro RC.
var mtxRot = float4x4.CreateRotationY(_cubeYRot) * float4x4.CreateRotationX(_cubeXRot);
var mtxPos = float4x4.CreateTranslation(_cubeXPos, _cubeYPos, 0);
var mtxCam = float4x4.LookAt(0, 200, 500, 0, 0, 0, 0, 1, 0);
We define the rotation matrix, followed by the translation matrix and finaly the camera matrix. This makes our definition of the modelview-matrix much more readable:
RC.ModelView = mtxPos * mtxRot * mtxCam;
Play around with the order of multiplications and see the different behaviours. Also the given multiplication isn't what you probably expected, since the movment depends on the rotation, in other words, the movement isn't in world coordinates. If you are coding along, you should also add another static object so you can see the movement relative to it.
[> Lesson 03 - The Bigger Picture](Lesson 03)