Refactor w.r.t. hands

This commit is contained in:
David Rowe 2014-09-10 23:17:33 -07:00
parent c7b34b4f54
commit 1f159a5ce5

View file

@ -13,15 +13,12 @@
var leapHands = (function () {
var leftHand,
rightHand,
var hands,
NUM_HANDS = 2, // 0 = left; 1 = right
fingers,
NUM_FINGERS = 5, // 0 = thumb; ...; 4 = pinky
THUMB = 0,
leftHandFingers,
rightHandFingers,
NUM_FINGER_JOINTS = 3, // 0 = metacarpal(hand)-proximal(finger) joint; ...; 2 = intermediate-distal(tip) joint
leftHandInactiveCount,
rightHandInactiveCount,
MAX_HAND_INACTIVE_COUNT = 20;
function printSkeletonJointNames() {
@ -114,8 +111,13 @@ var leapHands = (function () {
// TODO: Leap Motion controller joint naming doesn't match up with skeleton joint naming; numbers are out by 1.
leftHand = Controller.createInputController("Spatial", "joint_L_hand");
leftHandFingers = [
hands = [
{ controller: Controller.createInputController("Spatial", "joint_L_hand"), inactiveCount: 0 },
{ controller: Controller.createInputController("Spatial", "joint_R_hand"), inactiveCount: 0 }
];
fingers = [ {}, {} ];
fingers[0] = [
[
{ jointName: "LeftHandThumb1", controller: Controller.createInputController("Spatial", "joint_L_thumb2") },
{ jointName: "LeftHandThumb2", controller: Controller.createInputController("Spatial", "joint_L_thumb3") },
@ -142,10 +144,7 @@ var leapHands = (function () {
{ jointName: "LeftHandPinky3", controller: Controller.createInputController("Spatial", "joint_L_pinky4") }
]
];
leftHandInactiveCount = MAX_HAND_INACTIVE_COUNT;
rightHand = Controller.createInputController("Spatial", "joint_R_hand");
rightHandFingers = [
fingers[1] = [
[
{ jointName: "RightHandThumb1", controller: Controller.createInputController("Spatial", "joint_R_thumb2") },
{ jointName: "RightHandThumb2", controller: Controller.createInputController("Spatial", "joint_R_thumb3") },
@ -172,104 +171,82 @@ var leapHands = (function () {
{ jointName: "RightHandPinky3", controller: Controller.createInputController("Spatial", "joint_R_pinky4") }
]
];
rightHandInactiveCount = MAX_HAND_INACTIVE_COUNT;
}
function moveHands() {
var i,
var h,
i,
j,
side,
locRotation;
if (leftHand.isActive()) {
leftHandInactiveCount = 0;
for (h = 0; h < NUM_HANDS; h += 1) {
side = h === 0 ? -1.0 : 1.0;
// Fixed hand location for starters ...
MyAvatar.setJointData("LeftArm", Quat.fromPitchYawRollDegrees(90.0, 0.0, -90.0));
MyAvatar.setJointData("LeftForeArm", Quat.fromPitchYawRollDegrees(90.0, 0.0, 180.0));
MyAvatar.setJointData("LeftHand", Quat.fromPitchYawRollDegrees(0.0, 0.0, 0.0));
if (hands[h].controller.isActive()) {
// Finger joints ...
// TODO: 2.0 * scale factors should not be necessary; Leap Motion controller code needs investigating.
for (i = 0; i < NUM_FINGERS; i += 1) {
for (j = 0; j < NUM_FINGER_JOINTS; j += 1) {
if (leftHandFingers[i][j].controller !== null) {
locRotation = leftHandFingers[i][j].controller.getLocRotation();
if (i === THUMB) {
MyAvatar.setJointData(leftHandFingers[i][j].jointName,
Quat.fromPitchYawRollRadians(2.0 * -locRotation.y, 2.0 * -locRotation.z, 2.0 * locRotation.x));
} else {
MyAvatar.setJointData(leftHandFingers[i][j].jointName,
Quat.fromPitchYawRollRadians(2.0 * -locRotation.x, 0.0, 2.0 * -locRotation.y));
// Fixed hand location for starters ...
if (h === 0) {
MyAvatar.setJointData("LeftArm", Quat.fromPitchYawRollDegrees(90.0, 0.0, -90.0));
MyAvatar.setJointData("LeftForeArm", Quat.fromPitchYawRollDegrees(90.0, 0.0, 180.0));
MyAvatar.setJointData("LeftHand", Quat.fromPitchYawRollDegrees(0.0, 0.0, 0.0));
} else {
MyAvatar.setJointData("RightArm", Quat.fromPitchYawRollDegrees(90.0, 0.0, 90.0));
MyAvatar.setJointData("RightForeArm", Quat.fromPitchYawRollDegrees(90.0, 0.0, 180.0));
MyAvatar.setJointData("RightHand", Quat.fromPitchYawRollDegrees(0.0, 0.0, 0.0));
}
// Finger joints ...
// TODO: 2.0 * scale factors should not be necessary; Leap Motion controller code needs investigating.
for (i = 0; i < NUM_FINGERS; i += 1) {
for (j = 0; j < NUM_FINGER_JOINTS; j += 1) {
if (fingers[h][i][j].controller !== null) {
locRotation = fingers[h][i][j].controller.getLocRotation();
if (i === THUMB) {
MyAvatar.setJointData(fingers[h][i][j].jointName,
Quat.fromPitchYawRollRadians(2.0 * side * locRotation.y, 2.0 * -locRotation.z,
2.0 * side * -locRotation.x));
} else {
MyAvatar.setJointData(fingers[h][i][j].jointName,
Quat.fromPitchYawRollRadians(2.0 * -locRotation.x, 0.0, 2.0 * -locRotation.y));
}
}
}
}
}
leftHandInactiveCount = 0;
} else {
leftHandInactiveCount += 1;
hands[h].inactiveCount = 0;
if (leftHandInactiveCount === MAX_HAND_INACTIVE_COUNT) {
MyAvatar.clearJointData("LeftHand");
MyAvatar.clearJointData("LeftForeArm");
MyAvatar.clearJointData("LeftArm");
}
}
} else {
if (rightHand.isActive()) {
hands[h].inactiveCount += 1;
// Fixed hand location for starters ...
MyAvatar.setJointData("RightArm", Quat.fromPitchYawRollDegrees(90.0, 0.0, 90.0));
MyAvatar.setJointData("RightForeArm", Quat.fromPitchYawRollDegrees(90.0, 0.0, 180.0));
MyAvatar.setJointData("RightHand", Quat.fromPitchYawRollDegrees(0.0, 0.0, 0.0));
// Finger joints ...
// TODO: 2.0 * scale factors should not be necessary; Leap Motion controller code needs investigating.
for (i = 0; i < NUM_FINGERS; i += 1) {
for (j = 0; j < NUM_FINGER_JOINTS; j += 1) {
if (rightHandFingers[i][j].controller !== null) {
locRotation = rightHandFingers[i][j].controller.getLocRotation();
if (i === THUMB) {
MyAvatar.setJointData(rightHandFingers[i][j].jointName,
Quat.fromPitchYawRollRadians(2.0 * locRotation.y, 2.0 * -locRotation.z, 2.0 * -locRotation.x));
} else {
MyAvatar.setJointData(rightHandFingers[i][j].jointName,
Quat.fromPitchYawRollRadians(2.0 * -locRotation.x, 0.0, 2.0 * -locRotation.y));
}
if (hands[h].inactiveCount === MAX_HAND_INACTIVE_COUNT) {
if (h === 0) {
MyAvatar.clearJointData("LeftHand");
MyAvatar.clearJointData("LeftForeArm");
MyAvatar.clearJointData("LeftArm");
} else {
MyAvatar.clearJointData("RightHand");
MyAvatar.clearJointData("RightForeArm");
MyAvatar.clearJointData("RightArm");
}
}
}
rightHandInactiveCount = 0;
} else {
rightHandInactiveCount += 1;
if (rightHandInactiveCount === MAX_HAND_INACTIVE_COUNT) {
MyAvatar.clearJointData("RightHand");
MyAvatar.clearJointData("RightForeArm");
MyAvatar.clearJointData("RightArm");
}
}
}
function tearDown() {
var i,
var h,
i,
j;
Controller.releaseInputController(leftHand);
for (i = 0; i < NUM_FINGERS; i += 1) {
for (j = 0; j < NUM_FINGER_JOINTS; j += 1) {
if (leftHandFingers[i][j].controller !== null) {
Controller.releaseInputController(leftHandFingers[i][j].controller);
}
}
}
Controller.releaseInputController(rightHand);
for (i = 0; i < NUM_FINGERS; i += 1) {
for (j = 0; j < NUM_FINGER_JOINTS; j += 1) {
if (rightHandFingers[i][j].controller !== null) {
Controller.releaseInputController(rightHandFingers[i][j].controller);
for (h = 0; h < NUM_HANDS; h += 1) {
Controller.releaseInputController(hands[h].controller);
for (i = 0; i < NUM_FINGERS; i += 1) {
for (j = 0; j < NUM_FINGER_JOINTS; j += 1) {
if (fingers[h][i][j].controller !== null) {
Controller.releaseInputController(fingers[h][i][j].controller);
}
}
}
}