Book Image

Mastering Leap Motion

By : Brandon Sanders
Book Image

Mastering Leap Motion

By: Brandon Sanders

Overview of this book

<p>Leap Motion technology offers a truly innovative way of interacting with software. Traditionally, computing has always involved the use of a keyboard, a mouse, or a controller. Leap Motion gives developers a new solution to creating a radically new user experience, in a way that is both comprehensive and disruptive in the most exciting sense of the term. From typing to robotic hands and virtual harps, Leap Motion makes human and computer interaction so much more immersive.</p> <p>Beginning with a quick step-by-step guide to get you set up and an overview of how the Leap Motion API works to consolidate your knowledge, the book then looks closely at writing a 2D painting application and explores how to create a 3D application. Featuring diagrams, screenshots, and code examples to guide you as you master Leap Motion, this book will keep you in touch with the future of technology.</p>
Table of Contents (18 chapters)
Mastering Leap Motion
Credits
Foreword
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Cutting back on Leap Motion API calls


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...