Skip to main content

Class BhapticsLibrary

class BhapticsLibrary contains the functions for using haptic devices from bHaptics.

References

  • Path: Assets\Bhaptics\SDK2\Scripts\Core\Plugins\BhapticsLibrary.cs

Static Functions — Play Event-driven Haptic

Play the haptic patterns bound to specific haptic event made from Designer/Portal. We highly recommend to use these functions.

Play

public static int Play(string eventID);

Play the haptic event. It is the most basic way to call a haptic event. It has the lowest overhead of any haptic event-calling function.

Parameters

  • string eventId: Name of haptic event which you want to play.

Returns

Request ID. You can use the request ID to stop the haptic. It returns -1 if the return fails.

Example

using Bhaptics.SDK2;

public class Example : MonoBehaviour
{
private void OnShoot()
{
// Assume the event name is "shootpistol"
BhapticsLibrary.Play(BhapticsEvent.SHOOTPISTOL);
// or
BhapticsLibrary.Play("shootpistol");
}
}

PlayParam

public static int PlayParam(string eventId, float intensity, float duration, float angleX, float offsetY);

Play haptic event, with adjusting the strength, duration, and direction of the haptic.

Parameters

  • string eventId: Name of haptic event which you want to play.
  • float intensity: The haptic intensity is multiplied by this value.
  • float duration: The haptic duration is multiplied by this value.
  • float angleX: Rotate haptic counterclockwise around the global Vector3.up. Valid range is: [0.0f - 360.0f]
  • float offsetY: Move haptic up or down. Valid range is: [-0.5f - 0.5f]

Returns

Request ID. You can use the request ID to stop the haptic. It returns -1 if the return fails.

Example

using Bhaptics.SDK2;

public class Example : MonoBehaviour
{
private void OnShoot()
{
BhapticsLibrary.PlayParam(
BhapticsEvent.SHOOTPISTOL, // Haptic name
1.0f, // Haptic intensity
1.0f, // Haptic duration
20.0f, // Rotate haptic around global Vector3.up (0f - 360f)
0.3f // Move haptic up or down (-0.5f - 0.5f)
);
}
}

PlayLoop

public static int PlayLoop(
string eventId,
float intensity = 1.0f,
float duration = 1.0f,
float angleX = 0.0f,
float offsetY = 0.0f,
int interval = 200,
int maxCount = 999999
);

Play the haptic repeatedly. Additionally, like the function PlayHapticWithOption, you can adjust the strength, duration, and direction of the haptic.

Parameters

  • string eventId: Name of haptic event which you want to play.
  • float intensity: The haptic intensity is multiplied by this value.
  • float duration: The haptic duration is multiplied by this value.
  • float angleX: Rotate haptic counterclockwise around the global Vector3.up. Valid range is: [0.0f - 360.0f]
  • float offsetY: Move haptic up or down. Valid range is: [-0.5f - 0.5f]
  • int interval: The time interval between loops, measured in milliseconds.
  • int maxCount: The number of loops.

Returns

Request ID. You can use the request ID to stop the haptic. It returns -1 if the return fails.

Example

using Bhaptics.SDK2;

public class Example : MonoBehaviour
{
private void OnShoot()
{
BhapticsLibrary.PlayLoop(
BhapticsEvent.SHOOTPISTOL, // Haptic name
1.0f, // Haptic intensity
1.0f, // Haptic duration
20.0f, // Rotate haptic around global Vector3.up (0f - 360f)
0.3f // Move haptic up or down (-0.5f - 0.5f)
200, // Loop interval time (millisecond)
999999 // Loop count
);
}
}

Static Functions — Play Haptic Directly

If you want to play haptics without creating a new event, use these functions.

PlayMotors

public static int PlayMotors(int position, int[] motors, int durationMillis);

Play haptic feedback on the specific haptic actuator. You can use this function without creating an event.

