Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haptics #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
using UnityEngine;
using UnityEngine.XR;

namespace BIMOS
{
[AddComponentMenu("BIMOS/Grabs/Grab (Auto)")]
public class AutoGrab : Grab
{
private InputDevice _leftDevice;
private InputDevice _rightDevice;

private void InitializeHapticDevices()
{
_leftDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
_rightDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
}

private void Start()
{
InitializeHapticDevices();
}

public override void AlignHand(Hand hand)
{
Vector3 handTargetPosition = GetComponent<Collider>().ClosestPoint(hand.PalmTransform.position);
Expand All @@ -22,13 +37,29 @@ public override void AlignHand(Hand hand)
if (hand.IsLeftHand)
crossed *= -1f;
hand.PhysicsHandTransform.rotation = Quaternion.LookRotation(-crossed, -projected);
hand.PhysicsHandTransform.position += hit.normal * 0.02f; // Moves hand out of collider
hand.PhysicsHandTransform.position += hit.normal * 0.02f;
}

hand.PhysicsHandTransform.position = hand.PhysicsHandTransform.TransformPoint(hand.PalmTransform.InverseTransformPoint(hand.PhysicsHandTransform.position));
hand.PhysicsHandTransform.rotation = hand.PhysicsHandTransform.rotation * Quaternion.Inverse(hand.PalmTransform.rotation) * hand.PhysicsHandTransform.rotation;
}

public override void OnGrab(Hand hand)
{
base.OnGrab(hand);

ProvideHapticFeedback(hand);
}

private void ProvideHapticFeedback(Hand hand)
{
InputDevice device = hand.IsLeftHand ? _leftDevice : _rightDevice;
if (device.isValid)
{
device.SendHapticImpulse(0, 10f, 0.7f);
}
}

public override void IgnoreCollision(Hand hand, bool ignore) { }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.XR;

namespace BIMOS
{
Expand All @@ -21,6 +22,9 @@ public class Grab : MonoBehaviour
[HideInInspector]
public Collider Collider;

private UnityEngine.XR.InputDevice _leftDevice;
private UnityEngine.XR.InputDevice _rightDevice;

private void OnEnable()
{
_body = Utilities.GetBody(transform, out _rigidBody, out _articulationBody);
Expand All @@ -37,6 +41,10 @@ private void OnEnable()
return;

CreateCollider();

// Initialize Input Devices for Haptic Feedback (controllers)
_leftDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
_rightDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
}

public virtual void CreateCollider()
Expand All @@ -47,15 +55,15 @@ public virtual void CreateCollider()
Collider = collider;
}

public virtual float CalculateRank(Transform handTransform) //Returned when in player grab range
public virtual float CalculateRank(Transform handTransform)
{
if (Collider is MeshCollider)
return 1f/1000f;
return 1f / 1000f;

return 1f / Vector3.Distance(handTransform.position, Collider.ClosestPoint(handTransform.position)); //Reciprocal of distance from hand to grab
return 1f / Vector3.Distance(handTransform.position, Collider.ClosestPoint(handTransform.position));
}

public virtual void OnGrab(Hand hand) //Triggered when player grabs the grab
public virtual void OnGrab(Hand hand)
{
hand.CurrentGrab = this;

Expand All @@ -64,7 +72,7 @@ public virtual void OnGrab(Hand hand) //Triggered when player grabs the grab
else
RightHand = hand;

hand.GrabHandler.ApplyGrabPose(HandPose); //Use the hand pose attached
hand.GrabHandler.ApplyGrabPose(HandPose);

AlignHand(hand);
CreateGrabJoint(hand);
Expand All @@ -83,6 +91,18 @@ public virtual void OnGrab(Hand hand) //Triggered when player grabs the grab
}

GetComponent<Interactable>()?.OnGrab();

ProvideHapticFeedback(hand);
}

