How I turned my 3D game into a VR game with eight lines of code
I recently published the full source code to my little mini-game "Asteroids3D". You can download it here:
Turning this into a VR game with Leadwerks Engine 4.5 was very easy. I will show you how here.
The first step is to enable VR. This code will check to see if OpenVR initializes correctly. If it fails for any reason, an error message will be printed and the game will exit:
if VR:Enable()==false then System:Print("VR failed to initialize.") return end
Now we need to adjust some settings. If you are converting an existing game into a VR game, you will probably use seated tracking space. This allows the player to sit and use the keyboard and mouse just like they would in the non-VR game:
VR:SetTrackingSpace(VR.Seated)
With seated VR mode the camera offset (your body's position and rotation) is automatically added to the camera when rendering, so there's nothing else to worry about.
After the map loads, right before your main loop starts, you should add a command to recenter the headset tracking. I added a five second delay beforehand to make sure the user has time to adjust their headset:
Time:Delay(5000) VR:CenterTracking()
If you want to mirror the display to the window, you can call VR:MirrorDisplay() right before Context:Sync(). (If you don't want this feature you can actually just remove the Sync() call completely):
VR:MirrorDisplay(context) context:Sync(false)
Sync will always disable VSync on the window when VR mode is enabled, since the headset refreshes at a higher rate than your monitor, and we don't want anything slowing it down.
The rest of your game will work just as it would normally. That's really all there is to creating a seated VR game! You can download the VR project here.
- 3
2 Comments
Recommended Comments