Parameters

  • int position: Type of haptic device.

    ValueDeviceSame as…Motors Count
    0TactSuitPositionType.Vest40
    1Tactosy for Arms(Left)PositionType.ForearmL6
    2Tactosy for Arms(Right)PositionType.ForearmR6
    3TactVisorPositionType.Head4
    4Tactosy for Hands(Left)PositionType.HandL6
    5Tactosy for Hands(Right)PositionType.HandR6
    6Tactosy for Feet(Left)PositionType.FootL6
    7Tactosy for Feet(Right)PositionType.FootR6
    8TactGlove(Left)PositionType.GloveL6
    9TactGlove(Right)PositionType.GloveR6
  • int[] motors: Assign the length of the array by the number of motors for device. Values in the array means motors' intensity. Valid range for each value in the array is: [1 - 100]

  • int durationMillis: The duration of haptic, measured in millisecond. Greater than or equal to 100 is recommended.

Returns

Request ID. You can use the request ID to stop the haptic. It returns -1 if the return fails.

Example

using Bhaptics.SDK2;

public class Example : MonoBehaviour
{
private void OnCall()
{
// TactSuit has 40 motors, so length of array should be 40 too.
int[] MotorValueArray = new int[] {
50, 40, 30, 20, 10, 50, 40, 30, 20, 10,
50, 40, 30, 20, 10, 50, 40, 30, 20, 10,
50, 40, 30, 20, 10, 50, 40, 30, 20, 10,
50, 40, 30, 20, 10, 50, 40, 30, 20, 10
};

BhapticsLibrary.PlayMotors(
(int)PositionType.Vest, // Device type
MotorValueArray, // Haptic intensities
500 // Haptic duration (millisecond)
);
}
}

PlayWaveform

public static int PlayWaveform(
PositionType positionType,
int[] motorValues,
GlovePlayTime[] playTimeValues,
GloveShapeValue[] shapeValues
);

TactGlove Only. Play haptics in TactGlove. Unlike using PlayMotors, you can finely adjust haptic duration and vibration intensity changes. This allows for even finer expression of haptic feedback.

Each array must have six elements, and at least one element is required to work.

Parameters

  • PositionType positionType: Type of haptic device.

    ValueDeviceSame as…
    PositionType.GloveLTactGlove(Left)8
    PositionType.GloveRTactGlove(Right)9
  • int[] motorValues: An array consisting of six elements, each representing the intensity of a motor. The array must have a length of six, as there are six motors in one TactGlove. Valid range for each value in the array is: [1 - 100]

    Array IndexMotor is positioned at…
    0Tip of the thumb
    1Tip of the index finger
    2Tip of the middle finger
    3Tip of the ring finger
    4Tip of the little finger
    5On the wrist
  • GlovePlayTime[] playTimeValues: An array consisting of six elements, each representing a time interval for actuation, with time defined using GlovePlayTime enums.

    ValueDurationSame as…
    GlovePlayTime.NoneNone0
    GlovePlayTime.FiveMS5ms1
    GlovePlayTime.TenMS10ms2
    GlovePlayTime.TwentyMS20ms4
    GlovePlayTime.ThirtyMS30ms6
    GlovePlayTime.FortyMS40ms8
  • GloveShapeValue[] shapeValues: An array consisting of six elements, each representing the forms of haptic intensity changes over time, specified by the GloveShapeValue enums.

    ValueWaveformSame as…
    GloveShapeValue.ConstantConstant intensity for the duration0
    GloveShapeValue.DecreasingStarts at the specified intensity and decreases by half1
    GloveShapeValue.IncreasingStarts at half of the specified intensity and increases to the specified one.2

Returns

Request ID. You can use the request ID to stop the haptic. It returns -1 if the return fails.

Example

using Bhaptics.SDK2;

public class TestHaptic : MonoBehaviour
{
public void OnCall()
{
int[] motorValues = new int[6] { 50, 80, 100, 0, 0, 0 };

GlovePlayTime[] playTimeValues = new GlovePlayTime[6] {
GlovePlayTime.FiveMS, GlovePlayTime.TwentyMS,
GlovePlayTime.ThirtyMS, GlovePlayTime.None,
GlovePlayTime.None, GlovePlayTime.None
};

GloveShapeValue[] shapeValues = new GloveShapeValue[6] {
GloveShapeValue.Constant, GloveShapeValue.Decreasing,
GloveShapeValue.Increasing, GloveShapeValue.Constant,
GloveShapeValue.Constant, GloveShapeValue.Constant
};

BhapticsLibrary.PlayWaveform(
PositionType.GloveL, // Device Type
motorValues, // Intensities
playTimeValues, // Intervals
shapeValues // Intensity changing forms
);
}
}

