Skip to content

Instantly share code, notes, and snippets.

@PhilipRosedale
Last active May 5, 2017 16:44
Show Gist options
  • Save PhilipRosedale/33e1e9666b95f1bfc2fd35238b6a97b4 to your computer and use it in GitHub Desktop.
Save PhilipRosedale/33e1e9666b95f1bfc2fd35238b6a97b4 to your computer and use it in GitHub Desktop.
//
// ControllerHandshake.js
//
// Created by Philip Rosedale on April 28, 2017
// Copyright 2016 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// This script makes the left and right hands 'mirror' each other, as a function of how much their triggers are pulled.
//
Script.include("/~/system/libraries/Xform.js");
var MAPPING_LEFT = "left";
var MAPPING_RIGHT = "right";
var mappingLeft = Controller.newMapping(MAPPING_LEFT);
var mappingRight = Controller.newMapping(MAPPING_RIGHT);
var MAX_HANDSHAKE_AVATAR_DISTANCE = 2.0;
var leftHandshakeJoint = "LeftHandIndex4";
var rightHandshakeJoint = "RightHandIndex4";
var targetLeftHand, targetRightHand;
function worldToAvatar(vec) {
worldToAvatarXform = new Xform(MyAvatar.orientation, MyAvatar.position).inv();
return worldToAvatarXform.xformPoint(vec);
}
function getClosestHands() {
//
// Look at the other avatars, find the hand closest to this one
//
var closest = MAX_HANDSHAKE_AVATAR_DISTANCE;
var found = false;
var closestAvatar;
var avatars = AvatarList.getAvatarIdentifiers();
avatars.forEach(function (id) {
var avatar = AvatarList.getAvatar(id);
if (MyAvatar.sessionUUID !== avatar.sessionUUID) {
var hand = avatar.position;
var distance = Vec3.distance(MyAvatar.position, avatar.position);
if (distance < closest) {
closest = distance;
closestAvatar = avatar;
found = true;
}
}
});
if (found) {
var worldToAvatarXform = new Xform(closestAvatar.orientation, closestAvatar.position);
targetLeftHand = worldToAvatar(worldToAvatarXform.xformPoint(closestAvatar.getAbsoluteJointTranslationInObjectFrame(-4)));
//targetLeftHand = worldToAvatar(closestAvatar.getJointPosition(leftHandshakeJoint));
targetRightHand = worldToAvatar(worldToAvatarXform.xformPoint(closestAvatar.getAbsoluteJointTranslationInObjectFrame(-3)));
}
};
/*
function updateHaptics() {
var leftTrigger = Controller.getValue(Controller.Standard.LT);
var rightTrigger = Controller.getValue(Controller.Standard.RT);
if (leftTrigger > 0.8) {
}
}*/
mappingLeft.from(function() {
if (Controller.Hardware.OculusTouch) {
var leftController = Controller.getPoseValue(Controller.Hardware.OculusTouch.LeftHand);
}
else {
var leftController = Controller.getPoseValue(Controller.Hardware.Vive.LeftHand);
}
var trigger = Controller.getValue(Controller.Standard.LT);
var pose = {
translation: Vec3.mix(leftController.translation, targetLeftHand, trigger/2),
rotation: leftController.rotation,
velocity: leftController.velocity,
angularVelocity: leftController.angularVelocity
};
return pose;
}).debug(true).to(Controller.Standard.LeftHand);
mappingRight.from(function() {
if (Controller.Hardware.OculusTouch) {
var rightController = Controller.getPoseValue(Controller.Hardware.OculusTouch.RightHand);
}
else {
var rightController = Controller.getPoseValue(Controller.Hardware.Vive.RightHand);
}
var trigger = Controller.getValue(Controller.Standard.RT);
var pose = {
translation: Vec3.mix(rightController.translation, targetRightHand, trigger/2),
rotation: rightController.rotation,
velocity: rightController.velocity,
angularVelocity: rightController.angularVelocity
};
return pose;
}).debug(true).to(Controller.Standard.RightHand);
Controller.enableMapping(MAPPING_LEFT);
Controller.enableMapping(MAPPING_RIGHT);
Script.update.connect(function() {
getClosestHands();
//updateHaptics();
});
Script.scriptEnding.connect(function(){
mappingLeft.disable();
mappingRight.disable();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment