- Home /
how to implement oculus vr haptic feedback
I want to implement haptic feedback for our oculus vr game, but I don't understand how. I went to the documentation which was confusing. A lot of those types aren't recognised. The following code sample is provided by the document.
uint8_t amplitude = (uint8_t)round(handTrigger[t] * 255);
result = ovr_GetControllerVibrationState(Session, touchController[t], &state);
if (result != ovrSuccess || state.SamplesQueued >= kLowLatencyBufferSizeInSamples)
{
DefaultChannel.LogWarningF("%s Haptics skipped. Queue size %d", kTouchStr[t], state.SamplesQueued);
continue;
}
for (int32_t i = 0; i < kLowLatencyBufferSizeInSamples; ++i)
samples.push_back(amplitude);
if (samples.size() > 0)
{
ovrHapticsBuffer buffer;
buffer.SubmitMode = ovrHapticsBufferSubmit_Enqueue;
buffer.SamplesCount = (uint32_t)samples.size();
buffer.Samples = samples.data();
result = ovr_SubmitControllerVibration(Session, touchController[t], &buffer);
if (result != ovrSuccess)
{
// Something bad happened
DefaultChannel.LogErrorF("%s: Haptics submit failed %d", kTouchStr[t], result);
}
}
Could someone please explain to me how to implement this?
Answer by HomerDalors · Jul 25, 2017 at 11:17 AM
This snippet might help:
public enum VibrationForce
{
Light,
Medium,
Hard,
}
public class OculusHaptics : MonoBehaviour
{
[SerializeField]
OVRInput.Controller controllerMask;
private OVRHapticsClip clipLight;
private OVRHapticsClip clipMedium;
private OVRHapticsClip clipHard;
public float lowViveHaptics { get; private set; }
public float mediumViveHaptics { get; private set; }
public float hardViveHaptics { get; private set; }
private void Start()
{
InitializeOVRHaptics();
}
private void InitializeOVRHaptics()
{
int cnt = 10;
clipLight = new OVRHapticsClip(cnt);
clipMedium = new OVRHapticsClip(cnt);
clipHard = new OVRHapticsClip(cnt);
for (int i = 0; i < cnt; i++)
{
clipLight.Samples[i] = i % 2 == 0 ? (byte)0 : (byte)45;
clipMedium.Samples[i] = i % 2 == 0 ? (byte)0 : (byte)100;
clipHard.Samples[i] = i % 2 == 0 ? (byte)0 : (byte)180;
}
clipLight = new OVRHapticsClip(clipLight.Samples, clipLight.Samples.Length);
clipMedium = new OVRHapticsClip(clipMedium.Samples, clipMedium.Samples.Length);
clipHard = new OVRHapticsClip(clipHard.Samples, clipHard.Samples.Length);
}
void OnEnable()
{
InitializeOVRHaptics();
}
public void Vibrate(VibrationForce vibrationForce)
{
var channel = OVRHaptics.RightChannel;
if (controllerMask == OVRInput.Controller.LTouch)
channel = OVRHaptics.LeftChannel;
switch (vibrationForce)
{
case VibrationForce.Light:
channel.Preempt(clipLight);
break;
case VibrationForce.Medium:
channel.Preempt(clipMedium);
break;
case VibrationForce.Hard:
channel.Preempt(clipHard);
break;
}
}
public IEnumerator VibrateTime(VibrationForce force, float time)
{
forcedHaptic = true;
var channel = OVRHaptics.RightChannel;
if (controllerMask == OVRInput.Controller.LTouch)
channel = OVRHaptics.LeftChannel;
for (float t = 0; t <= time; t += Time.deltaTime)
{
switch (force)
{
case VibrationForce.Light:
channel.Queue(clipLight);
break;
case VibrationForce.Medium:
channel.Queue(clipMedium);
break;
case VibrationForce.Hard:
channel.Queue(clipHard);
break;
}
}
yield return new WaitForSeconds(time);
channel.Clear();
forcedHaptic = false;
yield return null;
}
Attach it to the Controller Anchor and set it accordingly (Ltouch or RTouch), then just call the methods Vibrate or the coroutine VibrateTime.
Thank you for answering! This looks like really good, but I can't test it right now. In two weeks I get my hands on a Oculus again, I will test it then and come back here, if that's okey.
Thanks for sharing! They should add this to the docs as an example, this is something I wouldn't have figured out by myself. I got one point, the bool forcedHaptic was never declared.
Opss, sorry. the forcedHaptic bool was an artifact in our project. it is irrelevant in this snippet sorry.
Hello, using this code I get a Divison by Zero exception:
IndexOutOfRangeException: Array index is out of range.
VibrationBehaviour.InitializeOVRHaptics () (at Assets/VibrationBehaviour.cs:42)
VibrationBehaviour.OnEnable () (at Assets/VibrationBehaviour.cs:55)
$$anonymous$$y code line 42 corresponds to Line 39 in the code posted above.
Any idea why this happens?
Best, Stephan