Skip to content

Commit

Permalink
feat(Controller): move haptic feedback to controller actions
Browse files Browse the repository at this point in the history
The `SteamVR_ControllerActions` is a more appropriate place to initiate
an effect on the controller as it calls an action for the controller to
do such as vibrate or show/hide it's model.

A new example scene has been added to demonstrate the haptic rumble.
  • Loading branch information
thestonefox committed May 6, 2016
1 parent 422a7a9 commit e8e3bfe
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 17 deletions.
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Examples/016_Controller_HapticRumble.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Assets/Examples/Scripts/Breakable_Cube.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using UnityEngine;
using System.Collections;

public class Breakable_Cube : MonoBehaviour {
float breakForce = 150f;

void Start()
{
this.GetComponent<Rigidbody>().collisionDetectionMode = CollisionDetectionMode.Continuous;
}

void OnCollisionEnter(Collision collision)
{
if(collision.collider.name == "Sword" && collision.collider.GetComponent<Sword>().CollisionForce() > breakForce)
{
foreach(Transform face in this.GetComponentsInChildren<Transform>())
{
if (face.transform.name == this.transform.name) continue;
ExplodeFace(face);
}
Destroy(this.gameObject, 10f);
}
}

void ExplodeFace(Transform face)
{
face.transform.parent = null;
Rigidbody rb = face.gameObject.AddComponent<Rigidbody>();
rb.isKinematic = false;
rb.useGravity = true;
rb.AddExplosionForce(500f, Vector3.zero, 0f);
Destroy(face.gameObject, 10f);
}
}
12 changes: 12 additions & 0 deletions Assets/Examples/Scripts/Breakable_Cube.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Assets/Examples/Scripts/Sword.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using UnityEngine;
using System.Collections;

public class Sword : SteamVR_InteractableObject {
SteamVR_ControllerActions controllerActions;
Rigidbody rb;
float impactMagnifier = 100f;
float collisionForce = 0f;

public float CollisionForce()
{
return collisionForce;
}

public override void Grabbed(GameObject grabbingObject)
{
base.Grabbed(grabbingObject);
controllerActions = grabbingObject.GetComponent<SteamVR_ControllerActions>();
}

private void Awake()
{
rb = this.GetComponent<Rigidbody>();
rb.collisionDetectionMode = CollisionDetectionMode.Continuous;
}

private void OnCollisionEnter(Collision collision)
{
if (controllerActions && IsGrabbed())
{
collisionForce = collision.impulse.magnitude * impactMagnifier;
controllerActions.TriggerHapticPulse(40, (ushort)collisionForce);
}
}
}
12 changes: 12 additions & 0 deletions Assets/Examples/Scripts/Sword.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
using System.Collections;

public class SteamVR_ControllerActions : MonoBehaviour {
bool controllerVisible = true;
private bool controllerVisible = true;
private ushort hapticPulseStrength;
private int hapticPulseCountdown;

private uint controllerIndex;
private SteamVR_TrackedObject trackedController;
private SteamVR_Controller.Device device;
private ushort maxHapticVibration = 3999;

public bool IsControllerVisible()
{
Expand All @@ -17,4 +24,27 @@ public void ToggleControllerModel(bool on)
}
controllerVisible = on;
}

public void TriggerHapticPulse(int duration, ushort strength)
{
hapticPulseCountdown = duration;
hapticPulseStrength = (strength <= maxHapticVibration ? strength : maxHapticVibration);
}

private void Awake()
{
trackedController = GetComponent<SteamVR_TrackedObject>();
}

private void Update()
{
controllerIndex = (uint)trackedController.index;
device = SteamVR_Controller.Input((int)controllerIndex);

if (hapticPulseCountdown > 0)
{
device.TriggerHapticPulse(hapticPulseStrength);
hapticPulseCountdown -= 1;
}
}
}
15 changes: 0 additions & 15 deletions Assets/SteamVR_Unity_Toolkit/Scripts/SteamVR_ControllerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@ public enum ButtonAlias
private Vector2 touchpadAxis = Vector2.zero;
private Vector2 triggerAxis = Vector2.zero;

private ushort hapticPulseStrength;
private int hapticPulseCountdown;

public void TriggerHapticPulse(int duration, ushort strength)
{
hapticPulseCountdown = duration;
hapticPulseStrength = strength;
}

public virtual void OnTriggerClicked(ControllerClickedEventArgs e)
{
if (TriggerClicked != null)
Expand Down Expand Up @@ -278,12 +269,6 @@ void Update()
controllerIndex = (uint)trackedController.index;
device = SteamVR_Controller.Input((int)controllerIndex);

if (hapticPulseCountdown > 0)
{
device.TriggerHapticPulse(hapticPulseStrength);
hapticPulseCountdown -= 1;
}

Vector2 currentTriggerAxis = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger);
Vector2 currentTouchpadAxis = device.GetAxis();

Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,24 @@ The current available scripts are:
#### Controller Actions (SteamVR_ControllerActions)

The Controller Actions script provides helper methods to deal with
common controller actions. The following methods are available:
common controller actions. The following public methods are available:

* **IsControllerVisible():** returns true is the controller model
is visible, returns false if it is not visible.
* **ToggleControllerModel(bool on):** sets the visibility of the
controller model to the given boolean state. If true is passed
then the controller model is displayed, if false is passed then
the controller model is hidden.
* **TriggerHapticPulse(int duration, ushort strength):**
initiates the controller to begin vibrating for the given tick
duration provided in the first parameter at a vibration intensity
given as the strength parameter. The max strength that can be
provided is 3999, any number higher than that will be capped.

An example of the `SteamVR_ControllerActions` script can be viewed in
the scene `Examples/016_Controller_HapticRumble` which demonstrates
the ability to hide a controller model and make the controller
vibrate for a given length of time at a given intensity.

#### Controller Events (SteamVR_ControllerEvents)

Expand Down Expand Up @@ -812,6 +822,12 @@ The current examples are:
the trigger is depressed, the higher the car will jump.
* [View Example Tour on Youtube](https://www.youtube.com/watch?v=4J8abeLzH58)

* **016_Controller_HapticRumble:** A scene with a collection of
breakable boxes and a sword. The sword can be picked up and swung
at the boxes. The controller rumbles at an appropriate vibration
depending on how hard the sword hits the box. The box also breaks
apart if it is hit hard enough by the sword.

## Contributing

I'd love this to be a community effort, but as I'm just getting
Expand Down

0 comments on commit e8e3bfe

Please sign in to comment.