diff --git a/examples/grab.js b/examples/grab.js index efacc26612..552a194100 100644 --- a/examples/grab.js +++ b/examples/grab.js @@ -27,7 +27,7 @@ var ANGULAR_DAMPING_RATE = 0.40; var SCREEN_TO_METERS = 0.001; var currentPosition, currentVelocity, cameraEntityDistance, currentRotation; var velocityTowardTarget, desiredVelocity, addedVelocity, newVelocity, dPosition, camYaw, distanceToTarget, targetPosition; -var originalGravity; +var originalGravity = {x: 0, y: 0, z: 0}; var shouldRotate = false; var dQ, theta, axisAngle, dT; var angularVelocity = { @@ -56,6 +56,15 @@ var dropLine = Overlays.addOverlay("line3d", { }); +function vectorIsZero(v) { + return v.x == 0 && v.y == 0 && v.z == 0; +} + +function vectorToString(v) { + return "(" + v.x + ", " + v.y + ", " + v.z + ")" +} + + function mousePressEvent(event) { if (!event.isLeftButton) { return; @@ -67,10 +76,16 @@ function mousePressEvent(event) { var props = Entities.getEntityProperties(grabbedEntity) isGrabbing = true; originalGravity = props.gravity; + print("mouse-press setting originalGravity " + originalGravity + " " + vectorToString(originalGravity)); targetPosition = props.position; currentPosition = props.position; currentVelocity = props.velocity; updateDropLine(targetPosition); + + Entities.editEntity(grabbedEntity, { + gravity: {x: 0, y: 0, z: 0} + }); + Audio.playSound(grabSound, { position: props.position, volume: 0.4 @@ -99,9 +114,20 @@ function mouseReleaseEvent() { if (isGrabbing) { isGrabbing = false; - Entities.editEntity(grabbedEntity, { - gravity: originalGravity - }); + // only restore the original gravity if it's not zero. This is to avoid... + // 1. interface A grabs an entity and locally saves off its gravity + // 2. interface A sets the entity's gravity to zero + // 3. interface B grabs the entity and saves off its gravity (which is zero) + // 4. interface A releases the entity and puts the original gravity back + // 5. interface B releases the entity and puts the original gravity back (to zero) + if (!vectorIsZero(originalGravity)) { + print("mouse-release restoring originalGravity" + vectorToString(originalGravity)); + Entities.editEntity(grabbedEntity, { + gravity: originalGravity + }); + } else { + print("mouse-release not restoring originalGravity of zero"); + } Overlays.editOverlay(dropLine, { visible: false @@ -117,6 +143,13 @@ function mouseReleaseEvent() { function mouseMoveEvent(event) { if (isGrabbing) { + // see if something added/restored gravity + var props = Entities.getEntityProperties(grabbedEntity); + if (!vectorIsZero(props.gravity)) { + originalGravity = props.gravity; + print("mouse-move adopting originalGravity" + vectorToString(originalGravity)); + } + deltaMouse.x = event.x - prevMouse.x; if (!moveUpDown) { deltaMouse.z = event.y - prevMouse.y; @@ -211,8 +244,7 @@ function update(deltaTime) { Entities.editEntity(grabbedEntity, { velocity: newVelocity, - angularVelocity: angularVelocity, - gravity: {x: 0, y: 0, z: 0} + angularVelocity: angularVelocity }) updateDropLine(targetPosition); }