For the uninitiated, the Leap Motion is a motion sensor, tracking hand and finger movement. It has an SDK enabling one to use these movements as inputs in applications. It is actually quite a sophisticated gadget, as it can recognise both hands and fingers individually and as well as detect gestures like swiping and tapping.
Setting it up
I started by forking an existing Breakout clone as I was not interested too much in writing the game as that was not the intent of the hack. I rearranged the project to be in a multi-module maven project. This allowed me to seperate out the Leap Motion code from everything else.
It ended up looking like this:
\brick-attack-game \brick-attack-leapmotion \repository
Note that the source code to this post can be found here.
The brick-attack-game module contains the game which encompasses the graphics and game logic. The brick-attack-leapmotion module is an extension of the game but with Leap Motion controls attached. The repository module simply exposes the Leap Motion libraries as maven dependencies to be used by the brick-attack-leapmotion module.
For a Java application to use Leap Motion, you need two files. The LeapJava.jar file which contains the Java API and the libLeapJava.dylib file which contains the native bindings for the library. This file name will vary depending on which operating system you are using. I use OSX, hence the .dylib extension. See the Leap Motion developer site for more information. These two files have been packaged up as part of the repository module, so when the brick-attack-leapmotion module depends on this module, these files are brought along as well.
Using Leap Motion
The crux of everything that happens is in the LeapMotionGame class. This subclasses the original Game class and adds the Leap Motion initialisation logic and overrides the Game’s movePaddle method to use Leap Motion for horizontal movement in lieu of the keyboard.
The main thing to note with this class is you must keep references to the listener and controller objects and ensure they are not garabage collected. Failing to do so will cause your application to crash.
Now let’s take a look at the actual LeapMotionListener class.
The listener simply gets the first hand in the frame and if that hand has a valid palm position, will store its X co-ordinate. Admittedly this is quite a simple use case, however the API does support more sophisticated operations like gesture detection and interrogating both hands.
Running the Game
With this all in place, you should be able to run the game which is a simple class of launching the main class (LeapMotionGame). The only thing to note here is you need to set the -Djava.library.path to the path of the native library. I have included a shell script that has an example of this.
Here is a video of the game in action.
Hopefully this post showed you how easy it is to play and experiment with the Leap Motion SDK. I hope you find an interesting use for it.
Here are some related links on the topic.