From 43aac813daab023eeb5003ed3d0db072f104cf7d Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 22 Oct 2015 11:46:55 -0700 Subject: [PATCH] more prep for shifting avatar during HMD motion --- interface/src/avatar/MyAvatar.cpp | 17 +++++++++++------ interface/src/avatar/MyAvatar.h | 5 ++--- interface/src/avatar/MyCharacterController.cpp | 13 ++++++++++++- interface/src/avatar/MyCharacterController.h | 3 ++- libraries/physics/src/CharacterController.h | 1 - 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index e52e56cd89..cfcd7a69f9 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -114,7 +114,6 @@ MyAvatar::MyAvatar(RigPointer rig) : ,_hmdAtRestDetector(glm::vec3(0), glm::quat()) #else ,_avatarOffsetFromHMD(0.0f) - ,_hmdVelocity(0.0f) #endif // OLD_HMD_TRACKER { for (int i = 0; i < MAX_DRIVE_KEYS; i++) { @@ -375,9 +374,16 @@ void MyAvatar::updateFromHMDSensorMatrix(const glm::mat4& hmdSensorMatrix) { } followHMD(deltaTime); +#else + // TODO adebug BOOKMARK -- this is where we need to add the new code for HMD_TRACKER #endif // OLD_HMD_TRACKER } +glm::vec3 MyAvatar::getHMDCorrectionVelocity() const { + // TODO: impelement this + return Vectors::ZERO; +} + #ifdef OLD_HMD_TRACKER void MyAvatar::beginFollowingHMD() { // begin homing toward derived body position. @@ -433,10 +439,6 @@ void MyAvatar::followHMD(float deltaTime) { _bodySensorMatrix = createMatFromQuatAndPos(rot, pos); } } -} -#else -void MyAvatar::harvestHMDOffset(glm::vec3 offset) { - } #endif // USE_OLD @@ -1292,7 +1294,7 @@ void MyAvatar::prepareForPhysicsSimulation() { relayDriveKeysToCharacterController(); _characterController.setTargetVelocity(getTargetVelocity()); _characterController.setAvatarPositionAndOrientation(getPosition(), getOrientation()); - //_characterController.setHMDVelocity(hmdVelocity); + _characterController.setHMDVelocity(getHMDCorrectionVelocity()); } void MyAvatar::harvestResultsFromPhysicsSimulation() { @@ -1301,6 +1303,9 @@ void MyAvatar::harvestResultsFromPhysicsSimulation() { _characterController.getAvatarPositionAndOrientation(position, orientation); nextAttitude(position, orientation); setVelocity(_characterController.getLinearVelocity()); + + // adebug TODO: harvest HMD shift here + //glm::vec3 hmdShift = _characterController.getHMDShift(); } QString MyAvatar::getScriptedMotorFrame() const { diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 2f2b8ca3b5..36c73d672d 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -76,6 +76,8 @@ public: // as it moves through the world. void updateFromHMDSensorMatrix(const glm::mat4& hmdSensorMatrix); + glm::vec3 getHMDCorrectionVelocity() const; + // best called at end of main loop, just before rendering. // update sensor to world matrix from current body position and hmd sensor. // This is so the correct camera can be used for rendering. @@ -272,8 +274,6 @@ private: void beginFollowingHMD(); bool shouldFollowHMD() const; void followHMD(float deltaTime); -#else - void harvestHMDOffset(glm::vec3 offset); #endif bool cameraInsideHead() const; @@ -376,7 +376,6 @@ private: bool _lastIsMoving { false }; #else glm::vec3 _avatarOffsetFromHMD; - glm::vec3 _hmdVelocity; #endif // OLD_HMD_TRACKER }; diff --git a/interface/src/avatar/MyCharacterController.cpp b/interface/src/avatar/MyCharacterController.cpp index 3e9755c6ca..ad2ca32b05 100644 --- a/interface/src/avatar/MyCharacterController.cpp +++ b/interface/src/avatar/MyCharacterController.cpp @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -161,6 +160,17 @@ void MyCharacterController::playerStep(btCollisionWorld* dynaWorld, btScalar dt) _rigidBody->setLinearVelocity(actualVelocity + tau * velocityCorrection); } } + + // Rather than add _hmdVelocity to the velocity of the RigidBody, we explicitly teleport + // the RigidBody forward according to the formula: distance = rate * time + if (_hmdVelocity.length2() > 0.0f) { + btTransform bodyTransform = _rigidBody->getWorldTransform(); + bodyTransform.setOrigin(bodyTransform.getOrigin() + dt * _hmdVelocity); + _rigidBody->setWorldTransform(bodyTransform); + } + // MyAvatar will ask us how far we stepped for HMD motion, which will depend on how + // much time has accumulated in _lastStepDuration. + _lastStepDuration += dt; } void MyCharacterController::jump() { @@ -390,6 +400,7 @@ void MyCharacterController::preSimulation() { } } } + _lastStepDuration = 0.0f; } void MyCharacterController::postSimulation() { diff --git a/interface/src/avatar/MyCharacterController.h b/interface/src/avatar/MyCharacterController.h index 5ee9f50f7e..de711c84f4 100644 --- a/interface/src/avatar/MyCharacterController.h +++ b/interface/src/avatar/MyCharacterController.h @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -48,7 +49,6 @@ public: // overrides from CharacterController virtual void preSimulation() override; virtual void postSimulation() override; - virtual void incrementSimulationTime(btScalar timeStep) override { _lastStepDuration += timeStep; } bool isHovering() const { return _isHovering; } void setHovering(bool enabled); @@ -65,6 +65,7 @@ public: void setTargetVelocity(const glm::vec3& velocity); void setHMDVelocity(const glm::vec3& velocity); + glm::vec3 getHMDShift() const { return _lastStepDuration * bulletToGLM(_hmdVelocity); } glm::vec3 getLinearVelocity() const; diff --git a/libraries/physics/src/CharacterController.h b/libraries/physics/src/CharacterController.h index cff257a790..e9e6f1328e 100644 --- a/libraries/physics/src/CharacterController.h +++ b/libraries/physics/src/CharacterController.h @@ -36,7 +36,6 @@ public: virtual void updateShapeIfNecessary() = 0; virtual void preSimulation() = 0; - virtual void incrementSimulationTime(btScalar stepTime) = 0; virtual void postSimulation() = 0; virtual void setWalkDirection(const btVector3 &walkDirection) { assert(false); }