Using the Oculus Rift DK1 with BGE

This aims to give a quick rundown of how to use the rift DK1 with BGE. It is largely thanks to Lubosz that I could even do it.

Part 1: Visuals:
These are easy thanks to lubosz. He’s got a nice single example blend you can download from his blog. (Page, File)


Part 2: Headtracking:
If you’re on arch linux this is easy as lubosz set up an AUR package for python-rift. Using your favourite AUR manager (eg yaourt) just point and click. I had a small issue with hidapi being required by another program, and hidapi-git, which python-rift depends on, needs to remove hidapi. Since you’re using arch, I’ll assume you have the know-how to fix it.

On ubuntu you have to compile openHMD and python-rift yourself. To my knowledge these do not exist in a PPA. You need several packages like cython3, libtool, and autoconf. A bit of googling on any compile errors should let you know what libraries you need.
First up, grab the openHMD source from github and extract it somewhere.
Open a terminal and change into it’s directory. Run:


./autogen.sh
./configure
make
sudo make install

If there were errors, you probably are missing some libraries. See the openHMD github page for more information.

Now openHMD is a compiled program that blender can’t access without a python layer over the top. This is provided in the form of python-rift, another package you have to install. Once again, download it and extract it somewhere. Change into it’s directory and type:

sudo python3 ./setup.py install

If it complains about not having cython, install the cython3 package.
My one spouted an error about not being able to find openhmd.h, but I knew it was in /usr/local/include/openhmd/openhmd.h Making a
link into /usr/include fixed this:

sudo ln /usr/local/include/openhmd/openhmd.h /usr/include/openhmd.h

I then reran setup.py

To see if this lot has worked, go into a terminal and type:


python3
>>> import rift

If it manages to import the module, then it’s (at least somewhat) working.

Now from blender (assuming blender is installed rather than running from an extracted zip), you should be able to access the oculus rift. The code I use is once again based on lubosz’:

from rift import PyRift
import mathutils
import bge

def rotateCam(rift):
    
    cont = bge.logic.getCurrentController()
    owner = cont.owner
    
    scene = bge.logic.getCurrentScene()
    rift.poll()
        
    rotation = mathutils.Quaternion((rift.rotation[0], 
        rift.rotation[1], 
        rift.rotation[2], 
        rift.rotation[3]))

    eu = rotation.to_euler()

    fix = mathutils.Euler((-1.57, 0, 3*1.57), 'XYZ')
    rot = mathutils.Euler((-eu.z, eu.y, -eu.x), 'XYZ')

    
    rot.rotate(fix)
     
    cam = scene.active_camera
    cam.localOrientation = rot

try:  
    rotateCam(bge.logic.rift)
except AttributeError:
    bge.logic.rift = PyRift()


It rotates the camera based on your head rotation. It uses localrotation so you can parent the camera to another object.

Now you’re up and away and can fly spaceships to your hearts content.
[IMAGE COMING WHEN I’M ON MY OTHER PC]


Part 3: developing:
The rift appears to linux as a second screen. This means you will work on your blend file on one screen and drag it onto the other to test. This quickly gets tiring so click (in blender):
window->duplicate window
On the window that just appeared, make the 3D view occupy the full screen and be in textured view. Move this to the other screen. Now you can do your development on your main monitor, move your mouse onto the rift and press ‘P’ to test the game.


Here I have taken a screencap of both screens. The left is my main monitor which I can see, and the right is the rift (currently in-game).
If you like the space-ship, head over to http://simplesix.sdfgeoff.space to fly it some more - even in multiplayer!

Test Blend:
If you have a rift and have python-rift installed and want a known working setup to test, here’s the blend of the image shown above:
See next post for demo blends.

Improved demo blend:

  • Allows for head rotation changing camera position slightly
  • Tweaked ship speed to feel more balanced
  • Tweaked eye separation to make ship seem about the size I think it should be.

SpaceCave.blend (2.73 MB)

I got the same error, but using the ln fix does not work in El Capitan, since Apple made /usr/include read only for security purposes. Best practice in this case is to go into the setup.py file in the python-rift folder, and simply modify “/usr/include/openhmd/openhmd.h” line to “/usr/local/include/openhmd”.

Thanks for the tutorial, just finished installing everything, can’t wait to test my rift with Blender!