get grabbing and scaling to get along better

This commit is contained in:
Seth Alves 2018-12-13 12:08:01 -08:00
parent 905af34564
commit 0967a94557
2 changed files with 45 additions and 33 deletions

View file

@ -7,11 +7,10 @@
/* global Script, Entities, MyAvatar, Controller, RIGHT_HAND, LEFT_HAND, getControllerJointIndex, enableDispatcherModule, /* global Script, Entities, MyAvatar, Controller, RIGHT_HAND, LEFT_HAND, getControllerJointIndex, enableDispatcherModule,
disableDispatcherModule, Messages, HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION, TRIGGER_OFF_VALUE, TRIGGER_ON_VALUE, disableDispatcherModule, Messages, HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION, TRIGGER_OFF_VALUE,
makeDispatcherModuleParameters, entityIsGrabbable, makeRunningValues, NEAR_GRAB_RADIUS, findGroupParent, Vec3, makeDispatcherModuleParameters, entityIsGrabbable, makeRunningValues, NEAR_GRAB_RADIUS, findGroupParent, Vec3, cloneEntity,
cloneEntity, entityIsCloneable, HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION, BUMPER_ON_VALUE, entityIsCloneable, HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION, BUMPER_ON_VALUE, distanceBetweenPointAndEntityBoundingBox,
distanceBetweenPointAndEntityBoundingBox, getGrabbableData, getGrabbableData, getEnabledModuleByName, DISPATCHER_PROPERTIES, HMD, NEAR_GRAB_DISTANCE
DISPATCHER_PROPERTIES, HMD, NEAR_GRAB_DISTANCE
*/ */
Script.include("/~/system/libraries/controllerDispatcherUtils.js"); Script.include("/~/system/libraries/controllerDispatcherUtils.js");
@ -33,9 +32,12 @@ Script.include("/~/system/libraries/controllers.js");
[], [],
100); 100);
this.startNearGrabEntity = function (controllerData, targetProps) { this.startGrab = function (targetProps) {
if (this.grabID) {
MyAvatar.releaseGrab(this.grabID);
}
var grabData = getGrabbableData(targetProps); var grabData = getGrabbableData(targetProps);
Controller.triggerHapticPulse(HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION, this.hand);
var handJointIndex; var handJointIndex;
if (HMD.mounted && HMD.isHandControllerAvailable() && grabData.grabFollowsController) { if (HMD.mounted && HMD.isHandControllerAvailable() && grabData.grabFollowsController) {
@ -44,33 +46,42 @@ Script.include("/~/system/libraries/controllers.js");
handJointIndex = MyAvatar.getJointIndex(this.hand === RIGHT_HAND ? "RightHand" : "LeftHand"); handJointIndex = MyAvatar.getJointIndex(this.hand === RIGHT_HAND ? "RightHand" : "LeftHand");
} }
var args = [this.hand === RIGHT_HAND ? "right" : "left", MyAvatar.sessionUUID];
Entities.callEntityMethod(targetProps.id, "startNearGrab", args);
this.targetEntityID = targetProps.id; this.targetEntityID = targetProps.id;
if (this.grabID) {
MyAvatar.releaseGrab(this.grabID);
}
var relativePosition = Entities.worldToLocalPosition(targetProps.position, MyAvatar.SELF_ID, handJointIndex); var relativePosition = Entities.worldToLocalPosition(targetProps.position, MyAvatar.SELF_ID, handJointIndex);
var relativeRotation = Entities.worldToLocalRotation(targetProps.rotation, MyAvatar.SELF_ID, handJointIndex); var relativeRotation = Entities.worldToLocalRotation(targetProps.rotation, MyAvatar.SELF_ID, handJointIndex);
this.grabID = MyAvatar.grab(targetProps.id, handJointIndex, relativePosition, relativeRotation); this.grabID = MyAvatar.grab(targetProps.id, handJointIndex, relativePosition, relativeRotation);
};
this.startNearGrabEntity = function (targetProps) {
Controller.triggerHapticPulse(HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION, this.hand);
this.startGrab(targetProps);
var args = [this.hand === RIGHT_HAND ? "right" : "left", MyAvatar.sessionUUID];
Entities.callEntityMethod(targetProps.id, "startNearGrab", args);
Messages.sendMessage('Hifi-Object-Manipulation', JSON.stringify({ Messages.sendMessage('Hifi-Object-Manipulation', JSON.stringify({
action: 'grab', action: 'grab',
grabbedEntity: targetProps.id, grabbedEntity: targetProps.id,
joint: this.hand === RIGHT_HAND ? "RightHand" : "LeftHand" joint: this.hand === RIGHT_HAND ? "RightHand" : "LeftHand"
})); }));
this.grabbing = true; this.grabbing = true;
}; };
this.endNearGrabEntity = function (controllerData) { this.endGrab = function () {
if (this.grabID) { if (this.grabID) {
MyAvatar.releaseGrab(this.grabID); MyAvatar.releaseGrab(this.grabID);
this.grabID = null; this.grabID = null;
} }
};
this.endNearGrabEntity = function () {
this.endGrab();
this.grabbing = false;
this.targetEntityID = null;
var args = [this.hand === RIGHT_HAND ? "right" : "left", MyAvatar.sessionUUID]; var args = [this.hand === RIGHT_HAND ? "right" : "left", MyAvatar.sessionUUID];
Entities.callEntityMethod(this.targetEntityID, "releaseGrab", args); Entities.callEntityMethod(this.targetEntityID, "releaseGrab", args);
@ -79,9 +90,6 @@ Script.include("/~/system/libraries/controllers.js");
grabbedEntity: this.targetEntityID, grabbedEntity: this.targetEntityID,
joint: this.hand === RIGHT_HAND ? "RightHand" : "LeftHand" joint: this.hand === RIGHT_HAND ? "RightHand" : "LeftHand"
})); }));
this.grabbing = false;
this.targetEntityID = null;
}; };
this.getTargetProps = function (controllerData) { this.getTargetProps = function (controllerData) {
@ -123,6 +131,13 @@ Script.include("/~/system/libraries/controllers.js");
return makeRunningValues(false, [], []); return makeRunningValues(false, [], []);
} }
var scaleModuleName = this.hand === RIGHT_HAND ? "RightScaleEntity" : "LeftScaleEntity";
var scaleModule = getEnabledModuleByName(scaleModuleName);
if (scaleModule.grabbedThingID || scaleModule.isReady(controllerData).active) {
// we're rescaling -- don't start a grab.
return makeRunningValues(false, [], []);
}
var targetProps = this.getTargetProps(controllerData); var targetProps = this.getTargetProps(controllerData);
if (targetProps) { if (targetProps) {
this.targetEntityID = targetProps.id; this.targetEntityID = targetProps.id;
@ -133,17 +148,11 @@ Script.include("/~/system/libraries/controllers.js");
}; };
this.run = function (controllerData, deltaTime) { this.run = function (controllerData, deltaTime) {
if (this.grabbing) { if (this.grabbing) {
if (controllerData.triggerClicks[this.hand] < TRIGGER_OFF_VALUE && if (controllerData.triggerClicks[this.hand] < TRIGGER_OFF_VALUE &&
controllerData.secondaryValues[this.hand] < TRIGGER_OFF_VALUE) { controllerData.secondaryValues[this.hand] < TRIGGER_OFF_VALUE) {
this.endNearGrabEntity(controllerData); this.endNearGrabEntity();
return makeRunningValues(false, [], []);
}
if (controllerData.secondaryValues[LEFT_HAND] >= TRIGGER_ON_VALUE &&
controllerData.secondaryValues[RIGHT_HAND] >= TRIGGER_ON_VALUE) {
// let scaleEntity module take over
this.endNearGrabEntity(controllerData);
return makeRunningValues(false, [], []); return makeRunningValues(false, [], []);
} }
@ -178,13 +187,13 @@ Script.include("/~/system/libraries/controllers.js");
var cloneProps = Entities.getEntityProperties(cloneID, DISPATCHER_PROPERTIES); var cloneProps = Entities.getEntityProperties(cloneID, DISPATCHER_PROPERTIES);
this.grabbing = true; this.grabbing = true;
this.targetEntityID = cloneID; this.targetEntityID = cloneID;
this.startNearGrabEntity(controllerData, cloneProps); this.startNearGrabEntity(cloneProps);
this.cloneAllowed = false; // prevent another clone call until inputs released this.cloneAllowed = false; // prevent another clone call until inputs released
} }
} }
} else if (targetProps) { } else if (targetProps) {
this.grabbing = true; this.grabbing = true;
this.startNearGrabEntity(controllerData, targetProps); this.startNearGrabEntity(targetProps);
} }
} }
} }

