Created
January 11, 2019 00:10
-
-
Save MiladNazeri/fed0a5087c5f336071b5e8698d427fd0 to your computer and use it in GitHub Desktop.
#hifi #raycast Raycasting code for controllers and mouse button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lastMouseX; | |
var lastMouseY; | |
// Record the last mousePressEvent | |
function mousePressEvent(event) { | |
lastMouseX = event.x; | |
lastMouseY = event.y; | |
} | |
Controller.mousePressEvent.connect(mousePressEvent); | |
mapping.from(Controller.Hardware.Keyboard.LeftMouseButton).to(function (value) { | |
if (value === 1) { | |
return; | |
} | |
var pickRay = Camera.computePickRay(lastMouseX, lastMouseY); | |
var avatarIntersection = AvatarList.findRayIntersection(pickRay); | |
var uuid = avatarIntersection.avatarID; | |
if (uuid && uuid !== MyAvatar.sessionUUID) { | |
doSomethingWith(avatarIntersection.avatarID); | |
} | |
}); | |
// Utility function to get the controller offset | |
function getGrabPointSphereOffset(handController, ignoreSensorToWorldScale) { | |
var GRAB_POINT_SPHERE_OFFSET = { x: 0.04, y: 0.13, z: 0.039 }; // x = upward, y = forward, z = lateral | |
var offset = GRAB_POINT_SPHERE_OFFSET; | |
if (handController === Controller.Standard.LeftHand) { | |
offset = { | |
x: -GRAB_POINT_SPHERE_OFFSET.x, | |
y: GRAB_POINT_SPHERE_OFFSET.y, | |
z: GRAB_POINT_SPHERE_OFFSET.z | |
}; | |
} | |
if (ignoreSensorToWorldScale) { | |
return offset; | |
} else { | |
return Vec3.multiply(MyAvatar.sensorToWorldScale, offset); | |
} | |
} | |
// controllerWorldLocation is where the controller would be, in-world, with an added offset | |
function getControllerWorldLocation(handController, doOffset) { | |
var orientation; | |
var position; | |
var valid = false; | |
if (handController >= 0) { | |
var pose = Controller.getPoseValue(handController); | |
valid = pose.valid; | |
var controllerJointIndex; | |
if (pose.valid) { | |
if (handController === Controller.Standard.RightHand) { | |
controllerJointIndex = MyAvatar.getJointIndex("_CAMERA_RELATIVE_CONTROLLER_RIGHTHAND"); | |
} else { | |
controllerJointIndex = MyAvatar.getJointIndex("_CAMERA_RELATIVE_CONTROLLER_LEFTHAND"); | |
} | |
orientation = Quat.multiply(MyAvatar.orientation, MyAvatar.getAbsoluteJointRotationInObjectFrame(controllerJointIndex)); | |
position = Vec3.sum(MyAvatar.position, Vec3.multiplyQbyV(MyAvatar.orientation, MyAvatar.getAbsoluteJointTranslationInObjectFrame(controllerJointIndex))); | |
// add to the real position so the grab-point is out in front of the hand, a bit | |
if (doOffset) { | |
var offset = getGrabPointSphereOffset(handController); | |
position = Vec3.sum(position, Vec3.multiplyQbyV(orientation, offset)); | |
} | |
} else if (!HMD.isHandControllerAvailable()) { | |
// NOTE: keep this offset in sync with scripts/system/controllers/handControllerPointer.js:493 | |
var VERTICAL_HEAD_LASER_OFFSET = 0.1 * MyAvatar.sensorToWorldScale; | |
position = Vec3.sum(Camera.position, Vec3.multiplyQbyV(Camera.orientation, { x: 0, y: VERTICAL_HEAD_LASER_OFFSET, z: 0 })); | |
orientation = Quat.multiply(Camera.orientation, Quat.angleAxis(-90, { x: 1, y: 0, z: 0 })); | |
valid = true; | |
} | |
} | |
return { | |
position: position, | |
translation: position, | |
orientation: orientation, | |
rotation: orientation, | |
valid: valid | |
}; | |
} | |
// Utility code to handle which hand triggers | |
function getUUIDFromLaser(hand){ | |
hand = hand === Controller.Standard.LeftHand | |
? Controller.Standard.LeftHand | |
: Controller.Standard.RightHand; | |
var pose = getControllerWorldLocation(hand); | |
var start = pose.position; | |
var direction = Vec3.multiplyQbyV(pose.orientation, {x:0, y:1, z: 0}); | |
var avatarIntersection = AvatarList.findRayIntersection({origin:start, direction:direction}); | |
var uuid = avatarIntersection.avatarID; | |
return uuid; | |
} | |
mapping.from(Controller.Standard.LTClick).to(function (value) { | |
if (value === 0) { | |
return; | |
} | |
var uuid = getUUIDFromLaser(Controller.Standard.LeftHand); | |
if (uuid && uuid !== MyAvatar.sessionUUID) { | |
soloAvatar(uuid); | |
} | |
}); | |
mapping.from(Controller.Standard.RTClick).to(function (value) { | |
if (value === 0) { | |
return; | |
} | |
var uuid = getUUIDFromLaser(Controller.Standard.RightHand); | |
if (uuid && uuid !== MyAvatar.sessionUUID) { | |
soloAvatar(uuid); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment