content/hifi-content/liv/dev/Bingo/bingoCard.js
2022-02-14 02:04:11 +01:00

156 lines
6.8 KiB
JavaScript

//
// bingoCard.js
//
// created by Rebecca Stankus on 10/10/18
// Copyright 2018 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
//
function exponentialSmoothing(target, current) {
var smoothingConstant = 0.75;
return target * (1 - smoothingConstant) + current * smoothingConstant;
}
(function() {
var HAND = {LEFT: 0, RIGHT: 1};
var HAND_OFFSET_Y = 0.1;
var _this;
var currentHand;
var mouseEquipAnimationHandler;
var offsetMultiplier = 0.8;
var previousLeftYPosition = 0;
var previousLeftXRotation = 0;
var previousLeftZRotation = 0;
var previousRightYPosition = 0;
var previousRightXRotation = 0;
var previousRightZRotation = 0;
function BingoCard() {
_this = this;
}
BingoCard.prototype = {
preload: function(entityID) {
// print("preload");
_this.entityID = entityID;
},
startEquip: function(id, params) {
// print("start equip");
currentHand = params[0] === "left" ? 0 : 1;
Entities.editEntity(_this.entityID, {
lifetime: -1
});
if (!HMD.active) {
_this.addMouseEquipAnimation();
}
},
releaseEquip: function(id, params) {
// print("release equip");
currentHand = null;
_this.removeMouseEquipAnimation();
},
addMouseEquipAnimation: function() {
_this.removeMouseEquipAnimation();
if (currentHand === HAND.LEFT) {
// print("adding LEFT animation state handler");
mouseEquipAnimationHandler = MyAvatar.addAnimationStateHandler(_this.leftHandMouseEquipAnimation, []);
} else if (currentHand === HAND.RIGHT) {
// print("adding RIGHT animation state handler");
mouseEquipAnimationHandler = MyAvatar.addAnimationStateHandler(_this.rightHandMouseEquipAnimation, []);
}
},
removeMouseEquipAnimation: function() {
if (mouseEquipAnimationHandler) {
mouseEquipAnimationHandler = MyAvatar.removeAnimationStateHandler(mouseEquipAnimationHandler);
}
},
leftHandMouseEquipAnimation: function() {
var result = {};
result.leftHandType = 0;
var leftHandPosition = MyAvatar.getJointPosition("LeftHand");
var leftShoulderPosition = MyAvatar.getJointPosition("LeftShoulder");
var shoulderToHandDistance = Vec3.distance(leftHandPosition, leftShoulderPosition);
var cameraForward = Quat.getForward(Camera.orientation);
var newForward = Vec3.multiply(cameraForward, shoulderToHandDistance);
var newLeftHandPosition = Vec3.sum(leftShoulderPosition, newForward);
var newLeftHandPositionAvatarFrame = Vec3.subtract(newLeftHandPosition, MyAvatar.position);
var headIndex = MyAvatar.getJointIndex("Head");
var offset = 0.5;
if (headIndex) {
offset = offsetMultiplier* MyAvatar.getAbsoluteJointTranslationInObjectFrame(headIndex).y;
}
result.leftHandPosition = Vec3.multiply(offset, {x: 1.2, y: 0, z: 1.5});
var yPosition = exponentialSmoothing(newLeftHandPositionAvatarFrame.y, previousLeftYPosition);
result.leftHandPosition.y = yPosition + HAND_OFFSET_Y;
previousLeftYPosition = yPosition;
var leftHandPositionNew = Vec3.sum(MyAvatar.position, result.leftHandPosition);
var rotation = Quat.lookAtSimple(leftHandPositionNew, leftShoulderPosition);
var rotationAngles = Quat.safeEulerAngles(rotation);
var xRotation = exponentialSmoothing(rotationAngles.x, previousLeftXRotation);
var zRotation = exponentialSmoothing(rotationAngles.z, previousLeftZRotation);
var newRotation = Quat.fromPitchYawRollDegrees(rotationAngles.x, 0, rotationAngles.z);
previousLeftXRotation = xRotation;
previousLeftZRotation = zRotation;
result.leftHandRotation = Quat.multiply(newRotation, Quat.fromPitchYawRollDegrees(80, -10, -90));
return result;
},
rightHandMouseEquipAnimation: function() {
var result = {};
result.rightHandType = 0;
var rightHandPosition = MyAvatar.getJointPosition("RightHand");
var rightShoulderPosition = MyAvatar.getJointPosition("RightShoulder");
var shoulderToHandDistance = Vec3.distance(rightHandPosition, rightShoulderPosition);
var cameraForward = Quat.getForward(Camera.orientation);
var newForward = Vec3.multiply(cameraForward, shoulderToHandDistance);
var newRightHandPosition = Vec3.sum(rightShoulderPosition, newForward);
var newRightHandPositionAvatarFrame = Vec3.subtract(newRightHandPosition, MyAvatar.position);
var headIndex = MyAvatar.getJointIndex("Head");
var offset = 0.5;
if (headIndex) {
offset = offsetMultiplier * MyAvatar.getAbsoluteJointTranslationInObjectFrame(headIndex).y;
}
result.rightHandPosition = Vec3.multiply(offset, {x: -1.2, y: 0, z: 1.5});
var yPosition = exponentialSmoothing(newRightHandPositionAvatarFrame.y, previousRightYPosition);
result.rightHandPosition.y = yPosition + HAND_OFFSET_Y;
previousRightYPosition = yPosition;
var rightHandPositionNew = Vec3.sum(MyAvatar.position, result.rightHandPosition);
var rotation = Quat.lookAtSimple(rightHandPositionNew, rightShoulderPosition);
var rotationAngles = Quat.safeEulerAngles(rotation);
var xRotation = exponentialSmoothing(rotationAngles.x, previousRightXRotation);
var zRotation = exponentialSmoothing(rotationAngles.z, previousRightZRotation);
var newRotation = Quat.fromPitchYawRollDegrees(rotationAngles.x, 0, rotationAngles.z);
previousRightXRotation = xRotation;
previousRightZRotation = zRotation;
result.rightHandRotation = Quat.multiply(newRotation, Quat.fromPitchYawRollDegrees(80, -5, 90));
return result;
},
unload: function() {
this.removeMouseEquipAnimation();
}
};
return new BingoCard();
});