private void ProvideHapticFeedback(Hand hand)
{
UnityEngine.XR.InputDevice device = hand.IsLeftHand ? _leftDevice : _rightDevice;

if (device.isValid)
{
device.SendHapticImpulse(0, 10f, 0.7f);
}
}

public virtual void IgnoreCollision(Hand hand, bool ignore)
Expand All @@ -103,7 +123,7 @@ private void CreateGrabJoint(Hand hand)
grabJoint.connectedArticulationBody = _articulationBody;
}

public void OnRelease(Hand hand, bool toggleGrabs) //Triggered when player releases the grab
public void OnRelease(Hand hand, bool toggleGrabs)
{
if (!hand)
return;
Expand Down Expand Up @@ -141,8 +161,8 @@ public virtual void DestroyGrabJoint(Hand hand)
if (!hand)
return;

FixedJoint grabJoint = hand.PhysicsHandTransform.GetComponent<FixedJoint>(); //Gets the grab joint
Destroy(grabJoint); //Deletes the joint, letting it go
FixedJoint grabJoint = hand.PhysicsHandTransform.GetComponent<FixedJoint>();
Destroy(grabJoint);

IgnoreCollision(hand, false);

Expand All @@ -156,4 +176,4 @@ private void OnDisable()
OnRelease(RightHand, false);
}
}
}
}
3 changes: 3 additions & 0 deletions Haptics PACKAGE AND PISTOL SCRIPT/HOW TO INSTALL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GO TO THE DIRECT OF THE GRABTYPE FOLDER DELETE IT AND REPLACE WITH THIS ONE>

DRAG THE UNITY PACKAGE INTO THE PROJECT AND LET IT OVERIDE YOUR PISTOL SCRIPT>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using UnityEngine;
using UnityEngine.XR;

namespace BIMOS
{
[AddComponentMenu("BIMOS/Grabs/Grab (Auto)")]
public class AutoGrab : Grab
{
private InputDevice _leftDevice;
private InputDevice _rightDevice;

private void InitializeHapticDevices()
{
_leftDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
_rightDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
}

private void Start()
{
InitializeHapticDevices();
}

public override void AlignHand(Hand hand)
{
Vector3 handTargetPosition = GetComponent<Collider>().ClosestPoint(hand.PalmTransform.position);
Vector3 handToTargetDirection = (handTargetPosition - hand.PalmTransform.position).normalized;

Ray ray = new Ray(hand.PalmTransform.position, handToTargetDirection);
RaycastHit hit;

if (GetComponent<Collider>().Raycast(ray, out hit, 10f))
{
Vector3 projected = Vector3.ProjectOnPlane(-hand.PalmTransform.up, hit.normal).normalized;
Debug.DrawRay(hit.point, projected, Color.blue, 5f);
hand.PhysicsHandTransform.position = handTargetPosition;
Vector3 crossed = Vector3.Cross(hit.normal, projected).normalized;
if (hand.IsLeftHand)
crossed *= -1f;
hand.PhysicsHandTransform.rotation = Quaternion.LookRotation(-crossed, -projected);
hand.PhysicsHandTransform.position += hit.normal * 0.02f;
}

hand.PhysicsHandTransform.position = hand.PhysicsHandTransform.TransformPoint(hand.PalmTransform.InverseTransformPoint(hand.PhysicsHandTransform.position));
hand.PhysicsHandTransform.rotation = hand.PhysicsHandTransform.rotation * Quaternion.Inverse(hand.PalmTransform.rotation) * hand.PhysicsHandTransform.rotation;
}

public override void OnGrab(Hand hand)
{
base.OnGrab(hand);

ProvideHapticFeedback(hand);
}

private void ProvideHapticFeedback(Hand hand)
{
InputDevice device = hand.IsLeftHand ? _leftDevice : _rightDevice;
if (device.isValid)
{
device.SendHapticImpulse(0, 10f, 0.7f);
}
}

public override void IgnoreCollision(Hand hand, bool ignore) { }
}
}

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

Loading