From 4b5e633258bd293ad8b0b936b275639fa6522690 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 10 Feb 2014 19:48:51 -0800 Subject: [PATCH] tweaks to hydraMove.js, added scale to MyAvatar JS --- examples/hydraMove.js | 178 +++++++++++++++++------------ libraries/avatars/src/AvatarData.h | 1 + 2 files changed, 109 insertions(+), 70 deletions(-) diff --git a/examples/hydraMove.js b/examples/hydraMove.js index 252d3c7756..6223e6fe2c 100644 --- a/examples/hydraMove.js +++ b/examples/hydraMove.js @@ -1,8 +1,8 @@ // -// hydraThrustAndView.js +// hydraMove.js // hifi // -// Created by Brad Hefta-Gaub on 2/6/14. +// Created by Brad Hefta-Gaub on 2/10/14. // Copyright (c) 2014 HighFidelity, Inc. All rights reserved. // // This is an example script that demonstrates use of the Controller and MyAvatar classes to implement @@ -27,8 +27,21 @@ var grabbingWithRightHand = false; var wasGrabbingWithRightHand = false; var grabbingWithLeftHand = false; var wasGrabbingWithLeftHand = false; -var EPSILON = 0.000001; //smallish positive number - used as margin of error for some computations +var EPSILON = 0.000001; var velocity = { x: 0, y: 0, z: 0}; +var deltaTime = 1/60; // approximately our FPS - maybe better to be elapsed time since last call +var THRUST_MAG_UP = 800.0; +var THRUST_MAG_DOWN = 300.0; +var THRUST_MAG_FWD = 500.0; +var THRUST_MAG_BACK = 300.0; +var THRUST_MAG_LATERAL = 250.0; +var THRUST_JUMP = 120.0; + +var YAW_MAG = 500.0; +var PITCH_MAG = 100.0; +var THRUST_MAG_HAND_JETS = THRUST_MAG_FWD; +var JOYSTICK_YAW_MAG = YAW_MAG; +var JOYSTICK_PITCH_MAG = PITCH_MAG * 0.5; var LEFT_PALM = 0; @@ -36,6 +49,7 @@ var LEFT_BUTTON_4 = 4; var RIGHT_PALM = 2; var RIGHT_BUTTON_4 = 10; +// Used by handleGrabBehavior() for managing the grab position changes function getAndResetGrabDelta() { var HAND_GRAB_SCALE_DISTANCE = 2.0; var delta = Vec3.multiply(grabDelta, (MyAvatar.scale * HAND_GRAB_SCALE_DISTANCE)); @@ -45,8 +59,9 @@ function getAndResetGrabDelta() { return result; } +// Used by handleGrabBehavior() for managing the grab velocity feature function getAndResetGrabDeltaVelocity() { - var HAND_GRAB_SCALE_VELOCITY = 5.0; + var HAND_GRAB_SCALE_VELOCITY = 50.0; var delta = Vec3.multiply(grabDeltaVelocity, (MyAvatar.scale * HAND_GRAB_SCALE_VELOCITY)); grabDeltaVelocity = { x: 0, y: 0, z: 0}; var avatarRotation = MyAvatar.orientation; @@ -54,28 +69,96 @@ function getAndResetGrabDeltaVelocity() { return result; } +// Used by handleGrabBehavior() for managing the grab rotation feature function getAndResetGrabRotation() { var quatDiff = Quat.multiply(grabCurrentRotation, Quat.inverse(grabStartRotation)); grabStartRotation = grabCurrentRotation; return quatDiff; } +// handles all the grab related behavior: position (crawl), velocity (flick), and rotate (twist) +function handleGrabBehavior() { + // check for and handle grab behaviors + grabbingWithRightHand = Controller.isButtonPressed(RIGHT_BUTTON_4); + grabbingWithLeftHand = Controller.isButtonPressed(LEFT_BUTTON_4); + stoppedGrabbingWithLeftHand = false; + stoppedGrabbingWithRightHand = false; + + if (grabbingWithRightHand && !wasGrabbingWithRightHand) { + // Just starting grab, capture starting rotation + grabStartRotation = Controller.getSpatialControlRawRotation(RIGHT_PALM); + } + if (grabbingWithRightHand) { + grabDelta = Vec3.sum(grabDelta, Vec3.multiply(Controller.getSpatialControlVelocity(RIGHT_PALM), deltaTime)); + grabCurrentRotation = Controller.getSpatialControlRawRotation(RIGHT_PALM); + } + if (!grabbingWithRightHand && wasGrabbingWithRightHand) { + // Just ending grab, capture velocity + grabDeltaVelocity = Controller.getSpatialControlVelocity(RIGHT_PALM); + stoppedGrabbingWithRightHand = true; + } + + if (grabbingWithLeftHand && !wasGrabbingWithLeftHand) { + // Just starting grab, capture starting rotation + grabStartRotation = Controller.getSpatialControlRawRotation(LEFT_PALM); + } + + if (grabbingWithLeftHand) { + grabDelta = Vec3.sum(grabDelta, Vec3.multiply(Controller.getSpatialControlVelocity(LEFT_PALM), deltaTime)); + grabCurrentRotation = Controller.getSpatialControlRawRotation(LEFT_PALM); + } + if (!grabbingWithLeftHand && wasGrabbingWithLeftHand) { + // Just ending grab, capture velocity + grabDeltaVelocity = Controller.getSpatialControlVelocity(LEFT_PALM); + stoppedGrabbingWithLeftHand = true; + } + + grabbing = grabbingWithRightHand || grabbingWithLeftHand; + stoppedGrabbing = stoppedGrabbingWithRightHand || stoppedGrabbingWithLeftHand; + + if (grabbing) { + + // move position + var moveFromGrab = getAndResetGrabDelta(); + if (Vec3.length(moveFromGrab) > EPSILON) { + MyAvatar.position = Vec3.sum(MyAvatar.position, moveFromGrab); + velocity = { x: 0, y: 0, z: 0}; + } + + // add some rotation... + var deltaRotation = getAndResetGrabRotation(); + var GRAB_CONTROLLER_TURN_SCALING = 0.5; + var euler = Vec3.multiply(Quat.safeEulerAngles(deltaRotation), GRAB_CONTROLLER_TURN_SCALING); + + // Adjust body yaw by yaw from controller + var orientation = Quat.multiply(Quat.angleAxis(-euler.y, {x:0, y: 1, z:0}), MyAvatar.orientation); + MyAvatar.orientation = orientation; + + // Adjust head pitch from controller + MyAvatar.headPitch = MyAvatar.headPitch - euler.x; + } + + //print("velocity=" +velocity.x +"," +velocity.y +"," +velocity.z + " length=" + Vec3.length(velocity)); + + // add some velocity... + if (stoppedGrabbing) { + velocity = Vec3.sum(velocity, getAndResetGrabDeltaVelocity()); + } + + // handle residual velocity + if(Vec3.length(velocity) > EPSILON) { + MyAvatar.position = Vec3.sum(MyAvatar.position, Vec3.multiply(velocity, deltaTime)); + // damp velocity + velocity = Vec3.multiply(velocity, damping); + } + + + wasGrabbingWithRightHand = grabbingWithRightHand; + wasGrabbingWithLeftHand = grabbingWithLeftHand; +} + +// Main update function that handles flying and grabbing behaviort function flyWithHydra() { - var deltaTime = 1/60; // approximately our FPS - maybe better to be elapsed time since last call - var THRUST_MAG_UP = 800.0; - var THRUST_MAG_DOWN = 300.0; - var THRUST_MAG_FWD = 500.0; - var THRUST_MAG_BACK = 300.0; - var THRUST_MAG_LATERAL = 250.0; - var THRUST_JUMP = 120.0; - var scale = 1.0; - - var YAW_MAG = 500.0; - var PITCH_MAG = 100.0; - var THRUST_MAG_HAND_JETS = THRUST_MAG_FWD; - var JOYSTICK_YAW_MAG = YAW_MAG; - var JOYSTICK_PITCH_MAG = PITCH_MAG * 0.5; - var thrustJoystickPosition = Controller.getJoystickPosition(THRUST_CONTROLLER); if (thrustJoystickPosition.x != 0 || thrustJoystickPosition.y != 0) { @@ -88,9 +171,11 @@ function flyWithHydra() { var right = Quat.getRight(currentOrientation); var up = Quat.getUp(currentOrientation); - var thrustFront = Vec3.multiply(front, scale * THRUST_MAG_HAND_JETS * thrustJoystickPosition.y * thrustMultiplier * deltaTime); + var thrustFront = Vec3.multiply(front, MyAvatar.scale * THRUST_MAG_HAND_JETS * + thrustJoystickPosition.y * thrustMultiplier * deltaTime); MyAvatar.addThrust(thrustFront); - var thrustRight = Vec3.multiply(right, scale * THRUST_MAG_HAND_JETS * thrustJoystickPosition.x * thrustMultiplier * deltaTime); + var thrustRight = Vec3.multiply(right, MyAvatar.scale * THRUST_MAG_HAND_JETS * + thrustJoystickPosition.x * thrustMultiplier * deltaTime); MyAvatar.addThrust(thrustRight); } else { thrustMultiplier = INITIAL_THRUST_MULTPLIER; @@ -110,56 +195,9 @@ function flyWithHydra() { var newPitch = MyAvatar.headPitch + (viewJoystickPosition.y * JOYSTICK_PITCH_MAG * deltaTime); MyAvatar.headPitch = newPitch; } - - // check for and handle grab behaviors - grabbingWithRightHand = Controller.isButtonPressed(RIGHT_BUTTON_4); - grabbingWithLeftHand = Controller.isButtonPressed(LEFT_BUTTON_4); - - if (grabbingWithRightHand) { - grabDelta = Vec3.sum(grabDelta, Vec3.multiply(Controller.getSpatialControlVelocity(RIGHT_PALM), deltaTime)); - grabCurrentRotation = Controller.getSpatialControlRawRotation(RIGHT_PALM); - } - if (!grabbingWithRightHand && wasGrabbingWithRightHand) { - // Just ending grab, capture velocity - grabDeltaVelocity = Controller.getSpatialControlVelocity(RIGHT_PALM); - } - if (grabbingWithRightHand && !wasGrabbingWithRightHand) { - // Just starting grab, capture starting rotation - grabStartRotation = Controller.getSpatialControlRawRotation(RIGHT_PALM); - } - - grabbing = grabbingWithRightHand || grabbingWithLeftHand; - - if (grabbing) { - - // move position - var moveFromGrab = getAndResetGrabDelta(); - if (Vec3.length(moveFromGrab) > EPSILON) { - position = Vec3.sum(position, moveFromGrab); - velocity = { x: 0, y: 0, z: 0}; - } - - // add some velocity... - velocity = Vec3.sum(velocity, getAndResetGrabDeltaVelocity()); - - // add some rotation... - var deltaRotation = getAndResetGrabRotation(); - var GRAB_CONTROLLER_TURN_SCALING = 0.5; - var euler = Vec3.multiply(Quat.safeEulerAngles(deltaRotation), GRAB_CONTROLLER_TURN_SCALING); - - // Adjust body yaw by yaw from controller - var orientation = Quat.multiply(Quat.angleAxis(-euler.y, {x:0, y: 1, z:0}), MyAvatar.orientation); - MyAvatar.orientation = orientation; - - // Adjust head pitch from controller - //getHead().setPitch(getHead().getPitch() - euler.x); - MyAvatar.headPitch = MyAvatar.headPitch - euler.x; - } - - wasGrabbingWithRightHand = grabbingWithRightHand; - wasGrabbingWithLeftHand = grabbingWithLeftHand; + handleGrabBehavior(); } - + Script.willSendVisualDataCallback.connect(flyWithHydra); Controller.captureJoystick(THRUST_CONTROLLER); Controller.captureJoystick(VIEW_CONTROLLER); diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 46d92c0f2e..08604a95f1 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -67,6 +67,7 @@ class AvatarData : public NodeData { Q_OBJECT Q_PROPERTY(glm::vec3 position READ getPosition WRITE setPosition) + Q_PROPERTY(float scale READ getTargetScale WRITE setTargetScale) Q_PROPERTY(glm::vec3 handPosition READ getHandPosition WRITE setHandPosition) Q_PROPERTY(float bodyYaw READ getBodyYaw WRITE setBodyYaw) Q_PROPERTY(float bodyPitch READ getBodyPitch WRITE setBodyPitch)