rework handControllerGrab.js

This commit is contained in:
Seth Alves 2015-09-17 23:15:18 -07:00
parent 493819c013
commit 6d7b129b83

View file

@ -10,31 +10,22 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
Script.include("../libraries/utils.js"); Script.include("../libraries/utils.js");
var RIGHT_HAND = 1;
var LEFT_HAND = 0;
var GRAB_RADIUS = 0.3;
var RADIUS_FACTOR = 4; var RADIUS_FACTOR = 4;
var ZERO_VEC = {x: 0, y: 0, z: 0};
var RIGHT_HAND_CLICK = Controller.findAction("RIGHT_HAND_CLICK"); var NULL_ACTION_ID = "{00000000-0000-0000-000000000000}";
var rightTriggerAction = RIGHT_HAND_CLICK;
var GRAB_USER_DATA_KEY = "grabKey";
var LEFT_HAND_CLICK = Controller.findAction("LEFT_HAND_CLICK");
var leftTriggerAction = LEFT_HAND_CLICK;
var LIFETIME = 10;
var EXTRA_TIME = 5;
var POINTER_CHECK_TIME = 5000;
var ZERO_VEC = {
x: 0,
y: 0,
z: 0
}
var LINE_LENGTH = 500; var LINE_LENGTH = 500;
var THICK_LINE_WIDTH = 7;
var THIN_LINE_WIDTH = 2; var rightController = new controller(RIGHT_HAND, Controller.findAction("RIGHT_HAND_CLICK"));
var leftController = new controller(LEFT_HAND, Controller.findAction("LEFT_HAND_CLICK"));
var startTime = Date.now();
var LIFETIME = 10;
var NO_INTERSECT_COLOR = { var NO_INTERSECT_COLOR = {
red: 10, red: 10,
@ -47,35 +38,16 @@ var INTERSECT_COLOR = {
blue: 10 blue: 10
}; };
var GRAB_RADIUS = 0.3;
var GRAB_COLOR = { var STATE_SEARCHING = 0;
red: 250, var STATE_DISTANCE_HOLDING = 1;
green: 10, var STATE_CLOSE_GRABBING = 2;
blue: 250 var STATE_CONTINUE_CLOSE_GRABBING = 3;
}; var STATE_RELEASE = 4;
var SHOW_LINE_THRESHOLD = 0.2;
var DISTANCE_HOLD_THRESHOLD = 0.8;
var right4Action = 18; function controller(hand, triggerAction) {
var left4Action = 17;
var RIGHT = 1;
var LEFT = 0;
var rightController = new controller(RIGHT, rightTriggerAction, right4Action, "right");
var leftController = new controller(LEFT, leftTriggerAction, left4Action, "left");
var startTime = Date.now();
//Need to wait before calling these methods for some reason...
Script.setTimeout(function() {
rightController.checkPointer();
leftController.checkPointer();
}, 100)
function controller(side, triggerAction, pullAction, hand) {
this.hand = hand; this.hand = hand;
if (hand === "right") { if (this.hand === RIGHT_HAND) {
this.getHandPosition = MyAvatar.getRightPalmPosition; this.getHandPosition = MyAvatar.getRightPalmPosition;
this.getHandRotation = MyAvatar.getRightPalmRotation; this.getHandRotation = MyAvatar.getRightPalmRotation;
} else { } else {
@ -83,309 +55,280 @@ function controller(side, triggerAction, pullAction, hand) {
this.getHandRotation = MyAvatar.getLeftPalmRotation; this.getHandRotation = MyAvatar.getLeftPalmRotation;
} }
this.triggerAction = triggerAction; this.triggerAction = triggerAction;
this.pullAction = pullAction; this.palm = 2 * hand;
this.actionID = null; this.tip = 2 * hand + 1;
this.distanceHolding = false;
this.closeGrabbing = false; this.actionID = null; // action this script created...
this.triggerValue = 0; this.grabbedEntity = null; // on this entity.
this.prevTriggerValue = 0; this.grabbedVelocity = ZERO_VEC;
this.palm = 2 * side; this.state = 0; // 0 = searching, 1 = distanceHolding, 2 = closeGrabbing
this.tip = 2 * side + 1; this.pointer = null; // entity-id of line object
this.pointer = null; this.triggerValue = 0; // rolling average of trigger value
} }
controller.prototype.updateLine = function() { controller.prototype.update = function() {
if (this.pointer != null) { switch(this.state) {
if (Entities.getEntityProperties(this.pointer).id != this.pointer) { case STATE_SEARCHING:
this.pointer = null; search(this);
} break;
case STATE_DISTANCE_HOLDING:
distanceHolding(this);
break;
case STATE_CLOSE_GRABBING:
closeGrabbing(this);
break;
case STATE_CONTINUE_CLOSE_GRABBING:
continueCloseGrabbing(this);
break;
case STATE_RELEASE:
release(this);
break;
} }
}
if (this.pointer == null) {
this.lineCreationTime = Date.now(); function lineOn(self, closePoint, farPoint, color) {
this.pointer = Entities.addEntity({ // draw a line
if (self.pointer == null) {
self.pointer = Entities.addEntity({
type: "Line", type: "Line",
name: "pointer", name: "pointer",
color: NO_INTERSECT_COLOR, dimensions: {x: 1000, y: 1000, z: 1000},
dimensions: {
x: 1000,
y: 1000,
z: 1000
},
visible: true, visible: true,
position: closePoint,
linePoints: [ ZERO_VEC, farPoint ],
color: color,
lifetime: LIFETIME lifetime: LIFETIME
}); });
} } else {
Entities.editEntity(self.pointer, {
var handPosition = this.getHandPosition(); position: closePoint,
var direction = Quat.getUp(this.getHandRotation()); linePoints: [ ZERO_VEC, farPoint ],
color: color,
//only check if we havent already grabbed an object
if (this.distanceHolding) {
Entities.editEntity(this.pointer, {
position: handPosition,
linePoints: [ ZERO_VEC, Vec3.subtract(Entities.getEntityProperties(this.grabbedEntity).position, handPosition) ],
lifetime: (Date.now() - startTime) / 1000.0 + LIFETIME lifetime: (Date.now() - startTime) / 1000.0 + LIFETIME
}); });
}
}
function lineOff(self) {
if (self.pointer != null) {
Entities.deleteEntity(self.pointer);
}
self.pointer = null;
}
function triggerSmoothedSqueezed(self) {
var triggerValue = Controller.getActionValue(self.triggerAction);
self.triggerValue = (self.triggerValue * 0.7) + (triggerValue * 0.3); // smooth out trigger value
return self.triggerValue > 0.2;
}
function triggerSqueezed(self) {
var triggerValue = Controller.getActionValue(self.triggerAction);
return triggerValue > 0.2;
}
function search(self) {
if (!triggerSmoothedSqueezed(self)) {
self.state = STATE_RELEASE;
return; return;
} }
Entities.editEntity(this.pointer, { // the trigger is being pressed, do a ray test
position: handPosition, var handPosition = self.getHandPosition();
linePoints: [ ZERO_VEC, Vec3.multiply(direction, LINE_LENGTH) ], var pickRay = {origin: handPosition, direction: Quat.getUp(self.getHandRotation())};
lifetime: (Date.now() - startTime) / 1000.0 + LIFETIME
});
if (this.checkForIntersections(handPosition, direction)) {
Entities.editEntity(this.pointer, {
color: INTERSECT_COLOR,
});
} else {
Entities.editEntity(this.pointer, {
color: NO_INTERSECT_COLOR,
});
}
}
controller.prototype.checkPointer = function() {
var self = this;
Script.setTimeout(function() {
var props = Entities.getEntityProperties(self.pointer);
Entities.editEntity(self.pointer, {
lifetime: (Date.now() - startTime) / 1000.0 + LIFETIME
});
self.checkPointer();
}, POINTER_CHECK_TIME);
}
controller.prototype.checkForIntersections = function(origin, direction) {
var pickRay = {
origin: origin,
direction: direction
};
var intersection = Entities.findRayIntersection(pickRay, true); var intersection = Entities.findRayIntersection(pickRay, true);
if (intersection.intersects && intersection.properties.collisionsWillMove === 1) { if (intersection.intersects &&
var handPosition = Controller.getSpatialControlPosition(this.palm); intersection.properties.collisionsWillMove === 1 &&
this.distanceToEntity = Vec3.distance(handPosition, intersection.properties.position); intersection.properties.locked === 0) {
var intersectionDistance = Vec3.distance(handPosition, intersection.intersection); // the ray is intersecting something we can move.
var handControllerPosition = Controller.getSpatialControlPosition(self.palm);
var intersectionDistance = Vec3.distance(handControllerPosition, intersection.intersection);
self.grabbedEntity = intersection.entityID;
if (intersectionDistance < 0.6) { if (intersectionDistance < 0.6) {
//We are grabbing an entity, so let it know we've grabbed it // the hand is very close to the intersected object. go into close-grabbing mode.
this.grabbedEntity = intersection.entityID; self.state = STATE_CLOSE_GRABBING;
this.activateEntity(this.grabbedEntity);
this.hidePointer();
this.shouldDisplayLine = false;
this.grabEntity();
return true;
} else { } else {
Entities.editEntity(this.pointer, { // the hand is far from the intersected object. go into distance-holding mode
linePoints: [ self.state = STATE_DISTANCE_HOLDING;
ZERO_VEC, lineOn(self, pickRay.origin, Vec3.multiply(pickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR);
Vec3.multiply(direction, this.distanceToEntity)
]
});
this.grabbedEntity = intersection.entityID;
return true;
} }
} } else {
return false; // forward ray test failed, try sphere test.
} var nearbyEntities = Entities.findEntities(handPosition, GRAB_RADIUS);
var minDistance = GRAB_RADIUS;
var grabbedEntity = null;
controller.prototype.attemptMove = function() { for (var i = 0; i < nearbyEntities.length; i++) {
if (this.grabbedEntity || this.distanceHolding) { var props = Entities.getEntityProperties(nearbyEntities[i]);
var handPosition = Controller.getSpatialControlPosition(this.palm); var distance = Vec3.distance(props.position, handPosition);
var handRotation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(this.palm)); if (distance < minDistance && props.name !== "pointer" &&
props.collisionsWillMove === 1 &&
this.distanceHolding = true; props.locked === 0) {
if (this.actionID === null) { self.grabbedEntity = nearbyEntities[i];
this.currentObjectPosition = Entities.getEntityProperties(this.grabbedEntity).position; minDistance = distance;
this.currentObjectRotation = Entities.getEntityProperties(this.grabbedEntity).rotation; }
}
this.handPreviousPosition = handPosition; if (self.grabbedEntity === null) {
this.handPreviousRotation = handRotation; lineOn(self, pickRay.origin, Vec3.multiply(pickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR);
this.actionID = Entities.addAction("spring", this.grabbedEntity, {
targetPosition: this.currentObjectPosition,
linearTimeScale: .1,
targetRotation: this.currentObjectRotation,
angularTimeScale: .1
});
} else { } else {
var radius = Math.max(Vec3.distance(this.currentObjectPosition, handPosition) * RADIUS_FACTOR, 1.0); self.state = STATE_CLOSE_GRABBING;
var handMoved = Vec3.subtract(handPosition, this.handPreviousPosition);
this.handPreviousPosition = handPosition;
var superHandMoved = Vec3.multiply(handMoved, radius);
this.currentObjectPosition = Vec3.sum(this.currentObjectPosition, superHandMoved);
// ---------------- this tracks hand rotation
// var handChange = Quat.multiply(handRotation, Quat.inverse(this.handPreviousRotation));
// this.handPreviousRotation = handRotation;
// this.currentObjectRotation = Quat.multiply(handChange, this.currentObjectRotation);
// ----------------
// ---------------- this doubles hand rotation
var handChange = Quat.multiply(Quat.slerp(this.handPreviousRotation, handRotation, 2.0),
Quat.inverse(this.handPreviousRotation));
this.handPreviousRotation = handRotation;
this.currentObjectRotation = Quat.multiply(handChange, this.currentObjectRotation);
// ----------------
Entities.updateAction(this.grabbedEntity, this.actionID, {
targetPosition: this.currentObjectPosition, linearTimeScale: .1,
targetRotation: this.currentObjectRotation, angularTimeScale: .1
});
} }
} }
} }
controller.prototype.showPointer = function() {
Entities.editEntity(this.pointer, {
visible: true
});
} function distanceHolding(self) {
if (!triggerSmoothedSqueezed(self)) {
controller.prototype.hidePointer = function() { self.state = STATE_RELEASE;
Entities.editEntity(this.pointer, { return;
visible: false
});
}
controller.prototype.letGo = function() {
if (this.grabbedEntity && this.actionID) {
this.deactivateEntity(this.grabbedEntity);
Entities.deleteAction(this.grabbedEntity, this.actionID);
} }
this.grabbedEntity = null;
this.actionID = null;
this.distanceHolding = false;
this.closeGrabbing = false;
}
controller.prototype.update = function() { var handPosition = self.getHandPosition();
this.triggerValue = Controller.getActionValue(this.triggerAction); var handControllerPosition = Controller.getSpatialControlPosition(self.palm);
if (this.triggerValue > SHOW_LINE_THRESHOLD && this.prevTriggerValue < SHOW_LINE_THRESHOLD) { var handRotation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(self.palm));
//First check if an object is within close range and then run the close grabbing logic var grabbedProperties = Entities.getEntityProperties(self.grabbedEntity);
if (this.checkForInRangeObject()) {
this.grabEntity(); lineOn(self, handPosition, Vec3.subtract(grabbedProperties.position, handPosition), INTERSECT_COLOR);
} else {
this.showPointer(); if (self.actionID === null) {
this.shouldDisplayLine = true; // first time here since trigger pulled -- add the action and initialize some variables
self.currentObjectPosition = grabbedProperties.position;
self.currentObjectRotation = grabbedProperties.rotation;
self.handPreviousPosition = handControllerPosition;
self.handPreviousRotation = handRotation;
self.actionID = Entities.addAction("spring", self.grabbedEntity, {
targetPosition: self.currentObjectPosition,
linearTimeScale: .1,
targetRotation: self.currentObjectRotation,
angularTimeScale: .1
});
if (self.actionID == NULL_ACTION_ID) {
self.actionID = null;
} }
} else if (this.triggerValue < SHOW_LINE_THRESHOLD && this.prevTriggerValue > SHOW_LINE_THRESHOLD) { } else {
this.hidePointer(); // the action was set up on a previous call. update the targets.
this.letGo(); var radius = Math.max(Vec3.distance(self.currentObjectPosition, handControllerPosition) * RADIUS_FACTOR, RADIUS_FACTOR);
this.shouldDisplayLine = false;
}
if (this.shouldDisplayLine) { var handMoved = Vec3.subtract(handControllerPosition, self.handPreviousPosition);
this.updateLine(); self.handPreviousPosition = handControllerPosition;
} var superHandMoved = Vec3.multiply(handMoved, radius);
if (this.triggerValue > DISTANCE_HOLD_THRESHOLD && !this.closeGrabbing) { self.currentObjectPosition = Vec3.sum(self.currentObjectPosition, superHandMoved);
this.attemptMove();
}
this.prevTriggerValue = this.triggerValue; // this doubles hand rotation
var handChange = Quat.multiply(Quat.slerp(self.handPreviousRotation, handRotation, 2.0),
Quat.inverse(self.handPreviousRotation));
self.handPreviousRotation = handRotation;
self.currentObjectRotation = Quat.multiply(handChange, self.currentObjectRotation);
Entities.updateAction(self.grabbedEntity, self.actionID, {
targetPosition: self.currentObjectPosition, linearTimeScale: .1,
targetRotation: self.currentObjectRotation, angularTimeScale: .1
});
}
} }
controller.prototype.grabEntity = function() {
var handRotation = this.getHandRotation();
var handPosition = this.getHandPosition();
this.closeGrabbing = true;
//check if our entity has instructions on how to be grabbed, otherwise, just use default relative position and rotation
var userData = getEntityUserData(this.grabbedEntity);
var objectRotation = Entities.getEntityProperties(this.grabbedEntity).rotation; function closeGrabbing(self) {
if (!triggerSmoothedSqueezed(self)) {
self.state = STATE_RELEASE;
return;
}
lineOff(self);
var grabbedProperties = Entities.getEntityProperties(self.grabbedEntity);
var handRotation = self.getHandRotation();
var handPosition = self.getHandPosition();
var objectRotation = grabbedProperties.rotation;
var offsetRotation = Quat.multiply(Quat.inverse(handRotation), objectRotation); var offsetRotation = Quat.multiply(Quat.inverse(handRotation), objectRotation);
var objectPosition = Entities.getEntityProperties(this.grabbedEntity).position; self.currentObjectPosition = grabbedProperties.position;
var offset = Vec3.subtract(objectPosition, handPosition); self.currentObjectTime = Date.now();
var offset = Vec3.subtract(self.currentObjectPosition, handPosition);
var offsetPosition = Vec3.multiplyQbyV(Quat.inverse(Quat.multiply(handRotation, offsetRotation)), offset); var offsetPosition = Vec3.multiplyQbyV(Quat.inverse(Quat.multiply(handRotation, offsetRotation)), offset);
var relativePosition = offsetPosition; self.actionID = Entities.addAction("hold", self.grabbedEntity, {
var relativeRotation = offsetRotation; hand: self.hand == RIGHT_HAND ? "right" : "left",
if (userData.grabFrame) {
if (userData.grabFrame.relativePosition) {
relativePosition = userData.grabFrame.relativePosition;
}
if (userData.grabFrame.relativeRotation) {
relativeRotation = userData.grabFrame.relativeRotation;
}
}
this.actionID = Entities.addAction("hold", this.grabbedEntity, {
hand: this.hand,
timeScale: 0.05, timeScale: 0.05,
relativePosition: relativePosition, relativePosition: offsetPosition,
relativeRotation: relativeRotation relativeRotation: offsetRotation
}); });
if (self.actionID == NULL_ACTION_ID) {
self.actionID = null;
} else {
self.state = STATE_CONTINUE_CLOSE_GRABBING;
}
} }
controller.prototype.checkForInRangeObject = function() { function continueCloseGrabbing(self) {
var handPosition = Controller.getSpatialControlPosition(this.palm); if (!triggerSmoothedSqueezed(self)) {
var entities = Entities.findEntities(handPosition, GRAB_RADIUS); self.state = STATE_RELEASE;
var minDistance = GRAB_RADIUS; return;
var grabbedEntity = null; }
//Get nearby entities and assign nearest
for (var i = 0; i < entities.length; i++) { // keep track of the measured velocity of the held object
var props = Entities.getEntityProperties(entities[i]); var grabbedProperties = Entities.getEntityProperties(self.grabbedEntity);
var distance = Vec3.distance(props.position, handPosition); var now = Date.now();
if (distance < minDistance && props.name !== "pointer" && props.collisionsWillMove === 1) {
grabbedEntity = entities[i]; var deltaPosition = Vec3.subtract(grabbedProperties.position, self.currentObjectPosition);
minDistance = distance; var deltaTime = (now - self.currentObjectTime) / 1000.0; // convert to seconds
if (deltaTime > 0.0) {
var grabbedVelocity = Vec3.multiply(deltaPosition, 1.0 / deltaTime);
// don't update grabbedVelocity if the trigger is off. the smoothing of the trigger
// value would otherwise give the held object time to slow down.
if (triggerSqueezed(self)) {
self.grabbedVelocity = Vec3.sum(Vec3.multiply(self.grabbedVelocity, 0.1),
Vec3.multiply(grabbedVelocity, 0.9));
} }
} }
if (grabbedEntity === null) {
return false;
} else {
//We are grabbing an entity, so let it know we've grabbed it
this.grabbedEntity = grabbedEntity;
this.activateEntity(this.grabbedEntity);
return true; self.currentObjectPosition = grabbedProperties.position;
self.currentObjectTime = now;
}
function release(self) {
lineOff(self);
if (self.grabbedEntity != null && self.actionID != null) {
Entities.deleteAction(self.grabbedEntity, self.actionID);
} }
// the action will tend to quickly bring an object's velocity to zero. now that
// the action is gone, set the objects velocity to something the holder might expect.
Entities.editEntity(self.grabbedEntity, {velocity: self.grabbedVelocity});
self.grabbedVelocity = ZERO_VEC;
self.grabbedEntity = null;
self.actionID = null;
self.state = STATE_SEARCHING;
} }
controller.prototype.activateEntity = function(entity) {
var data = {
activated: true,
avatarId: MyAvatar.sessionUUID
};
setEntityCustomData(GRAB_USER_DATA_KEY, this.grabbedEntity, data);
}
controller.prototype.deactivateEntity = function(entity) {
var data = {
activated: false,
avatarId: null
};
setEntityCustomData(GRAB_USER_DATA_KEY, this.grabbedEntity, data);
}
controller.prototype.cleanup = function() { controller.prototype.cleanup = function() {
Entities.deleteEntity(this.pointer); release(this);
if (this.grabbedEntity) {
Entities.deleteAction(this.grabbedEntity, this.actionID);
}
} }
function update() { function update() {
rightController.update(); rightController.update();
leftController.update(); leftController.update();
} }
function cleanup() { function cleanup() {
rightController.cleanup(); rightController.cleanup();
leftController.cleanup(); leftController.cleanup();
} }
Script.scriptEnding.connect(cleanup); Script.scriptEnding.connect(cleanup);
Script.update.connect(update) Script.update.connect(update)