PlayPath

public static int PlayPath(
int position,
float[] xValues,
float[] yValues,
int[] intensityValues,
int duration
);

Play haptic around specific coordinates. Unlike PlayMotors function, which specifies the haptic intensity for each haptic actuator individually, this method specify the haptic intensity for particular coordinates.

When specifying haptic position, PlayMotors offers discrete control, while PlayPath is more continuous. PlayMotors assigns intensity to individual actuators, whereas PlayPath allows intensity specification for specific coordinates (between 0 and 1 for both X and Y axis), causing nearby actuators to vibrate accordingly.

You can put multiple coordinates with multiple intensities. Note that all actuators around these coordinates in the array will activate simultaneously (at the same time), not sequentially. Moreover, the size of all arrays must be the same.

By continuously calling this function while gradually changing the values, you can achieve the effect of a moving haptic point.

Frame 82.png

Parameters

  • int position: Type of haptic device.

    ValueDeviceSame as…
    0TactSuitPositionType.Vest
    1Tactosy for Arms(Left)PositionType.ForearmL
    2Tactosy for Arms(Right)PositionType.ForearmR
    3TactVisorPositionType.Head
    4Tactosy for Hands(Left)PositionType.HandL
    5Tactosy for Hands(Right)PositionType.HandR
    6Tactosy for Feet(Left)PositionType.FootL
    7Tactosy for Feet(Right)PositionType.FootR
    8TactGlove(Left)PositionType.GloveL
    9TactGlove(Right)PositionType.GloveR
  • float[] xValues: Assign X coordinate. Valid range for each value in the array is: [0.0f - 1.0f]

  • float[] yValues: Assign Y coordinate. Valid range for each value in the array is: [0.0f - 1.0f]

  • int[] intensityValues: Assign the length of the array by the number of coordinate. Values in the array means coordinates' intensity. Valid range for each value in the array is: [1 - 100]

  • int duration: The duration of haptic, measured in millisecond. Greater than or equal to 100 is recommended.

Returns

Request ID. You can use the request ID to stop the haptic. It returns -1 if the return fails.

Example

using Bhaptics.SDK2;

/* Assumption:
- For TactSuit
- Coordinate with Intensity
- (X: 0.1, Y: 0.2) Intensity 40
- (X: 0.3, Y: 0.4) Intensity 80
- (X: 0.5, Y: 0.6) Intensity 10
- Duration 500ms(=0.5 second)
*/
public class TestHaptic : MonoBehaviour
{
public void OnCall()
{
BhapticsLibrary.PlayPath(
(int)PositionType.Vest, // Device type
new float[] { 0.1f, 0.3f, 0.5f }, // X Coordinates
new float[] { 0.2f, 0.4f, 0.6f }, // Y Coordinates
new int[] { 40, 80, 10 }, // Intensities
500 // Duration
);
}
}

Static Functions — Others

using Bhaptics.SDK2;

public class TestHaptic : MonoBehaviour
{
public void OnCall()
{
int requestID = BhapticsLibrary.Play("TestHaptic");

if (BhapticsLibrary.IsPlayingByEventId("TestHaptic"))
{
BhapticsLibrary.StopByEventId("TestHaptic");
}

if (BhapticsLibrary.IsPlayingByRequestId(requestID))
{
BhapticsLibrary.StopInt(requestID);
}

if (BhapticsLibrary.IsPlaying())
{
BhapticsLibrary.StopAll();
}
}
}

StopByEventId

public static bool StopByEventId(string eventId)

Stop the haptic event by Event ID. Returns whether the stop was successful.

StopInt

public static bool StopInt(int requestId)

Stop the Haptic Event by using the Request ID from the return of the function that executes the haptic. Returns whether the stop was successful.

StopAll

public static bool StopAll()

Stops all haptic currently playing. Returns whether the stop was successful.

IsPlaying

public static bool IsPlaying()

Check if any haptic is currently playing.

IsPlayingByEventId

public static bool IsPlayingByEventId(string eventId)

Check if the haptic event for this Event ID is currently playing.

IsPlayingByRequestId

public static bool IsPlayingByRequestId(int requestId)

Check if the haptic event for this Request ID is currently playing.