Modified handControllerGrab script to trigger entity touch events for non-physical entities

This commit is contained in:
ericrius1 2015-09-22 09:38:06 -07:00
parent 7b5cbbf758
commit b9db495ebf

View file

@ -29,9 +29,21 @@ var TRIGGER_ON_VALUE = 0.2;
var DISTANCE_HOLDING_RADIUS_FACTOR = 5; // multiplied by distance between hand and object var DISTANCE_HOLDING_RADIUS_FACTOR = 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_ACTION_TIMEFRAME = 0.1; // how quickly objects move to their new position
var DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR = 2.0; // object rotates this much more than hand did var DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR = 2.0; // object rotates this much more than hand did
var NO_INTERSECT_COLOR = {red: 10, green: 10, blue: 255}; // line color when pick misses var NO_INTERSECT_COLOR = {
var INTERSECT_COLOR = {red: 250, green: 10, blue: 10}; // line color when pick hits red: 10,
var LINE_ENTITY_DIMENSIONS = {x: 1000, y: 1000, z: 1000}; green: 10,
blue: 255
}; // line color when pick misses
var INTERSECT_COLOR = {
red: 250,
green: 10,
blue: 10
}; // line color when pick hits
var LINE_ENTITY_DIMENSIONS = {
x: 1000,
y: 1000,
z: 1000
};
var LINE_LENGTH = 500; var LINE_LENGTH = 500;
@ -54,7 +66,11 @@ var RELEASE_VELOCITY_MULTIPLIER = 1.5; // affects throwing things
var RIGHT_HAND = 1; var RIGHT_HAND = 1;
var LEFT_HAND = 0; var LEFT_HAND = 0;
var ZERO_VEC = {x: 0, y: 0, z: 0}; var ZERO_VEC = {
x: 0,
y: 0,
z: 0
};
var NULL_ACTION_ID = "{00000000-0000-0000-000000000000}"; var NULL_ACTION_ID = "{00000000-0000-0000-000000000000}";
var MSEC_PER_SEC = 1000.0; var MSEC_PER_SEC = 1000.0;
@ -68,7 +84,9 @@ var STATE_DISTANCE_HOLDING = 1;
var STATE_CONTINUE_DISTANCE_HOLDING = 2; var STATE_CONTINUE_DISTANCE_HOLDING = 2;
var STATE_NEAR_GRABBING = 3; var STATE_NEAR_GRABBING = 3;
var STATE_CONTINUE_NEAR_GRABBING = 4; var STATE_CONTINUE_NEAR_GRABBING = 4;
var STATE_RELEASE = 5; var STATE_NEAR_TOUCHING = 5;
var STATE_CONTINUE_NEAR_TOUCHING = 6;
var STATE_RELEASE = 7;
var GRAB_USER_DATA_KEY = "grabKey"; var GRAB_USER_DATA_KEY = "grabKey";
@ -109,6 +127,12 @@ function controller(hand, triggerAction) {
case STATE_CONTINUE_NEAR_GRABBING: case STATE_CONTINUE_NEAR_GRABBING:
this.continueNearGrabbing(); this.continueNearGrabbing();
break; break;
case STATE_NEAR_TOUCHING:
this.nearTouching();
break;
case STATE_CONTINUE_NEAR_TOUCHING:
this.continueNearTouching();
break;
case STATE_RELEASE: case STATE_RELEASE:
this.release(); this.release();
break; break;
@ -171,7 +195,10 @@ function controller(hand, triggerAction) {
// the trigger is being pressed, do a ray test // the trigger is being pressed, do a ray test
var handPosition = this.getHandPosition(); var handPosition = this.getHandPosition();
var pickRay = {origin: handPosition, direction: Quat.getUp(this.getHandRotation())}; var pickRay = {
origin: handPosition,
direction: Quat.getUp(this.getHandRotation())
};
var intersection = Entities.findRayIntersection(pickRay, true); var intersection = Entities.findRayIntersection(pickRay, true);
if (intersection.intersects && if (intersection.intersects &&
intersection.properties.collisionsWillMove === 1 && intersection.properties.collisionsWillMove === 1 &&
@ -196,17 +223,18 @@ function controller(hand, triggerAction) {
for (var i = 0; i < nearbyEntities.length; i++) { for (var i = 0; i < nearbyEntities.length; i++) {
var props = Entities.getEntityProperties(nearbyEntities[i]); var props = Entities.getEntityProperties(nearbyEntities[i]);
var distance = Vec3.distance(props.position, handPosition); var distance = Vec3.distance(props.position, handPosition);
if (distance < minDistance && props.name !== "pointer" && if (distance < minDistance && props.name !== "pointer") {
props.collisionsWillMove === 1 &&
props.locked === 0) {
this.grabbedEntity = nearbyEntities[i]; this.grabbedEntity = nearbyEntities[i];
minDistance = distance; minDistance = distance;
} }
} }
if (this.grabbedEntity === null) { if (this.grabbedEntity === null) {
this.lineOn(pickRay.origin, Vec3.multiply(pickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR); this.lineOn(pickRay.origin, Vec3.multiply(pickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR);
} else { } else if (props.locked === 0 && props.collisionsWillMove === 1) {
this.state = STATE_NEAR_GRABBING; this.state = STATE_NEAR_GRABBING;
} else if (props.collisionsWillMove === 0) {
// We have grabbed a non-physical object, so we want to trigger a touch event as opposed to a grab event
this.state = STATE_NEAR_TOUCHING;
} }
} }
} }
@ -289,8 +317,10 @@ function controller(hand, triggerAction) {
Entities.callEntityMethod(this.grabbedEntity, "continueDistantGrab"); Entities.callEntityMethod(this.grabbedEntity, "continueDistantGrab");
Entities.updateAction(this.grabbedEntity, this.actionID, { Entities.updateAction(this.grabbedEntity, this.actionID, {
targetPosition: this.currentObjectPosition, linearTimeScale: DISTANCE_HOLDING_ACTION_TIMEFRAME, targetPosition: this.currentObjectPosition,
targetRotation: this.currentObjectRotation, angularTimeScale: DISTANCE_HOLDING_ACTION_TIMEFRAME linearTimeScale: DISTANCE_HOLDING_ACTION_TIMEFRAME,
targetRotation: this.currentObjectRotation,
angularTimeScale: DISTANCE_HOLDING_ACTION_TIMEFRAME
}); });
} }
@ -339,6 +369,23 @@ function controller(hand, triggerAction) {
this.currentObjectTime = Date.now(); this.currentObjectTime = Date.now();
} }
this.nearTouching = function() {
if (!this.triggerSmoothedSqueezed()) {
this.state = STATE_RELEASE;
return;
}
Entities.callEntityMethod(this.grabbedEntity, "startNearTouch")
this.state = STATE_CONTINUE_NEAR_TOUCHING;
}
this.continueNearTouching = function() {
if (!this.triggerSmoothedSqueezed()) {
this.state = STATE_RELEASE;
return;
}
Entities.callEntityMethod(this.grabbedEntity, "continueNearTouch");
}
this.continueNearGrabbing = function() { this.continueNearGrabbing = function() {
if (!this.triggerSmoothedSqueezed()) { if (!this.triggerSmoothedSqueezed()) {
@ -367,8 +414,7 @@ function controller(hand, triggerAction) {
// value would otherwise give the held object time to slow down. // value would otherwise give the held object time to slow down.
if (this.triggerSqueezed()) { if (this.triggerSqueezed()) {
this.grabbedVelocity = this.grabbedVelocity =
Vec3.sum(Vec3.multiply(this.grabbedVelocity, Vec3.sum(Vec3.multiply(this.grabbedVelocity, (1.0 - NEAR_GRABBING_VELOCITY_SMOOTH_RATIO)),
(1.0 - NEAR_GRABBING_VELOCITY_SMOOTH_RATIO)),
Vec3.multiply(grabbedVelocity, NEAR_GRABBING_VELOCITY_SMOOTH_RATIO)); Vec3.multiply(grabbedVelocity, NEAR_GRABBING_VELOCITY_SMOOTH_RATIO));
} }
@ -389,7 +435,9 @@ function controller(hand, triggerAction) {
// the action will tend to quickly bring an object's velocity to zero. now that // 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. // the action is gone, set the objects velocity to something the holder might expect.
Entities.editEntity(this.grabbedEntity, {velocity: this.grabbedVelocity}); Entities.editEntity(this.grabbedEntity, {
velocity: this.grabbedVelocity
});
this.deactivateEntity(this.grabbedEntity); this.deactivateEntity(this.grabbedEntity);
this.grabbedVelocity = ZERO_VEC; this.grabbedVelocity = ZERO_VEC;