Merge pull request #11401 from druiz17/module-scale-entities

ScaleEntity Module
This commit is contained in:
Brad Hefta-Gaub 2017-09-25 09:10:32 -07:00 committed by GitHub
commit 030da7d850
5 changed files with 114 additions and 12 deletions

View file

@ -44,10 +44,6 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
return (this.hand === RIGHT_HAND) ? leftNearParentingGrabEntity : rightNearParentingGrabEntity;
};
this.otherHandIsParent = function(props) {
return this.getOtherModule().thisHandIsParent(props);
};
this.thisHandIsParent = function(props) {
if (props.parentID !== MyAvatar.sessionUUID && props.parentID !== AVATAR_SELF_ID) {
return false;
@ -99,12 +95,6 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
// this should never happen, but if it does, don't set previous parent to be this hand.
// this.previousParentID[targetProps.id] = NULL;
// this.previousParentJointIndex[targetProps.id] = -1;
} else if (this.otherHandIsParent(targetProps)) {
// the other hand is parent. Steal the object and information
var otherModule = this.getOtherModule();
this.previousParentID[targetProps.id] = otherModule.previousParentID[targetProps.id];
this.previousParentJointIndex[targetProps.id] = otherModule.previousParentJointIndex[targetProps.id];
otherModule.endNearParentingGrabEntity();
} else {
this.previousParentID[targetProps.id] = targetProps.parentID;
this.previousParentJointIndex[targetProps.id] = targetProps.parentJointIndex;

View file

@ -1,4 +1,4 @@
// handControllerGrab.js
// scaleAvatar.js
//
// Created by Dante Ruiz on 9/11/17
//
@ -80,4 +80,5 @@
dispatcherUtils.disableDispatcherModule("LeftScaleAvatar");
dispatcherUtils.disableDispatcherModule("RightScaleAvatar");
};
Script.scriptEnding.connect(this.cleanup);
})();

View file

@ -0,0 +1,106 @@
// scaleEntity.js
//
// Created by Dante Ruiz on 9/18/17
//
// Grabs physically moveable entities with hydra-like controllers; it works for either near or far objects.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
/* global Script, Vec3, MyAvatar, RIGHT_HAND */
/* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */
(function() {
var dispatcherUtils = Script.require("/~/system/libraries/controllerDispatcherUtils.js");
function ScaleEntity(hand) {
this.hand = hand;
this.grabbedThingID = false;
this.scalingStartDistance = false;
this.scalingStartDimensions = false;
this.parameters = dispatcherUtils.makeDispatcherModuleParameters(
120,
this.hand === RIGHT_HAND ? ["rightHandTrigger"] : ["leftHandTrigger"],
[],
100
);
this.otherHand = function() {
return this.hand === dispatcherUtils.RIGHT_HAND ? dispatcherUtils.LEFT_HAND : dispatcherUtils.RIGHT_HAND;
};
this.otherModule = function() {
return this.hand === dispatcherUtils.RIGHT_HAND ? leftScaleEntity : rightScaleEntity;
};
this.bumperPressed = function(controllerData) {
return ( controllerData.secondaryValues[this.hand] > dispatcherUtils.BUMPER_ON_VALUE);
};
this.getTargetProps = function(controllerData) {
// nearbyEntityProperties is already sorted by length from controller
var nearbyEntityProperties = controllerData.nearbyEntityProperties[this.hand];
var sensorScaleFactor = MyAvatar.sensorToWorldScale;
for (var i = 0; i < nearbyEntityProperties.length; i++) {
var props = nearbyEntityProperties[i];
var handPosition = controllerData.controllerLocations[this.hand].position;
var distance = Vec3.distance(props.position, handPosition);
if (distance > dispatcherUtils.NEAR_GRAB_RADIUS * sensorScaleFactor) {
continue;
}
if ((dispatcherUtils.entityIsGrabbable(props) ||
dispatcherUtils.propsArePhysical(props)) && !props.locked) {
return props;
}
}
return null;
};
this.isReady = function(controllerData) {
var otherModule = this.otherModule();
if (this.bumperPressed(controllerData) && otherModule.bumperPressed(controllerData)) {
var thisHandTargetProps = this.getTargetProps(controllerData);
var otherHandTargetProps = otherModule.getTargetProps(controllerData);
if (thisHandTargetProps && otherHandTargetProps) {
if (thisHandTargetProps.id === otherHandTargetProps.id) {
this.grabbedThingID = thisHandTargetProps.id;
this.scalingStartDistance = Vec3.length(Vec3.subtract(controllerData.controllerLocations[this.hand].position,
controllerData.controllerLocations[this.otherHand()].position));
this.scalingStartDimensions = thisHandTargetProps.dimensions;
return dispatcherUtils.makeRunningValues(true, [], []);
}
}
}
return dispatcherUtils.makeRunningValues(false, [], []);
};
this.run = function(controllerData) {
var otherModule = this.otherModule();
if (this.bumperPressed(controllerData) && otherModule.bumperPressed(controllerData)) {
if (this.hand === dispatcherUtils.RIGHT_HAND) {
var scalingCurrentDistance =
Vec3.length(Vec3.subtract(controllerData.controllerLocations[this.hand].position,
controllerData.controllerLocations[this.otherHand()].position));
var currentRescale = scalingCurrentDistance / this.scalingStartDistance;
var newDimensions = Vec3.multiply(currentRescale, this.scalingStartDimensions);
Entities.editEntity(this.grabbedThingID, { dimensions: newDimensions });
}
return dispatcherUtils.makeRunningValues(true, [], []);
}
return dispatcherUtils.makeRunningValues(false, [], []);
};
}
var leftScaleEntity = new ScaleEntity(dispatcherUtils.LEFT_HAND);
var rightScaleEntity = new ScaleEntity(dispatcherUtils.RIGHT_HAND);
dispatcherUtils.enableDispatcherModule("LeftScaleEntity", leftScaleEntity);
dispatcherUtils.enableDispatcherModule("RightScaleEntity", rightScaleEntity);
this.cleanup = function() {
dispatcherUtils.disableDispatcherModule("LeftScaleEntity");
dispatcherUtils.disableDispatcherModule("RightScaleEntity");
};
Script.scriptEnding.connect(this.cleanup);
})();

View file

@ -29,7 +29,8 @@ var CONTOLLER_SCRIPTS = [
"controllerModules/disableOtherModule.js",
"controllerModules/farTrigger.js",
"controllerModules/teleport.js",
"controllerModules/scaleAvatar.js"
"controllerModules/scaleAvatar.js",
"controllerModules/scaleEntity.js"
];
var DEBUG_MENU_ITEM = "Debug defaultScripts.js";

View file

@ -319,7 +319,11 @@ if (typeof module !== 'undefined') {
LEFT_HAND: LEFT_HAND,
RIGHT_HAND: RIGHT_HAND,
BUMPER_ON_VALUE: BUMPER_ON_VALUE,
propsArePhysical: propsArePhysical,
entityIsGrabbable: entityIsGrabbable,
NEAR_GRAB_RADIUS: NEAR_GRAB_RADIUS,
projectOntoOverlayXYPlane: projectOntoOverlayXYPlane,
projectOntoEntityXYPlane: projectOntoEntityXYPlane
};
}