safe to use grabHockey.js to grab misc objects

This commit is contained in:
Andrew Meadows 2015-05-18 22:32:20 -07:00
parent bb1fe8f439
commit b60907570a

View file

@ -10,7 +10,6 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
var adebug = 0;
// these are hand-measured bounds of the AirHockey table // these are hand-measured bounds of the AirHockey table
var fieldMaxOffset = { var fieldMaxOffset = {
x: 0.475, x: 0.475,
@ -30,12 +29,14 @@ var tablePosition = {
z: 0 z: 0
} }
var isGrabbing = false; var isGrabbing = false;
var isGrabbingPaddle = false;
var grabbedEntity = null; var grabbedEntity = null;
var prevMouse = {}; var prevMouse = {};
var deltaMouse = { var deltaMouse = {
z: 0 z: 0
} }
var MAX_GRAB_DISTANCE = 100;
var TABLE_SEARCH_RANGE = 10; var TABLE_SEARCH_RANGE = 10;
var entityProps; var entityProps;
var moveUpDown = false; var moveUpDown = false;
@ -47,6 +48,9 @@ var ANGULAR_DAMPING_RATE = 0.40;
var SCREEN_TO_METERS = 0.001; var SCREEN_TO_METERS = 0.001;
var currentPosition, currentVelocity, cameraEntityDistance, currentRotation; var currentPosition, currentVelocity, cameraEntityDistance, currentRotation;
var grabHeight; var grabHeight;
var initialVerticalGrabPosition;
var MAX_VERTICAL_ANGLE = Math.PI / 3;
var MIN_VERTICAL_ANGLE = - MAX_VERTICAL_ANGLE;
var velocityTowardTarget, desiredVelocity, addedVelocity, newVelocity, dPosition, camYaw, distanceToTarget, targetPosition; var velocityTowardTarget, desiredVelocity, addedVelocity, newVelocity, dPosition, camYaw, distanceToTarget, targetPosition;
var grabOffset; var grabOffset;
var originalGravity = { var originalGravity = {
@ -97,20 +101,50 @@ function nearLinePoint(targetPosition) {
} }
function xzPickRayIntersetion(pointOnPlane, mouseX, mouseY) { function xzPickRayIntersetion(pointOnPlane, mouseX, mouseY) {
var relativePosition = Vec3.subtract(pointOnPlane, Camera.getPosition()); var relativePoint = Vec3.subtract(pointOnPlane, Camera.getPosition());
var pickRay = Camera.computePickRay(mouseX, mouseY); var pickRay = Camera.computePickRay(mouseX, mouseY);
if (Math.abs(pickRay.direction.y) > 0.001) { if (Math.abs(pickRay.direction.y) > 0.001) {
var length = relativePosition.y / pickRay.direction.y; var distance = relativePoint.y / pickRay.direction.y;
var pickInersection = Vec3.multiply(pickRay.direction, length); var pickInersection = Vec3.multiply(pickRay.direction, distance);
pickInersection = Vec3.sum(Camera.getPosition(), pickInersection); pickInersection = Vec3.sum(Camera.getPosition(), pickInersection);
return pickInersection; return pickInersection;
} }
// point and line are more-or-less co-planar: compute closest approach of pickRay and pointOnPlane // point and line are more-or-less co-planar: compute closest approach of pickRay and pointOnPlane
var length = Vec3.dot(relativePosition, pickRay.direction); var distance = Vec3.dot(relativePoint, pickRay.direction);
var pickInersection = Vec3.multiply(pickRay.direction, length); var pickInersection = Vec3.multiply(pickRay.direction, distance);
return pickInersection; return pickInersection;
} }
function forwardPickRayIntersection(pointOnPlane, mouseX, mouseY) {
var relativePoint = Vec3.subtract(pointOnPlane, Camera.getPosition());
var pickRay = Camera.computePickRay(mouseX, mouseY);
var planeNormal = Quat.getFront(Camera.getOrientation());
var distance = Vec3.dot(planeNormal, relativePoint);
var rayDistance = Vec3.dot(planeNormal, pickRay.direction);
var pickIntersection = Vec3.multiply(pickRay.direction, distance / rayDistance);
pickIntersection = Vec3.sum(pickIntersection, Camera.getPosition())
return pickIntersection;
}
/*
function yCylinderPickRayIntersection(grabRadius, mouseX, mouseY) {
var pickRay = Camera.computePickRay(mouseX, mouseY);
var angle = Math.asin(pickRay.direction.y);
if (angle > MAX_VERTICAL_ANGLE) {
angle = MAX_VERTICAL_ANGLE;
} else if (angle < MIN_VERTICAL_ANGLE) {
angle = MIN_VERTICAL_ANGLE;
}
var horizontalNormal = pickRay.direction;
horizontalNormal.y = 0;
horizontalNormal = Vec3.normalize(horizontalNormal);
var pickIntersection = Vec3.multiply(horizontalNormal, grabRadius);
pickIntersection.y = grabRadius * Math.tan(angle);
pickIntersection = Vec3.sum(pickIntersection, Camera.getPosition())
return pickIntersection;
}
*/
function mousePressEvent(event) { function mousePressEvent(event) {
if (!event.isLeftButton) { if (!event.isLeftButton) {
return; return;
@ -126,10 +160,17 @@ function mousePressEvent(event) {
if (pickResults.properties.collisionsWillMove) { if (pickResults.properties.collisionsWillMove) {
grabbedEntity = pickResults.entityID; grabbedEntity = pickResults.entityID;
var props = Entities.getEntityProperties(grabbedEntity) var props = Entities.getEntityProperties(grabbedEntity)
isGrabbing = true;
originalGravity = props.gravity; originalGravity = props.gravity;
var objectPosition = props.position; var objectPosition = props.position;
currentPosition = props.position; currentPosition = props.position;
if (Vec3.distance(currentPosition, Camera.getPosition()) > MAX_GRAB_DISTANCE) {
// don't allow grabs of things far away
return;
}
isGrabbing = true;
isGrabbingPaddle = (props.name == "air-hockey-paddle-23j4h1jh82jsjfw91jf232n2k");
currentVelocity = props.velocity; currentVelocity = props.velocity;
updateDropLine(objectPosition); updateDropLine(objectPosition);
@ -140,6 +181,7 @@ function mousePressEvent(event) {
// remember the height of the object when first grabbed // remember the height of the object when first grabbed
// we'll try to maintain this height during the rest of this grab // we'll try to maintain this height during the rest of this grab
grabHeight = currentPosition.y; grabHeight = currentPosition.y;
initialVerticalGrabPosition = currentPosition;
Entities.editEntity(grabbedEntity, { Entities.editEntity(grabbedEntity, {
gravity: { gravity: {
@ -242,10 +284,15 @@ function mouseMoveEvent(event) {
theta = 2 * Math.acos(dQ.w); theta = 2 * Math.acos(dQ.w);
axisAngle = Quat.axis(dQ); axisAngle = Quat.axis(dQ);
angularVelocity = Vec3.multiply((theta / dT), axisAngle); angularVelocity = Vec3.multiply((theta / dT), axisAngle);
} else {
if (moveUpDown) {
targetPosition = forwardPickRayIntersection(currentPosition, event.x, event.y);
grabHeight = targetPosition.y;
} else { } else {
var pointOnPlane = xzPickRayIntersetion(currentPosition, event.x, event.y); var pointOnPlane = xzPickRayIntersetion(currentPosition, event.x, event.y);
pointOnPlane = Vec3.subtract(pointOnPlane, grabOffset); pointOnPlane = Vec3.subtract(pointOnPlane, grabOffset);
if (isGrabbingPaddle) {
// translate pointOnPlane into local-frame // translate pointOnPlane into local-frame
pointOnPlane = Vec3.subtract(pointOnPlane, tablePosition); pointOnPlane = Vec3.subtract(pointOnPlane, tablePosition);
@ -280,11 +327,23 @@ function mouseMoveEvent(event) {
// translate into world-frame // translate into world-frame
pointOnPlane = Vec3.sum(tablePosition, pointOnPlane); pointOnPlane = Vec3.sum(tablePosition, pointOnPlane);
} else {
// we're grabbing a non-paddle object
// clamp to gragHeight // clamp distance
var relativePosition = Vec3.subtract(pointOnPlane, Camera.getPosition());
var length = Vec3.length(relativePosition);
if (length > MAX_GRAB_DISTANCE) {
relativePosition = Vec3.multiply(relativePosition, MAX_GRAB_DISTANCE / length);
pointOnPlane = Vec3.sum(relativePosition, Camera.getPosition());
}
}
// clamp to grabHeight
pointOnPlane.y = grabHeight; pointOnPlane.y = grabHeight;
targetPosition = pointOnPlane; targetPosition = pointOnPlane;
initialVerticalGrabPosition = targetPosition;
}
} }
} }
prevMouse.x = event.x; prevMouse.x = event.x;