Skip to content
bengfarrell edited this page May 4, 2013 · 10 revisions

Basic usage of the NuiMotion AddOn probably isn't the prettiest thing to look at. Without web sockets to a browser, or some kind of other addon to display graphics from Node.js, the best we can do is offer text output of gestures, user events, and skeletal data.

That said, we can make a quick Node.js application that offers text output to test if our plugin works.

Use the Quickstart Example at https://github.com/bengfarrell/nuimotion/blob/master/quickstart/basicquickstart.js

In the Quickstart, we do several things:

Check for Joint Positions in our Skeleton

nuimotion.startSkeletonListener( [ nuimotion.Joints.LEFT_HAND, nuimotion.Joints.RIGHT_HAND ], onSkeletonUpdate /* , 50 (the default) */ );

nuimotion.startSkeletonListener accepts an array of joints, a callback method, and an option check frequency (default is 50ms). Joints are all enumerated in the nuimotion.Joints object.

The callback method will return one parameter. This parameter is the skeleton object. It will contain key/value pairs where the key is the joint name and the value is the joint object containing x, y, and z coordinates as well as other values like rotation, if the joint is active, and (if an arm) what percent that arm is extended.

Listen for User/Device Events

nuimotion.addListener( [ nuimotion.Events.SKELETON_TRACKING, nuimotion.Events.SKELETON_STOPPED_TRACKING ], onEvent );

In our example quickstart, we listen for when the skeleton starts tracking and stops tracking. There are more events enumerated in the nuimotion.Events object. We can add an event type to listen for or an array of event types followed by our callback method.

Listen for Gestures

nuimotion.addGesture( [ nuimotion.Events.Gestures.Swipe.types.up, nuimotion.Events.Gestures.Swipe.types.down, nuimotion.Events.Gestures.Swipe.types.left, nuimotion.Events.Gestures.Swipe.types.right, nuimotion.Events.Gestures.Wave.types.hand], onEvent);

In our example quickstart, we listen for gestures much like events.

Wrapping up the implementation

After your custom code is setup such as gestures/events added, skeleton listener set up, run init to start using the device and tracking:

nuimotion.init();

Once you are finished, please call the close method. Here, we put it inside the Node.js application close event:

process.on('exit', function() { nuimotion.close(); });

Use with WebSockets

Grab the websockets addon via NPM.

Instead of the basic usage example above, use the NuiMotion Websockets Quickstart

Also grab nuimotion-websockets.js for use in your browser to hide the implementation details of the sockets and pretend like you're directly interacting with NuiMotion.

The only difference is passing the socket address on nuimotion.init:

nuimotion.init("ws://localhost:8080");

More Examples

More complicated examples and demos are added as part of the Sweatin' to the Web project. Source code can be found here:

https://github.com/bengfarrell/node-sweatintotheweb-examples

Clone this wiki locally