2017-08-12 17:27:48 -07:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// farActionGrabEntity.js
|
|
|
|
//
|
|
|
|
// Distributed under the Apache License, Version 2.0.
|
|
|
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
|
2017-08-31 18:06:55 -07:00
|
|
|
/* jslint bitwise: true */
|
2017-08-12 17:27:48 -07:00
|
|
|
|
2018-03-01 08:43:30 -08:00
|
|
|
/* global Script, Controller, RIGHT_HAND, LEFT_HAND, Mat4, MyAvatar, Vec3, Camera, Quat,
|
2018-08-29 12:35:14 -07:00
|
|
|
getEnabledModuleByName, makeRunningValues, Entities,
|
2017-10-17 16:38:05 +13:00
|
|
|
enableDispatcherModule, disableDispatcherModule, entityIsDistanceGrabbable, entityIsGrabbable,
|
2017-08-13 15:54:03 -07:00
|
|
|
makeDispatcherModuleParameters, MSECS_PER_SEC, HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION,
|
2018-08-29 12:35:14 -07:00
|
|
|
TRIGGER_OFF_VALUE, TRIGGER_ON_VALUE, ZERO_VEC, ensureDynamic,
|
2020-08-30 03:23:22 -07:00
|
|
|
getControllerWorldLocation, projectOntoEntityXYPlane, HMD,
|
2018-08-29 12:35:14 -07:00
|
|
|
Picks, makeLaserLockInfo, makeLaserParams, AddressManager, getEntityParents, Selection, DISPATCHER_HOVERING_LIST,
|
2019-10-01 14:52:33 -07:00
|
|
|
worldPositionToRegistrationFrameMatrix, DISPATCHER_PROPERTIES, Uuid, Picks, handsAreTracked, Messages
|
2017-08-12 17:27:48 -07:00
|
|
|
*/
|
|
|
|
|
2017-09-07 14:42:46 -07:00
|
|
|
Script.include("/~/system/libraries/controllerDispatcherUtils.js");
|
2017-08-12 17:27:48 -07:00
|
|
|
Script.include("/~/system/libraries/controllers.js");
|
|
|
|
|
|
|
|
(function() {
|
2017-08-28 17:35:34 -07:00
|
|
|
|
2023-11-30 00:26:01 +01:00
|
|
|
var controllerStandard = Controller.Standard;
|
|
|
|
|
2017-10-02 16:46:43 -07:00
|
|
|
var MARGIN = 25;
|
2017-11-03 17:14:27 -07:00
|
|
|
|
2017-11-06 15:21:20 -08:00
|
|
|
function TargetObject(entityID, entityProps) {
|
2017-11-03 17:14:27 -07:00
|
|
|
this.entityID = entityID;
|
2017-11-06 15:21:20 -08:00
|
|
|
this.entityProps = entityProps;
|
|
|
|
this.targetEntityID = null;
|
|
|
|
this.targetEntityProps = null;
|
|
|
|
this.previousCollisionStatus = null;
|
2017-11-03 17:14:27 -07:00
|
|
|
this.madeDynamic = null;
|
|
|
|
|
|
|
|
this.makeDynamic = function() {
|
2017-11-06 15:21:20 -08:00
|
|
|
if (this.targetEntityID) {
|
|
|
|
var newProps = {
|
|
|
|
dynamic: true,
|
|
|
|
collisionless: true
|
|
|
|
};
|
|
|
|
this.previousCollisionStatus = this.targetEntityProps.collisionless;
|
|
|
|
Entities.editEntity(this.targetEntityID, newProps);
|
|
|
|
this.madeDynamic = true;
|
|
|
|
}
|
2017-11-03 17:14:27 -07:00
|
|
|
};
|
|
|
|
|
2017-11-06 15:21:20 -08:00
|
|
|
this.restoreTargetEntityOriginalProps = function() {
|
|
|
|
if (this.madeDynamic) {
|
|
|
|
var props = {};
|
|
|
|
props.dynamic = false;
|
|
|
|
props.collisionless = this.previousCollisionStatus;
|
|
|
|
var zeroVector = {x: 0, y: 0, z:0};
|
|
|
|
props.localVelocity = zeroVector;
|
|
|
|
props.localRotation = zeroVector;
|
|
|
|
Entities.editEntity(this.targetEntityID, props);
|
|
|
|
}
|
2017-11-03 17:14:27 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getTargetEntity = function() {
|
|
|
|
var parentPropsLength = this.parentProps.length;
|
|
|
|
if (parentPropsLength !== 0) {
|
2017-11-06 15:21:20 -08:00
|
|
|
var targetEntity = {
|
|
|
|
id: this.parentProps[parentPropsLength - 1].id,
|
|
|
|
props: this.parentProps[parentPropsLength - 1]};
|
|
|
|
this.targetEntityID = targetEntity.id;
|
|
|
|
this.targetEntityProps = targetEntity.props;
|
|
|
|
return targetEntity;
|
2017-11-03 17:14:27 -07:00
|
|
|
}
|
2017-11-06 15:21:20 -08:00
|
|
|
this.targetEntityID = this.entityID;
|
|
|
|
this.targetEntityProps = this.entityProps;
|
|
|
|
return {
|
|
|
|
id: this.entityID,
|
|
|
|
props: this.entityProps};
|
2017-11-03 17:14:27 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-12 17:27:48 -07:00
|
|
|
function FarActionGrabEntity(hand) {
|
|
|
|
this.hand = hand;
|
|
|
|
this.grabbedThingID = null;
|
2017-11-03 17:14:27 -07:00
|
|
|
this.targetObject = null;
|
2017-08-12 17:27:48 -07:00
|
|
|
this.actionID = null; // action this script created...
|
2017-11-03 17:14:27 -07:00
|
|
|
this.entityToLockOnto = null;
|
2017-11-06 15:21:20 -08:00
|
|
|
this.locked = false;
|
2017-10-02 16:46:43 -07:00
|
|
|
this.reticleMinX = MARGIN;
|
2018-08-29 12:35:14 -07:00
|
|
|
this.reticleMaxX = null;
|
2017-10-02 16:46:43 -07:00
|
|
|
this.reticleMinY = MARGIN;
|
2018-08-29 12:35:14 -07:00
|
|
|
this.reticleMaxY = null;
|
2017-08-12 17:27:48 -07:00
|
|
|
|
2018-10-03 17:40:42 -07:00
|
|
|
this.ignoredEntities = [];
|
|
|
|
|
2017-08-13 15:54:03 -07:00
|
|
|
var ACTION_TTL = 15; // seconds
|
|
|
|
|
|
|
|
var DISTANCE_HOLDING_RADIUS_FACTOR = 3.5; // multiplied by distance between hand and object
|
|
|
|
var DISTANCE_HOLDING_ACTION_TIMEFRAME = 0.1; // how quickly objects move to their new position
|
|
|
|
var DISTANCE_HOLDING_UNITY_MASS = 1200; // The mass at which the distance holding action timeframe is unmodified
|
|
|
|
var DISTANCE_HOLDING_UNITY_DISTANCE = 6; // The distance at which the distance holding action timeframe is unmodified
|
|
|
|
|
2017-08-12 17:27:48 -07:00
|
|
|
this.parameters = makeDispatcherModuleParameters(
|
|
|
|
550,
|
|
|
|
this.hand === RIGHT_HAND ? ["rightHand"] : ["leftHand"],
|
|
|
|
[],
|
2017-10-30 16:58:14 -07:00
|
|
|
100,
|
2017-11-28 15:37:36 -08:00
|
|
|
makeLaserParams(this.hand, false));
|
2017-08-12 17:27:48 -07:00
|
|
|
|
|
|
|
|
|
|
|
this.handToController = function() {
|
2023-11-30 00:26:01 +01:00
|
|
|
return (this.hand === RIGHT_HAND) ? controllerStandard.RightHand : controllerStandard.LeftHand;
|
2017-08-12 17:27:48 -07:00
|
|
|
};
|
|
|
|
|
2017-08-13 15:54:03 -07:00
|
|
|
this.distanceGrabTimescale = function(mass, distance) {
|
|
|
|
var timeScale = DISTANCE_HOLDING_ACTION_TIMEFRAME * mass /
|
|
|
|
DISTANCE_HOLDING_UNITY_MASS * distance /
|
|
|
|
DISTANCE_HOLDING_UNITY_DISTANCE;
|
|
|
|
if (timeScale < DISTANCE_HOLDING_ACTION_TIMEFRAME) {
|
|
|
|
timeScale = DISTANCE_HOLDING_ACTION_TIMEFRAME;
|
|
|
|
}
|
|
|
|
return timeScale;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getMass = function(dimensions, density) {
|
|
|
|
return (dimensions.x * dimensions.y * dimensions.z) * density;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.startFarGrabAction = function (controllerData, grabbedProperties) {
|
|
|
|
var controllerLocation = controllerData.controllerLocations[this.hand];
|
|
|
|
var worldControllerPosition = controllerLocation.position;
|
|
|
|
var worldControllerRotation = controllerLocation.orientation;
|
|
|
|
|
|
|
|
// transform the position into room space
|
|
|
|
var worldToSensorMat = Mat4.inverse(MyAvatar.getSensorToWorldMatrix());
|
|
|
|
var roomControllerPosition = Mat4.transformPoint(worldToSensorMat, worldControllerPosition);
|
|
|
|
|
|
|
|
var now = Date.now();
|
|
|
|
|
|
|
|
// add the action and initialize some variables
|
|
|
|
this.currentObjectPosition = grabbedProperties.position;
|
|
|
|
this.currentObjectRotation = grabbedProperties.rotation;
|
|
|
|
this.currentObjectTime = now;
|
|
|
|
this.currentCameraOrientation = Camera.orientation;
|
|
|
|
|
|
|
|
this.grabRadius = this.grabbedDistance;
|
|
|
|
this.grabRadialVelocity = 0.0;
|
|
|
|
|
|
|
|
// offset between controller vector at the grab radius and the entity position
|
|
|
|
var targetPosition = Vec3.multiply(this.grabRadius, Quat.getUp(worldControllerRotation));
|
|
|
|
targetPosition = Vec3.sum(targetPosition, worldControllerPosition);
|
|
|
|
this.offsetPosition = Vec3.subtract(this.currentObjectPosition, targetPosition);
|
|
|
|
|
|
|
|
// compute a constant based on the initial conditions which we use below to exaggerate hand motion
|
|
|
|
// onto the held object
|
|
|
|
this.radiusScalar = Math.log(this.grabRadius + 1.0);
|
|
|
|
if (this.radiusScalar < 1.0) {
|
|
|
|
this.radiusScalar = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compute the mass for the purpose of energy and how quickly to move object
|
|
|
|
this.mass = this.getMass(grabbedProperties.dimensions, grabbedProperties.density);
|
|
|
|
var distanceToObject = Vec3.length(Vec3.subtract(MyAvatar.position, grabbedProperties.position));
|
|
|
|
var timeScale = this.distanceGrabTimescale(this.mass, distanceToObject);
|
|
|
|
this.linearTimeScale = timeScale;
|
|
|
|
this.actionID = Entities.addAction("far-grab", this.grabbedThingID, {
|
|
|
|
targetPosition: this.currentObjectPosition,
|
|
|
|
linearTimeScale: timeScale,
|
|
|
|
targetRotation: this.currentObjectRotation,
|
|
|
|
angularTimeScale: timeScale,
|
|
|
|
tag: "far-grab-" + MyAvatar.sessionUUID,
|
|
|
|
ttl: ACTION_TTL
|
|
|
|
});
|
2024-10-19 19:06:16 +02:00
|
|
|
if (this.actionID === Uuid.NONE) {
|
2017-08-13 15:54:03 -07:00
|
|
|
this.actionID = null;
|
|
|
|
}
|
|
|
|
|
2017-09-15 10:50:46 -07:00
|
|
|
if (this.actionID !== null) {
|
|
|
|
var args = [this.hand === RIGHT_HAND ? "right" : "left", MyAvatar.sessionUUID];
|
2017-09-15 12:07:50 -07:00
|
|
|
Entities.callEntityMethod(this.grabbedThingID, "startDistanceGrab", args);
|
2017-09-15 10:50:46 -07:00
|
|
|
}
|
2017-08-13 15:54:03 -07:00
|
|
|
|
|
|
|
Controller.triggerHapticPulse(HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION, this.hand);
|
|
|
|
this.previousRoomControllerPosition = roomControllerPosition;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.continueDistanceHolding = function(controllerData) {
|
|
|
|
var controllerLocation = controllerData.controllerLocations[this.hand];
|
|
|
|
var worldControllerPosition = controllerLocation.position;
|
|
|
|
var worldControllerRotation = controllerLocation.orientation;
|
|
|
|
|
|
|
|
// also transform the position into room space
|
|
|
|
var worldToSensorMat = Mat4.inverse(MyAvatar.getSensorToWorldMatrix());
|
|
|
|
var roomControllerPosition = Mat4.transformPoint(worldToSensorMat, worldControllerPosition);
|
|
|
|
|
2023-12-08 23:01:01 +01:00
|
|
|
var grabbedProperties = Entities.getEntityProperties(this.grabbedThingID, "position");
|
2017-08-13 15:54:03 -07:00
|
|
|
var now = Date.now();
|
|
|
|
var deltaObjectTime = (now - this.currentObjectTime) / MSECS_PER_SEC; // convert to seconds
|
|
|
|
this.currentObjectTime = now;
|
|
|
|
|
|
|
|
// the action was set up when this.distanceHolding was called. update the targets.
|
|
|
|
var radius = Vec3.distance(this.currentObjectPosition, worldControllerPosition) *
|
|
|
|
this.radiusScalar * DISTANCE_HOLDING_RADIUS_FACTOR;
|
|
|
|
if (radius < 1.0) {
|
|
|
|
radius = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var roomHandDelta = Vec3.subtract(roomControllerPosition, this.previousRoomControllerPosition);
|
|
|
|
var worldHandDelta = Mat4.transformVector(MyAvatar.getSensorToWorldMatrix(), roomHandDelta);
|
|
|
|
var handMoved = Vec3.multiply(worldHandDelta, radius);
|
|
|
|
this.currentObjectPosition = Vec3.sum(this.currentObjectPosition, handMoved);
|
|
|
|
|
2017-09-15 10:50:46 -07:00
|
|
|
var args = [this.hand === RIGHT_HAND ? "right" : "left", MyAvatar.sessionUUID];
|
2017-09-15 12:07:50 -07:00
|
|
|
Entities.callEntityMethod(this.grabbedThingID, "continueDistanceGrab", args);
|
2017-08-13 15:54:03 -07:00
|
|
|
|
|
|
|
// Update radialVelocity
|
|
|
|
var lastVelocity = Vec3.multiply(worldHandDelta, 1.0 / deltaObjectTime);
|
|
|
|
var delta = Vec3.normalize(Vec3.subtract(grabbedProperties.position, worldControllerPosition));
|
|
|
|
var newRadialVelocity = Vec3.dot(lastVelocity, delta);
|
|
|
|
|
|
|
|
var VELOCITY_AVERAGING_TIME = 0.016;
|
|
|
|
var blendFactor = deltaObjectTime / VELOCITY_AVERAGING_TIME;
|
|
|
|
if (blendFactor < 0.0) {
|
|
|
|
blendFactor = 0.0;
|
|
|
|
} else if (blendFactor > 1.0) {
|
|
|
|
blendFactor = 1.0;
|
|
|
|
}
|
|
|
|
this.grabRadialVelocity = blendFactor * newRadialVelocity + (1.0 - blendFactor) * this.grabRadialVelocity;
|
|
|
|
|
|
|
|
var RADIAL_GRAB_AMPLIFIER = 10.0;
|
|
|
|
if (Math.abs(this.grabRadialVelocity) > 0.0) {
|
|
|
|
this.grabRadius = this.grabRadius + (this.grabRadialVelocity * deltaObjectTime *
|
|
|
|
this.grabRadius * RADIAL_GRAB_AMPLIFIER);
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't let grabRadius go all the way to zero, because it can't come back from that
|
|
|
|
var MINIMUM_GRAB_RADIUS = 0.1;
|
|
|
|
if (this.grabRadius < MINIMUM_GRAB_RADIUS) {
|
|
|
|
this.grabRadius = MINIMUM_GRAB_RADIUS;
|
|
|
|
}
|
|
|
|
var newTargetPosition = Vec3.multiply(this.grabRadius, Quat.getUp(worldControllerRotation));
|
|
|
|
newTargetPosition = Vec3.sum(newTargetPosition, worldControllerPosition);
|
|
|
|
newTargetPosition = Vec3.sum(newTargetPosition, this.offsetPosition);
|
|
|
|
|
|
|
|
// XXX
|
|
|
|
// this.maybeScale(grabbedProperties);
|
|
|
|
|
|
|
|
var distanceToObject = Vec3.length(Vec3.subtract(MyAvatar.position, this.currentObjectPosition));
|
|
|
|
|
|
|
|
this.linearTimeScale = (this.linearTimeScale / 2);
|
|
|
|
if (this.linearTimeScale <= DISTANCE_HOLDING_ACTION_TIMEFRAME) {
|
|
|
|
this.linearTimeScale = DISTANCE_HOLDING_ACTION_TIMEFRAME;
|
|
|
|
}
|
|
|
|
var success = Entities.updateAction(this.grabbedThingID, this.actionID, {
|
|
|
|
targetPosition: newTargetPosition,
|
|
|
|
linearTimeScale: this.linearTimeScale,
|
|
|
|
targetRotation: this.currentObjectRotation,
|
|
|
|
angularTimeScale: this.distanceGrabTimescale(this.mass, distanceToObject),
|
|
|
|
ttl: ACTION_TTL
|
|
|
|
});
|
|
|
|
if (!success) {
|
|
|
|
print("continueDistanceHolding -- updateAction failed: " + this.actionID);
|
|
|
|
this.actionID = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.previousRoomControllerPosition = roomControllerPosition;
|
|
|
|
};
|
|
|
|
|
2018-06-09 11:01:18 +12:00
|
|
|
this.endFarGrabAction = function () {
|
2017-08-15 11:45:45 -07:00
|
|
|
ensureDynamic(this.grabbedThingID);
|
2017-08-13 15:54:03 -07:00
|
|
|
this.distanceHolding = false;
|
|
|
|
this.distanceRotating = false;
|
|
|
|
Entities.deleteAction(this.grabbedThingID, this.actionID);
|
2017-09-15 10:50:46 -07:00
|
|
|
|
|
|
|
var args = [this.hand === RIGHT_HAND ? "right" : "left", MyAvatar.sessionUUID];
|
2017-09-15 12:07:50 -07:00
|
|
|
Entities.callEntityMethod(this.grabbedThingID, "releaseGrab", args);
|
2017-11-06 15:21:20 -08:00
|
|
|
if (this.targetObject) {
|
|
|
|
this.targetObject.restoreTargetEntityOriginalProps();
|
2017-10-10 09:17:01 -07:00
|
|
|
}
|
2017-08-13 15:54:03 -07:00
|
|
|
this.actionID = null;
|
|
|
|
this.grabbedThingID = null;
|
2017-11-06 15:21:20 -08:00
|
|
|
this.targetObject = null;
|
2017-08-13 15:54:03 -07:00
|
|
|
};
|
|
|
|
|
2017-11-01 11:36:36 -07:00
|
|
|
this.updateRecommendedArea = function() {
|
2017-10-02 16:46:43 -07:00
|
|
|
var dims = Controller.getViewportDimensions();
|
|
|
|
this.reticleMaxX = dims.x - MARGIN;
|
|
|
|
this.reticleMaxY = dims.y - MARGIN;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.calculateNewReticlePosition = function(intersection) {
|
|
|
|
this.updateRecommendedArea();
|
|
|
|
var point2d = HMD.overlayFromWorldPoint(intersection);
|
|
|
|
point2d.x = Math.max(this.reticleMinX, Math.min(point2d.x, this.reticleMaxX));
|
|
|
|
point2d.y = Math.max(this.reticleMinY, Math.min(point2d.y, this.reticleMaxY));
|
|
|
|
return point2d;
|
|
|
|
};
|
|
|
|
|
2018-10-03 17:40:42 -07:00
|
|
|
this.restoreIgnoredEntities = function() {
|
2018-10-08 13:10:41 -07:00
|
|
|
for (var i = 0; i < this.ignoredEntities.length; i++) {
|
2018-10-03 17:40:42 -07:00
|
|
|
var data = {
|
|
|
|
action: 'remove',
|
|
|
|
id: this.ignoredEntities[i]
|
|
|
|
};
|
2024-08-13 17:39:51 -07:00
|
|
|
Messages.sendMessage('Hifi-Hand-RayPick-Blocklist', JSON.stringify(data));
|
2018-10-03 17:40:42 -07:00
|
|
|
}
|
|
|
|
this.ignoredEntities = [];
|
|
|
|
};
|
|
|
|
|
2017-08-30 15:20:03 -07:00
|
|
|
this.notPointingAtEntity = function(controllerData) {
|
2017-08-25 17:40:30 -07:00
|
|
|
var intersection = controllerData.rayPicks[this.hand];
|
2023-12-08 23:01:01 +01:00
|
|
|
var entityProperty = Entities.getEntityProperties(intersection.objectID, "type");
|
2017-08-28 17:35:34 -07:00
|
|
|
var entityType = entityProperty.type;
|
2017-10-02 16:46:43 -07:00
|
|
|
var hudRayPick = controllerData.hudRayPicks[this.hand];
|
|
|
|
var point2d = this.calculateNewReticlePosition(hudRayPick.intersection);
|
2017-10-20 14:37:10 -07:00
|
|
|
if ((intersection.type === Picks.INTERSECTED_ENTITY && entityType === "Web") ||
|
|
|
|
intersection.type === Picks.INTERSECTED_OVERLAY || Window.isPointOnDesktopWindow(point2d)) {
|
2017-08-23 17:32:41 -07:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-31 18:06:55 -07:00
|
|
|
return false;
|
2017-08-23 17:32:41 -07:00
|
|
|
};
|
|
|
|
|
2017-08-28 17:35:34 -07:00
|
|
|
this.distanceRotate = function(otherFarGrabModule) {
|
2017-08-31 18:06:55 -07:00
|
|
|
this.distanceRotating = true;
|
2017-08-28 17:35:34 -07:00
|
|
|
this.distanceHolding = false;
|
|
|
|
|
|
|
|
var worldControllerRotation = getControllerWorldLocation(this.handToController(), true).orientation;
|
2017-09-15 10:50:46 -07:00
|
|
|
var controllerRotationDelta =
|
|
|
|
Quat.multiply(worldControllerRotation, Quat.inverse(this.previousWorldControllerRotation));
|
2017-08-28 17:35:34 -07:00
|
|
|
// Rotate entity by twice the delta rotation.
|
|
|
|
controllerRotationDelta = Quat.multiply(controllerRotationDelta, controllerRotationDelta);
|
2017-08-31 18:06:55 -07:00
|
|
|
|
2017-08-28 17:35:34 -07:00
|
|
|
// Perform the rotation in the translation controller's action update.
|
|
|
|
otherFarGrabModule.currentObjectRotation = Quat.multiply(controllerRotationDelta,
|
2017-08-31 18:06:55 -07:00
|
|
|
otherFarGrabModule.currentObjectRotation);
|
|
|
|
|
2017-08-28 17:35:34 -07:00
|
|
|
this.previousWorldControllerRotation = worldControllerRotation;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.prepareDistanceRotatingData = function(controllerData) {
|
|
|
|
var intersection = controllerData.rayPicks[this.hand];
|
2017-08-31 18:06:55 -07:00
|
|
|
|
2017-08-28 17:35:34 -07:00
|
|
|
var controllerLocation = getControllerWorldLocation(this.handToController(), true);
|
|
|
|
var worldControllerPosition = controllerLocation.position;
|
|
|
|
var worldControllerRotation = controllerLocation.orientation;
|
2017-08-31 18:06:55 -07:00
|
|
|
|
2023-12-08 23:01:01 +01:00
|
|
|
var grabbedProperties = Entities.getEntityProperties(intersection.objectID, "position");
|
2017-08-28 17:35:34 -07:00
|
|
|
this.currentObjectPosition = grabbedProperties.position;
|
|
|
|
this.grabRadius = intersection.distance;
|
|
|
|
|
|
|
|
// Offset between controller vector at the grab radius and the entity position.
|
|
|
|
var targetPosition = Vec3.multiply(this.grabRadius, Quat.getUp(worldControllerRotation));
|
|
|
|
targetPosition = Vec3.sum(targetPosition, worldControllerPosition);
|
|
|
|
this.offsetPosition = Vec3.subtract(this.currentObjectPosition, targetPosition);
|
2017-08-31 18:06:55 -07:00
|
|
|
|
2017-08-28 17:35:34 -07:00
|
|
|
// Initial controller rotation.
|
|
|
|
this.previousWorldControllerRotation = worldControllerRotation;
|
|
|
|
};
|
|
|
|
|
2018-02-08 17:08:51 -08:00
|
|
|
this.targetIsNull = function() {
|
2023-12-08 23:01:01 +01:00
|
|
|
var properties = Entities.getEntityProperties(this.grabbedThingID, "type");
|
2018-02-08 17:08:51 -08:00
|
|
|
if (Object.keys(properties).length === 0 && this.distanceHolding) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-03-02 14:23:35 -08:00
|
|
|
};
|
2018-02-08 17:08:51 -08:00
|
|
|
|
2017-08-12 17:27:48 -07:00
|
|
|
this.isReady = function (controllerData) {
|
2018-01-18 10:48:35 -08:00
|
|
|
if (HMD.active) {
|
2019-10-01 14:52:33 -07:00
|
|
|
if (handsAreTracked()) {
|
|
|
|
return makeRunningValues(false, [], []);
|
|
|
|
}
|
2018-01-18 10:48:35 -08:00
|
|
|
if (this.notPointingAtEntity(controllerData)) {
|
|
|
|
return makeRunningValues(false, [], []);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.distanceHolding = false;
|
|
|
|
this.distanceRotating = false;
|
|
|
|
|
|
|
|
if (controllerData.triggerValues[this.hand] > TRIGGER_ON_VALUE) {
|
|
|
|
this.prepareDistanceRotatingData(controllerData);
|
|
|
|
return makeRunningValues(true, [], []);
|
|
|
|
} else {
|
|
|
|
return makeRunningValues(false, [], []);
|
|
|
|
}
|
2017-08-12 17:27:48 -07:00
|
|
|
}
|
2018-01-18 10:48:35 -08:00
|
|
|
return makeRunningValues(false, [], []);
|
2017-08-12 17:27:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
this.run = function (controllerData) {
|
2018-10-08 16:59:39 -07:00
|
|
|
|
|
|
|
var intersection = controllerData.rayPicks[this.hand];
|
|
|
|
if (intersection.type === Picks.INTERSECTED_ENTITY && !Window.isPhysicsEnabled()) {
|
|
|
|
// add to ignored items.
|
|
|
|
if (this.ignoredEntities.indexOf(intersection.objectID) === -1) {
|
|
|
|
var data = {
|
|
|
|
action: 'add',
|
|
|
|
id: intersection.objectID
|
|
|
|
};
|
2024-08-13 17:39:51 -07:00
|
|
|
Messages.sendMessage('Hifi-Hand-RayPick-Blocklist', JSON.stringify(data));
|
2018-10-08 16:59:39 -07:00
|
|
|
this.ignoredEntities.push(intersection.objectID);
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 10:50:46 -07:00
|
|
|
if (controllerData.triggerValues[this.hand] < TRIGGER_OFF_VALUE ||
|
2018-10-08 16:59:39 -07:00
|
|
|
(this.notPointingAtEntity(controllerData) && Window.isPhysicsEnabled()) || this.targetIsNull()) {
|
2018-06-09 11:01:18 +12:00
|
|
|
this.endFarGrabAction();
|
2018-10-03 17:40:42 -07:00
|
|
|
this.restoreIgnoredEntities();
|
2017-08-13 11:41:12 -07:00
|
|
|
return makeRunningValues(false, [], []);
|
2017-08-12 17:27:48 -07:00
|
|
|
}
|
2017-09-28 11:18:40 -07:00
|
|
|
this.intersectionDistance = controllerData.rayPicks[this.hand].distance;
|
2017-09-08 17:07:27 -07:00
|
|
|
|
2017-08-31 18:06:55 -07:00
|
|
|
var otherModuleName =this.hand === RIGHT_HAND ? "LeftFarActionGrabEntity" : "RightFarActionGrabEntity";
|
|
|
|
var otherFarGrabModule = getEnabledModuleByName(otherModuleName);
|
|
|
|
|
2017-08-13 11:41:12 -07:00
|
|
|
// gather up the readiness of the near-grab modules
|
|
|
|
var nearGrabNames = [
|
2017-09-11 15:07:14 -07:00
|
|
|
this.hand === RIGHT_HAND ? "RightScaleAvatar" : "LeftScaleAvatar",
|
2017-09-06 14:58:04 -07:00
|
|
|
this.hand === RIGHT_HAND ? "RightFarTriggerEntity" : "LeftFarTriggerEntity",
|
2017-08-13 11:41:12 -07:00
|
|
|
this.hand === RIGHT_HAND ? "RightNearActionGrabEntity" : "LeftNearActionGrabEntity",
|
|
|
|
this.hand === RIGHT_HAND ? "RightNearParentingGrabEntity" : "LeftNearParentingGrabEntity",
|
2018-08-31 10:25:12 +12:00
|
|
|
this.hand === RIGHT_HAND ? "RightNearParentingGrabOverlay" : "LeftNearParentingGrabOverlay",
|
|
|
|
this.hand === RIGHT_HAND ? "RightNearTabletHighlight" : "LeftNearTabletHighlight"
|
2017-08-13 11:41:12 -07:00
|
|
|
];
|
2017-08-31 18:06:55 -07:00
|
|
|
|
2017-08-13 11:41:12 -07:00
|
|
|
var nearGrabReadiness = [];
|
|
|
|
for (var i = 0; i < nearGrabNames.length; i++) {
|
|
|
|
var nearGrabModule = getEnabledModuleByName(nearGrabNames[i]);
|
|
|
|
var ready = nearGrabModule ? nearGrabModule.isReady(controllerData) : makeRunningValues(false, [], []);
|
|
|
|
nearGrabReadiness.push(ready);
|
|
|
|
}
|
2017-08-12 17:27:48 -07:00
|
|
|
|
2017-08-13 15:54:03 -07:00
|
|
|
if (this.actionID) {
|
2018-06-09 11:32:10 +12:00
|
|
|
// if we are doing a distance grab and the object or tablet gets close enough to the controller,
|
2017-08-13 16:54:37 -07:00
|
|
|
// stop the far-grab so the near-grab or equip can take over.
|
|
|
|
for (var k = 0; k < nearGrabReadiness.length; k++) {
|
2018-08-29 12:35:14 -07:00
|
|
|
if (nearGrabReadiness[k].active && (nearGrabReadiness[k].targets[0] === this.grabbedThingID ||
|
|
|
|
HMD.tabletID && nearGrabReadiness[k].targets[0] === HMD.tabletID)) {
|
2018-06-09 11:01:18 +12:00
|
|
|
this.endFarGrabAction();
|
2018-10-03 17:40:42 -07:00
|
|
|
this.restoreIgnoredEntities();
|
2017-08-13 16:54:37 -07:00
|
|
|
return makeRunningValues(false, [], []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 15:54:03 -07:00
|
|
|
this.continueDistanceHolding(controllerData);
|
|
|
|
} else {
|
2017-08-13 16:54:37 -07:00
|
|
|
// if we are doing a distance search and this controller moves into a position
|
|
|
|
// where it could near-grab something, stop searching.
|
|
|
|
for (var j = 0; j < nearGrabReadiness.length; j++) {
|
|
|
|
if (nearGrabReadiness[j].active) {
|
2018-06-09 11:01:18 +12:00
|
|
|
this.endFarGrabAction();
|
2018-10-03 17:40:42 -07:00
|
|
|
this.restoreIgnoredEntities();
|
2017-08-13 16:54:37 -07:00
|
|
|
return makeRunningValues(false, [], []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 15:54:03 -07:00
|
|
|
var rayPickInfo = controllerData.rayPicks[this.hand];
|
2017-10-20 14:37:10 -07:00
|
|
|
if (rayPickInfo.type === Picks.INTERSECTED_ENTITY) {
|
2017-08-30 15:20:03 -07:00
|
|
|
if (controllerData.triggerClicks[this.hand]) {
|
|
|
|
var entityID = rayPickInfo.objectID;
|
2018-08-29 12:35:14 -07:00
|
|
|
var targetProps = Entities.getEntityProperties(entityID, DISPATCHER_PROPERTIES);
|
2018-03-01 16:16:47 -08:00
|
|
|
if (targetProps.href !== "") {
|
|
|
|
AddressManager.handleLookupString(targetProps.href);
|
2018-10-03 17:40:42 -07:00
|
|
|
this.restoreIgnoredEntities();
|
2018-03-01 16:16:47 -08:00
|
|
|
return makeRunningValues(false, [], []);
|
|
|
|
}
|
2017-08-31 18:06:55 -07:00
|
|
|
|
2017-11-06 15:21:20 -08:00
|
|
|
this.targetObject = new TargetObject(entityID, targetProps);
|
|
|
|
this.targetObject.parentProps = getEntityParents(targetProps);
|
2018-01-03 16:03:37 -08:00
|
|
|
|
2020-08-30 03:23:22 -07:00
|
|
|
Selection.removeFromSelectedItemsList("contextOverlayHighlightList", "entity", entityID);
|
2018-01-03 16:03:37 -08:00
|
|
|
|
2017-11-06 15:21:20 -08:00
|
|
|
var targetEntity = this.targetObject.getTargetEntity();
|
|
|
|
entityID = targetEntity.id;
|
|
|
|
targetProps = targetEntity.props;
|
2017-09-05 12:06:32 -07:00
|
|
|
|
2018-03-27 14:05:43 -07:00
|
|
|
if (entityIsGrabbable(targetProps) || entityIsGrabbable(this.targetObject.entityProps)) {
|
2017-10-10 09:17:01 -07:00
|
|
|
if (!entityIsDistanceGrabbable(targetProps)) {
|
2017-11-06 15:21:20 -08:00
|
|
|
this.targetObject.makeDynamic();
|
2017-10-10 09:17:01 -07:00
|
|
|
}
|
2017-10-25 11:29:32 -07:00
|
|
|
|
2017-08-30 15:20:03 -07:00
|
|
|
if (!this.distanceRotating) {
|
|
|
|
this.grabbedThingID = entityID;
|
|
|
|
this.grabbedDistance = rayPickInfo.distance;
|
|
|
|
}
|
2017-10-10 09:43:02 -07:00
|
|
|
|
2018-03-02 14:23:35 -08:00
|
|
|
if (otherFarGrabModule.grabbedThingID === this.grabbedThingID &&
|
|
|
|
otherFarGrabModule.distanceHolding) {
|
2017-09-27 11:47:26 -07:00
|
|
|
this.prepareDistanceRotatingData(controllerData);
|
2017-08-30 15:20:03 -07:00
|
|
|
this.distanceRotate(otherFarGrabModule);
|
|
|
|
} else {
|
|
|
|
this.distanceHolding = true;
|
|
|
|
this.distanceRotating = false;
|
|
|
|
this.startFarGrabAction(controllerData, targetProps);
|
|
|
|
}
|
2017-08-13 15:54:03 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-28 17:35:34 -07:00
|
|
|
} else if (this.distanceRotating) {
|
|
|
|
this.distanceRotate(otherFarGrabModule);
|
2017-08-30 15:20:03 -07:00
|
|
|
}
|
2017-08-13 15:54:03 -07:00
|
|
|
}
|
2017-11-13 16:42:16 -08:00
|
|
|
return this.exitIfDisabled(controllerData);
|
2017-09-01 17:07:26 -07:00
|
|
|
};
|
|
|
|
|
2017-11-13 16:42:16 -08:00
|
|
|
this.exitIfDisabled = function(controllerData) {
|
2017-09-01 17:07:26 -07:00
|
|
|
var moduleName = this.hand === RIGHT_HAND ? "RightDisableModules" : "LeftDisableModules";
|
|
|
|
var disableModule = getEnabledModuleByName(moduleName);
|
|
|
|
if (disableModule) {
|
|
|
|
if (disableModule.disableModules) {
|
2018-06-09 11:01:18 +12:00
|
|
|
this.endFarGrabAction();
|
2018-04-04 12:55:28 -07:00
|
|
|
Selection.removeFromSelectedItemsList(DISPATCHER_HOVERING_LIST, "entity",
|
|
|
|
this.highlightedEntity);
|
2018-04-05 15:38:56 -07:00
|
|
|
this.highlightedEntity = null;
|
2018-10-03 17:40:42 -07:00
|
|
|
this.restoreIgnoredEntities();
|
2017-09-01 17:07:26 -07:00
|
|
|
return makeRunningValues(false, [], []);
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 16:42:16 -08:00
|
|
|
var grabbedThing = (this.distanceHolding || this.distanceRotating) ? this.targetObject.entityID : null;
|
|
|
|
var offset = this.calculateOffset(controllerData);
|
|
|
|
var laserLockInfo = makeLaserLockInfo(grabbedThing, false, this.hand, offset);
|
2017-10-30 16:58:14 -07:00
|
|
|
return makeRunningValues(true, [], [], laserLockInfo);
|
2017-08-12 17:27:48 -07:00
|
|
|
};
|
|
|
|
|
2017-11-13 16:42:16 -08:00
|
|
|
this.calculateOffset = function(controllerData) {
|
|
|
|
if (this.distanceHolding || this.distanceRotating) {
|
2018-08-27 12:47:17 -07:00
|
|
|
var targetProps = Entities.getEntityProperties(this.targetObject.entityID,
|
|
|
|
[ "position", "rotation", "registrationPoint", "dimensions" ]);
|
|
|
|
return worldPositionToRegistrationFrameMatrix(targetProps, controllerData.rayPicks[this.hand].intersection);
|
2017-11-13 16:42:16 -08:00
|
|
|
}
|
|
|
|
return undefined;
|
2017-08-12 17:27:48 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var leftFarActionGrabEntity = new FarActionGrabEntity(LEFT_HAND);
|
|
|
|
var rightFarActionGrabEntity = new FarActionGrabEntity(RIGHT_HAND);
|
|
|
|
|
|
|
|
enableDispatcherModule("LeftFarActionGrabEntity", leftFarActionGrabEntity);
|
|
|
|
enableDispatcherModule("RightFarActionGrabEntity", rightFarActionGrabEntity);
|
|
|
|
|
2017-11-01 11:03:48 -07:00
|
|
|
function cleanup() {
|
2017-08-12 17:27:48 -07:00
|
|
|
disableDispatcherModule("LeftFarActionGrabEntity");
|
|
|
|
disableDispatcherModule("RightFarActionGrabEntity");
|
2017-11-01 11:36:36 -07:00
|
|
|
}
|
2017-11-01 11:03:48 -07:00
|
|
|
Script.scriptEnding.connect(cleanup);
|
2017-08-12 17:27:48 -07:00
|
|
|
}());
|