From 8acb6d13d57efb1d685d9b402b26dcc09cc405f4 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 10 Feb 2014 15:25:09 -0800 Subject: [PATCH] move hydra thrust and view code into JS example --- examples/cameraExample.js | 7 +++ examples/hydraThrustAndView.js | 86 +++++++++++++++++++++++++++++++ interface/src/Application.cpp | 2 +- interface/src/avatar/Hand.cpp | 1 - interface/src/avatar/Hand.h | 6 --- interface/src/avatar/MyAvatar.cpp | 34 +----------- interface/src/avatar/MyAvatar.h | 10 ++-- 7 files changed, 100 insertions(+), 46 deletions(-) create mode 100644 examples/hydraThrustAndView.js diff --git a/examples/cameraExample.js b/examples/cameraExample.js index d42c3c1b0e..65cffb17dd 100644 --- a/examples/cameraExample.js +++ b/examples/cameraExample.js @@ -69,6 +69,13 @@ function checkCamera() { var viewJoystickPosition = Controller.getJoystickPosition(VIEW_CONTROLLER); yaw -= viewJoystickPosition.x * JOYSTICK_YAW_MAG * deltaTime; pitch += viewJoystickPosition.y * JOYSTICK_PITCH_MAG * deltaTime; + if (yaw > 360) { + yaw -= 360; + } + if (yaw < -360) { + yaw += 360; + } + print("pitch="+ pitch + " yaw="+ yaw + " roll=" + roll); var orientation = Quat.fromPitchYawRoll(pitch, yaw, roll); Camera.setOrientation(orientation); } diff --git a/examples/hydraThrustAndView.js b/examples/hydraThrustAndView.js new file mode 100644 index 0000000000..249aa5948c --- /dev/null +++ b/examples/hydraThrustAndView.js @@ -0,0 +1,86 @@ +// +// hydraThrustAndView.js +// hifi +// +// Created by Brad Hefta-Gaub on 2/6/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 +// avatar flying through the hydra/controller joysticks +// +// + +var damping = 0.9; +var position = { x: MyAvatar.position.x, y: MyAvatar.position.y, z: MyAvatar.position.z }; +var joysticksCaptured = false; +var THRUST_CONTROLLER = 0; +var VIEW_CONTROLLER = 1; +var INITIAL_THRUST_MULTPLIER = 1.0; +var THRUST_INCREASE_RATE = 1.05; +var MAX_THRUST_MULTIPLIER = 75.0; +var thrustMultiplier = INITIAL_THRUST_MULTPLIER; + +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) { + if (thrustMultiplier < MAX_THRUST_MULTIPLIER) { + thrustMultiplier *= 1 + (deltaTime * THRUST_INCREASE_RATE); + } + var currentOrientation = MyAvatar.orientation; + + var front = Quat.getFront(currentOrientation); + var right = Quat.getRight(currentOrientation); + var up = Quat.getUp(currentOrientation); + + var thrustFront = Vec3.multiply(front, 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); + MyAvatar.addThrust(thrustRight); + } else { + thrustMultiplier = INITIAL_THRUST_MULTPLIER; + } + + // View Controller + var viewJoystickPosition = Controller.getJoystickPosition(VIEW_CONTROLLER); + if (viewJoystickPosition.x != 0 || viewJoystickPosition.y != 0) { + + // change the body yaw based on our x controller + var orientation = MyAvatar.orientation; + var deltaOrientation = Quat.fromPitchYawRoll(0, (-1 * viewJoystickPosition.x * JOYSTICK_YAW_MAG * deltaTime), 0); + MyAvatar.orientation = Quat.multiply(orientation, deltaOrientation); + + // change the headPitch based on our x controller + //pitch += viewJoystickPosition.y * JOYSTICK_PITCH_MAG * deltaTime; + var newPitch = MyAvatar.headPitch + (viewJoystickPosition.y * JOYSTICK_PITCH_MAG * deltaTime); + MyAvatar.headPitch = newPitch; + } +} + +Script.willSendVisualDataCallback.connect(flyWithHydra); +Controller.captureJoystick(THRUST_CONTROLLER); +Controller.captureJoystick(VIEW_CONTROLLER); + +// Map keyPress and mouse move events to our callbacks +function scriptEnding() { + // re-enabled the standard application for touch events + Controller.releaseJoystick(THRUST_CONTROLLER); + Controller.releaseJoystick(VIEW_CONTROLLER); +} +Script.scriptEnding.connect(scriptEnding); + diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 160d6b7c2c..004b71074b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4042,7 +4042,7 @@ void Application::loadScript(const QString& fileNameString) { scriptEngine->getParticlesScriptingInterface()->setParticleTree(_particles.getTree()); // hook our avatar object into this script engine - scriptEngine->setAvatarData( static_cast(_myAvatar), "MyAvatar"); + scriptEngine->setAvatarData(_myAvatar, "MyAvatar"); // leave it as a MyAvatar class to expose thrust features CameraScriptableObject* cameraScriptable = new CameraScriptableObject(&_myCamera, &_viewFrustum); scriptEngine->registerGlobalObject("Camera", cameraScriptable); diff --git a/interface/src/avatar/Hand.cpp b/interface/src/avatar/Hand.cpp index b441000cc1..302475df8a 100644 --- a/interface/src/avatar/Hand.cpp +++ b/interface/src/avatar/Hand.cpp @@ -33,7 +33,6 @@ Hand::Hand(Avatar* owningAvatar) : _collisionCenter(0,0,0), _collisionAge(0), _collisionDuration(0), - _pitchUpdate(0), _grabDelta(0, 0, 0), _grabDeltaVelocity(0, 0, 0), _grabStartRotation(0, 0, 0, 1), diff --git a/interface/src/avatar/Hand.h b/interface/src/avatar/Hand.h index 3c8ec2d562..aec5cdb921 100755 --- a/interface/src/avatar/Hand.h +++ b/interface/src/avatar/Hand.h @@ -58,10 +58,6 @@ public: const glm::vec3& getLeapFingerTipBallPosition (int ball) const { return _leapFingerTipBalls [ball].position;} const glm::vec3& getLeapFingerRootBallPosition(int ball) const { return _leapFingerRootBalls[ball].position;} - // Pitch from controller input to view - const float getPitchUpdate() const { return _pitchUpdate; } - void setPitchUpdate(float pitchUpdate) { _pitchUpdate = pitchUpdate; } - // Get the drag distance to move glm::vec3 getAndResetGrabDelta(); glm::vec3 getAndResetGrabDeltaVelocity(); @@ -101,8 +97,6 @@ private: void handleVoxelCollision(PalmData* palm, const glm::vec3& fingerTipPosition, VoxelTreeElement* voxel, float deltaTime); - float _pitchUpdate; - glm::vec3 _grabDelta; glm::vec3 _grabDeltaVelocity; glm::quat _grabStartRotation; diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 98eb9a4431..4eec6e70f8 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -323,6 +323,7 @@ void MyAvatar::simulate(float deltaTime) { updateChatCircle(deltaTime); + // TODO: this should be removed and turned into JS instead // Get any position, velocity, or rotation update from Grab Drag controller glm::vec3 moveFromGrab = _hand.getAndResetGrabDelta(); if (glm::length(moveFromGrab) > EPSILON) { @@ -797,39 +798,6 @@ void MyAvatar::updateThrust(float deltaTime) { up; } } - // Add thrust and rotation from hand controllers - const float THRUST_MAG_HAND_JETS = THRUST_MAG_FWD; - const float JOYSTICK_YAW_MAG = YAW_MAG; - const float JOYSTICK_PITCH_MAG = PITCH_MAG * 0.5f; - const int THRUST_CONTROLLER = 0; - const int VIEW_CONTROLLER = 1; - for (size_t i = 0; i < getHand().getPalms().size(); ++i) { - PalmData& palm = getHand().getPalms()[i]; - - // If the script hasn't captured this joystick, then let the default behavior work - if (!Application::getInstance()->getControllerScriptingInterface()->isJoystickCaptured(palm.getSixenseID())) { - if (palm.isActive() && (palm.getSixenseID() == THRUST_CONTROLLER)) { - if (palm.getJoystickY() != 0.f) { - FingerData& finger = palm.getFingers()[0]; - if (finger.isActive()) { - } - _thrust += front * _scale * THRUST_MAG_HAND_JETS * palm.getJoystickY() * _thrustMultiplier * deltaTime; - } - if (palm.getJoystickX() != 0.f) { - _thrust += right * _scale * THRUST_MAG_HAND_JETS * palm.getJoystickX() * _thrustMultiplier * deltaTime; - } - } else if (palm.isActive() && (palm.getSixenseID() == VIEW_CONTROLLER)) { - if (palm.getJoystickX() != 0.f) { - _bodyYawDelta -= palm.getJoystickX() * JOYSTICK_YAW_MAG * deltaTime; - } - if (palm.getJoystickY() != 0.f) { - getHand().setPitchUpdate(getHand().getPitchUpdate() + - (palm.getJoystickY() * JOYSTICK_PITCH_MAG * deltaTime)); - } - } - } - - } // Update speed brake status const float MIN_SPEED_BRAKE_VELOCITY = _scale * 0.4f; diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 7dfb8812dd..4e381509b9 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -45,7 +45,6 @@ public: // setters void setMousePressed(bool mousePressed) { _mousePressed = mousePressed; } - void setThrust(glm::vec3 newThrust) { _thrust = newThrust; } void setVelocity(const glm::vec3 velocity) { _velocity = velocity; } void setLeanScale(float scale) { _leanScale = scale; } void setGravity(glm::vec3 gravity); @@ -78,9 +77,6 @@ public: static void sendKillAvatar(); - // Set/Get update the thrust that will move the avatar around - void addThrust(glm::vec3 newThrust) { _thrust += newThrust; }; - glm::vec3 getThrust() { return _thrust; }; void orbit(const glm::vec3& position, int deltaX, int deltaY); @@ -94,9 +90,13 @@ public slots: void increaseSize(); void decreaseSize(); void resetSize(); - void sendIdentityPacket(); + // Set/Get update the thrust that will move the avatar around + void addThrust(glm::vec3 newThrust) { _thrust += newThrust; }; + glm::vec3 getThrust() { return _thrust; }; + void setThrust(glm::vec3 newThrust) { _thrust = newThrust; } + private: bool _mousePressed; float _bodyPitchDelta;