Simple as it might sound, cutting back on calls to the Leap API and caching tracking data can (slightly) increase the responsiveness of your code. Take the following (bad) example of an infinite loop that outputs the position of the first hand in the Leap's field of view to the console:
while (true) { Controller controller = new Controller(); if (controller.frame().hands().isEmpty() == false) { float x = controller.frame().hands().get(0).palmPosition().getX(); float y = controller.frame().hands().get(0).palmPosition().getX(); System.out.println("Hand X|Y: " + x ":" + y); } }
This piece of code is pretty simple and short, right? This shortness is actually its downfall, allowing this snippet to demonstrate a few things you can do wrong when writing a piece of Leap code.
Let's break down what's wrong with this snippet:
We create an entirely new
Controller
reference in each iteration. This is terrible because initialization of a new object...