More consistent tablet angle when first creating the tablet.

The tablet should more constantly be placed above your hand, while facing your head.
It fixes the issue where the tablet would appear almost horizontal when your hand was close to your HMD and at eye level.
This commit is contained in:
Anthony J. Thibault 2017-02-21 16:01:46 -08:00
parent 5c782deb4d
commit 4866be1f52

View file

@ -49,24 +49,30 @@ function calcSpawnInfo(hand, height) {
var handController = getControllerWorldLocation(hand, true); var handController = getControllerWorldLocation(hand, true);
var controllerPosition = handController.position; var controllerPosition = handController.position;
// compute the angle of the chord with length (height / 2) // base of the tablet is slightly above controller position
var theta = Math.asin(height / (2 * Vec3.distance(headPos, controllerPosition))); var TABLET_BASE_DISPLACEMENT = {x: 0, y: 0.1, z: 0};
var tabletBase = Vec3.sum(controllerPosition, TABLET_BASE_DISPLACEMENT);
// then we can use this angle to rotate the vector between the HMD position and the center of the tablet. var d = Vec3.subtract(headPos, tabletBase);
// this vector, u, will become our new look at direction. var theta = Math.acos(d.y / Vec3.length(d));
var d = Vec3.normalize(Vec3.subtract(headPos, controllerPosition)); d.y = 0;
if (Vec3.length(d) < 0.0001) {
d = {x: 1, y: 0, z: 0};
} else {
d = Vec3.normalize(d);
}
var w = Vec3.normalize(Vec3.cross(Y_AXIS, d)); var w = Vec3.normalize(Vec3.cross(Y_AXIS, d));
var q = Quat.angleAxis(theta * (180 / Math.PI), w); var ANGLE_OFFSET = 25;
var q = Quat.angleAxis(theta * (180 / Math.PI) - (90 - ANGLE_OFFSET), w);
var u = Vec3.multiplyQbyV(q, d); var u = Vec3.multiplyQbyV(q, d);
// use u to compute a full lookAt quaternion. // use u to compute a full lookAt quaternion.
var lookAtRot = Quat.lookAt(controllerPosition, Vec3.sum(controllerPosition, u), Y_AXIS); var lookAtRot = Quat.lookAt(tabletBase, Vec3.sum(tabletBase, u), Y_AXIS);
var yDisplacement = (height / 2);
// adjust the tablet position by a small amount.
var yDisplacement = (height / 2) + 0.1;
var zDisplacement = 0.05; var zDisplacement = 0.05;
var tabletOffset = Vec3.multiplyQbyV(lookAtRot, {x: 0, y: yDisplacement, z: zDisplacement}); var tabletOffset = Vec3.multiplyQbyV(lookAtRot, {x: 0, y: yDisplacement, z: zDisplacement});
finalPosition = Vec3.sum(controllerPosition, tabletOffset); finalPosition = Vec3.sum(tabletBase, tabletOffset);
return { return {
position: finalPosition, position: finalPosition,
rotation: lookAtRot rotation: lookAtRot