Gamedevlog: The First VR Test
Tags: vrgame, android, opengles2, devlog
Published: 2020-09-07 10:51:16

# Preparations for the first VR test.
To prepare for the first VR test I wanted to add the following features which I thought may be needed: 
* The ability to change the distance between the two camera positions using the xbox controller.
* The ability to switch the point that each camera was looking between a common point or independent points directly in front of each camera. 

# Setting the distance between each camera
To achieve this I added a variable to the <code>PlayerVision</code> class to store the distance that each camera should be from the center <code>mHalfCameraDistance</code>, this was then used instead of the hard coded 1 and -1 values when calculating each camera’s position. 
To modify this distance, I created two functions, one to increase the distance and one to decrease it by the passed in amounts.

# Setting the cameras look at position
To achieve this I added a boolean variable to the <code>PlayerVision</code> class called <code>mLookStraightAhead</code>, if this was set to true then the cameras would look directly ahead of themselves, however if the value was false, then the cameras would share a common look at point.

This was done by calculating a common look at point which was in the look direction from the players position.

![Calculating common look at point](https://dev-to-uploads.s3.amazonaws.com/i/5tmci999lxmg6i8y3s9s.PNG)

The look direction for each camera was calculated as the look position minus the cameras position.

![Look direction example](https://dev-to-uploads.s3.amazonaws.com/i/ix2m2b2oj5r2355whfvh.PNG)

# Controlling these settings from the Xbox controller
To facilitate controlling these settings from the Xbox controller I updated the <code>ButtonControlInterface</code> and the <code>XboxController</code> and <code>GameControlHub</code> classes to allow detection and reporting of activity on the LB (L1) and RB (R1) buttons.
Then in the main game loop I checked for:
* The action button pressed which would toggle between the two camera look at modes.
* The L1 button pressed which would decrease the distance between the two cameras.
* The R1 button pressed which would increase the distance between the two cameras. 

![Changes to the game loop](https://dev-to-uploads.s3.amazonaws.com/i/h3k2w0jf28p3iskvd5re.PNG)

# The first test
With these changes implemented I placed the phone into the headset and pressed the L1 button to decrease the distance between the two cameras, this had the effect of increasing the distance???
It turns out that when calculating my look direction I had been using a positive z value instead of a negative z value as all my calculations in this test had assumed. 
Fixing this issue by inverting the look direction, then highlighted the fact that all my controls had been set to work with this incorrect look direction, testing the values returned from the Xbox controller stick movements showed that these values were inverted (except the one used for Yaw) from what I had assumed, this was fixed by inverting the stick values in the <code>XboxController</code> class. 

# The first real test
With the above issues resolved, I once again placed the phone into the headset and my first impressions were good, while looking at objects close to the camera there was a kind of ghosting where you could see the difference between the two camera positions, decreasing the distance between the two cameras had the desired effect and at distances below about 1.0 (0.5 half distance) the image looked great.
One interesting thing I did notice was that when changing the camera at the lower distances, I could see the 3D models move and then my brain would compensate and they would merge back to a single model again, I am assuming that this compensation is not possible at the larger distances between the two cameras, so as not to stress the eyes / brain too much I have decided to work with the half distance set to 0.1 for now. 

I did not notice any major difference when the two cameras were looking at a common or independent points but this may be something I notice as the scene grows.

# Conclusions
All things considered, this update was quite productive, I got the chance to brush up on my blender skills, fixed some issues with the game and had a successful VR test. 

![New VR Scene](https://dev-to-uploads.s3.amazonaws.com/i/9e3taxsla5b18hq4l9ay.png)

The code at this point in the project can be found [here](https://github.com/longevity-software/AndroidVrGameDemo/releases/tag/B1.10).