Refactoring...
Tags: android, vrgame, devlog, opengles2
Published: 2020-05-09 09:25:50

#The issue with the last devlog
While reviewing the previous devlog I had one of those "Oh c**p" moments... my direction changes did not account for the way the player was currently facing, if the player turned around 180° then the left and right would be reversed to what you'd expect, as would the forward and backwards controls.

#Fixing this and refactoring the code
While fixing this I decided to perform the following code changes at the same time.
<ul>
<li>Create a game loop thread which will process the controller inputs and render the scene at 40 frames per second.</li>
<li>Create a player vision class which will process the look rotation and adjust the two <code>GameCamera</code>'s accordingly</li>
<li>Create a player class which will hold the players position and an instance of the new vision class.</li>
</ul>

#Accounting for the rotation when moving
when moving in any direction (forward, backwards, left, right) we need to know what way the player is looking to know what direction forward etc is.
To do this we create a translation matrix for the player movement, then create a rotation matrix from the Yaw component of the look rotation and multiply the two.
This will rotate the direction the player wants to move to the direction the player is looking.

#A bit more Kotlin
As part of these changes I made significant use of Kotlin's <code>also</code> scoped function.
This allowed me to declare a <code>FloatArray</code> for a matrix and perform actions on the Matrix, all within the scope of the declaration, this is probably not the intended use of this function but I found it much cleaner when there are multiple steps to set a variables value e.g.
![One use case for the also function](https://dev-to-uploads.s3.amazonaws.com/i/vp7yiz317cfe5vd0z8eg.PNG)

#Reformatting the code
During the previous developments I always thought it was a bit odd, passing the <code>GameControlHub</code> to the <code>VrRenderer</code>.
Therefore, as part of this stage, I decided to create a GameLoop class which implements the <code>Runnable</code> interface and processes any game controller input, adjusts the <code>Player</code> and then requests the scene is rendered.
Initially I have added a 25mS delay after the scene is rendered to limit the frame rate to < 40fps, going forward I shall make this a more accurate refresh rate.

#The <code>PlayerVision</code>
This new class holds the two <code>GameCamera</code> instances that were previously in the <code>VrRenderer</code> class.
An instance of the the class is created in the <code>MainActivity</code>'s <code>onCreate</code> method. this instance is then passed to the an instance of the <code>Player</code> class which updates the camera location and look/up directions, and it is also passed to the <code>VrRenderer</code> via the <code>VrGlSurfaceView</code> which uses the each of the <code>GameCamera</code>'s to render the scene as before.
Each loop of the <code>GameLoop</code> updates the <code>Player</code> which in turn updates the <code>PlayerVision</code>.
First a rotation matrix is generated using the pitch and yaw rotations
![setting the look rotation](https://dev-to-uploads.s3.amazonaws.com/i/ihaeh9ohm6wqa18pkpkl.PNG)
Then this rotation is then applied to the look direction, up direction, left camera position and right camera position e.g.
![rotating the look direction](https://dev-to-uploads.s3.amazonaws.com/i/vp7yiz317cfe5vd0z8eg.PNG)
Finally we update the two <code>GameCamera</code>'s position, look direction and up directions which the resulting matrices.
![Setting the camera position](https://dev-to-uploads.s3.amazonaws.com/i/wjl12nrgdj0ojl1wszh7.PNG)
One thing to note is that the up and look direction components are direction vectors but the <code>GameCamera</code>'s position is a position vector, therefore the players position (x and z) is applied to the calculated camera positions. 

Until next time, the source code at this point in the process can be found [in pre-release B1.4 on my github](https://github.com/longevity-software/AndroidVrGameDemo/releases/tag/B1.4).