Getting started with HAPI


Jump to: navigation, search

To use the haptics features of HAPI in an application built in C++ a haptics device must be created and initialized. To create an interface to a haptics device simply create an instance of the class interfacing with the desired haptics device, such as PhantomHapticsDevice or ForceDimensionHapticsDevice. The AnyHapticsDevice class will connect to the first found device when initialized and will therefore be used in example code. To create and initialize do the following.

AnyHapticsDevice hd;
if( hd.initDevice() != HAPIHapticsDevice::SUCCESS ) {
  cerr << hd.getLastErrorMsg() << endl;
  return 0;

For HAPI to render forces from 3D-information a haptics renderer has to be set. Provided that an instance of a haptics device is created simply add the renderer like this:

// In this case we want a GodObjectRenderer
hd.setHapticsRenderer( new GodObjectRenderer );

The instance of the renderer will be destroyed when the haptics device instance is destroyed.

Now when a renderer is choosen it is possible to feel the surface of a simple shape. The shape and surface has to be constructed and the shape has to be transferred to the haptics loop. In the following example the shape is a Sphere.

// Creating a default surface.
HAPISurfaceObject * my_surface = new FrictionSurface();
// Creating a sphere with radius 0.05 and center in (0, 0, 0). Units in m.
HapticPrimitive *my_haptic_sphere =  new HapticPrimitive(
  new Collision::Sphere( Vec3( 0, 0, 0 ), 0.05 ),
  my_surface );
// Add the shape to be rendered on the device.
hd.addShape( my_haptic_sphere );
// Transfer objects (shapes) to the haptics loop.

The surface and the shape are reference counted and no explicit call to delete is needed for my_surface and my_haptic_sphere.

A similar procedure is used when adding a force effect to be rendered on the haptics device. Create and instance of the force effect and add it to the haptics device then transfer it to the haptics loop.

// The spring effect with a position and spring_constant input by the user.
HapticSpring *spring_effect = new HapticSpring( Vec3( x, y, z ),
                                                spring_constant );
// Add the effect to the haptics device.
hd.addEffect( spring_effect );
// Send the effect to the haptics loop and from now on it will be used to
// send forces to the device.

Full examples for HAPI can be found in the HAPI/examples folder of a HAPI installation. Some of the functions used here and in the examples have more arguments to control how things are added and how they are rendered. Check the doxygen documentation and/or the source code for understanding these arguments.

Personal tools
go to