View file

@ -19,7 +19,7 @@
this.parameters = dispatcherUtils.makeDispatcherModuleParameters( this.parameters = dispatcherUtils.makeDispatcherModuleParameters(
120, 120,
this.hand === RIGHT_HAND ? ["rightHandTrigger", "rightHand"] : ["leftHandTrigger", "leftHand"], this.hand === RIGHT_HAND ? ["rightHandTrigger"] : ["leftHandTrigger"],
[], [],
100 100
); );
@ -63,13 +63,15 @@
if (thisHandTargetProps && otherHandTargetProps) { if (thisHandTargetProps && otherHandTargetProps) {
if (thisHandTargetProps.id === otherHandTargetProps.id) { if (thisHandTargetProps.id === otherHandTargetProps.id) {
this.grabbedThingID = thisHandTargetProps.id; this.grabbedThingID = thisHandTargetProps.id;
this.scalingStartDistance = Vec3.length(Vec3.subtract(controllerData.controllerLocations[this.hand].position, this.scalingStartDistance =
controllerData.controllerLocations[this.otherHand()].position)); Vec3.length(Vec3.subtract(controllerData.controllerLocations[this.hand].position,
controllerData.controllerLocations[this.otherHand()].position));
this.scalingStartDimensions = thisHandTargetProps.dimensions; this.scalingStartDimensions = thisHandTargetProps.dimensions;
return dispatcherUtils.makeRunningValues(true, [], []); return dispatcherUtils.makeRunningValues(true, [], []);
} }
} }
} }
this.grabbedThingID = false;
return dispatcherUtils.makeRunningValues(false, [], []); return dispatcherUtils.makeRunningValues(false, [], []);
}; };
@ -82,10 +84,11 @@
controllerData.controllerLocations[this.otherHand()].position)); controllerData.controllerLocations[this.otherHand()].position));
var currentRescale = scalingCurrentDistance / this.scalingStartDistance; var currentRescale = scalingCurrentDistance / this.scalingStartDistance;
var newDimensions = Vec3.multiply(currentRescale, this.scalingStartDimensions); var newDimensions = Vec3.multiply(currentRescale, this.scalingStartDimensions);
Entities.editEntity(this.grabbedThingID, { dimensions: newDimensions }); Entities.editEntity(this.grabbedThingID, { localDimensions: newDimensions });
} }
return dispatcherUtils.makeRunningValues(true, [], []); return dispatcherUtils.makeRunningValues(true, [], []);
} }
this.grabbedThingID = false;
return dispatcherUtils.makeRunningValues(false, [], []); return dispatcherUtils.makeRunningValues(false, [], []);
}; };
} }