From a8b688d540a9b3cc4ea21008aaa73f1d7b4c81ed Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 4 Mar 2015 15:26:02 -0800 Subject: [PATCH] move _velocity into base class. walking on a cube works --- interface/src/avatar/Avatar.cpp | 1 - interface/src/avatar/Avatar.h | 3 --- interface/src/avatar/MyAvatar.cpp | 22 +++++++++++++--------- interface/src/avatar/MyAvatar.h | 4 +--- libraries/avatars/src/AvatarData.cpp | 3 ++- libraries/avatars/src/AvatarData.h | 4 ++++ libraries/physics/src/PhysicsEngine.cpp | 17 +++-------------- 7 files changed, 23 insertions(+), 31 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index cac071a677..3333c0ab92 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -63,7 +63,6 @@ Avatar::Avatar() : _skeletonModel(this), _skeletonOffset(0.0f), _bodyYawDelta(0.0f), - _velocity(0.0f), _positionDeltaAccumulator(0.0f), _lastVelocity(0.0f), _acceleration(0.0f), diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index 77c9459999..2951208d95 100644 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -155,7 +155,6 @@ public: Q_INVOKABLE glm::vec3 getNeckPosition() const; - Q_INVOKABLE glm::vec3 getVelocity() const { return _velocity; } Q_INVOKABLE glm::vec3 getAcceleration() const { return _acceleration; } Q_INVOKABLE glm::vec3 getAngularVelocity() const { return _angularVelocity; } Q_INVOKABLE glm::vec3 getAngularAcceleration() const { return _angularAcceleration; } @@ -184,8 +183,6 @@ protected: QVector _attachmentModels; float _bodyYawDelta; - glm::vec3 _velocity; - // These position histories and derivatives are in the world-frame. // The derivatives are the MEASURED results of all external and internal forces // and are therefore READ-ONLY --> motion control of the Avatar is NOT obtained diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 3068bd2046..e6f84330bc 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -181,8 +181,8 @@ void MyAvatar::simulate(float deltaTime) { { PerformanceTimer perfTimer("transform"); updateOrientation(deltaTime); - if (_enablePhysics) { - updatePhysicsPosition(deltaTime); + if (isPhysicsEnabled()) { + updatePositionWithPhysics(deltaTime); } else { updatePosition(deltaTime); } @@ -1396,21 +1396,25 @@ void MyAvatar::updatePosition(float deltaTime) { } -void MyAvatar::updatePhysicsPosition(float deltaTime) { - glm::vec3 velocity = _velocity; - - bool pushingUp = (_driveKeys[UP] - _driveKeys[DOWN] > 0.0f) || _scriptedMotorVelocity.y > 0.0f; +void MyAvatar::updatePositionWithPhysics(float deltaTime) { + // bool pushingUp = (_driveKeys[UP] - _driveKeys[DOWN] > 0.0f) || _scriptedMotorVelocity.y > 0.0f; + // rotate velocity into camera frame glm::quat rotation = getHead()->getCameraOrientation(); - glm::vec3 localVelocity = glm::inverse(rotation) * velocity; + glm::vec3 localVelocity = glm::inverse(rotation) * _velocity; bool hasFloor = false; glm::vec3 newLocalVelocity = applyKeyboardMotor(deltaTime, localVelocity, hasFloor); newLocalVelocity = applyScriptedMotor(deltaTime, newLocalVelocity); - // rotate back into world-frame - velocity = rotation * newLocalVelocity; + // cap avatar speed + float speed = glm::length(_velocity); + if (speed > MAX_AVATAR_SPEED) { + _velocity *= MAX_AVATAR_SPEED / speed; + } + // rotate back into world-frame + _velocity = rotation * newLocalVelocity; } diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index d74b7fc5ac..6c55125b5c 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -166,8 +166,6 @@ public slots: glm::vec3 getThrust() { return _thrust; }; void setThrust(glm::vec3 newThrust) { _thrust = newThrust; } - void setVelocity(const glm::vec3 velocity) { _velocity = velocity; } - void updateMotionBehavior(); void onToggleRagdoll(); @@ -235,7 +233,7 @@ private: glm::vec3 applyKeyboardMotor(float deltaTime, const glm::vec3& velocity, bool walkingOnFloor); glm::vec3 applyScriptedMotor(float deltaTime, const glm::vec3& velocity); void updatePosition(float deltaTime); - void updatePhysicsPosition(float deltaTime); + void updatePositionWithPhysics(float deltaTime); void updateCollisionWithAvatars(float deltaTime); void updateCollisionWithEnvironment(float deltaTime, float radius); void updateCollisionWithVoxels(float deltaTime, float radius); diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index a3d330f84b..5b391b33f8 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -55,7 +55,8 @@ AvatarData::AvatarData() : _billboard(), _errorLogExpiry(0), _owningAvatarMixer(), - _lastUpdateTimer() + _lastUpdateTimer(), + _velocity(0.0f) { } diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 857489eaa7..bc0305338c 100644 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -311,6 +311,8 @@ public: bool tryLockForWrite() { return _lock.tryLockForWrite(); } void unlock() { _lock.unlock(); } + void setVelocity(const glm::vec3 velocity) { _velocity = velocity; } + Q_INVOKABLE glm::vec3 getVelocity() const { return _velocity; } public slots: void sendAvatarDataPacket(); @@ -401,6 +403,8 @@ protected: virtual void updateJointMappings(); void changeReferential(Referential* ref); + glm::vec3 _velocity; + private: // privatize the copy constructor and assignment operator so they cannot be called AvatarData(const AvatarData&); diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 63e6901ec4..d7df649713 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -284,6 +284,7 @@ void PhysicsEngine::stepSimulation() { if (_avatarData->isPhysicsEnabled()) { _avatarGhostObject->setWorldTransform(btTransform(glmToBullet(_avatarData->getOrientation()), glmToBullet(_avatarData->getPosition()))); + _characterController->setWalkDirection(glmToBullet(_avatarData->getVelocity())); } const int MAX_NUM_SUBSTEPS = 4; @@ -364,7 +365,7 @@ void PhysicsEngine::computeCollisionEvents() { } - +#if 0 // avatar collisions btManifoldArray manifoldArray; btBroadphasePairArray& pairArray = _avatarGhostObject->getOverlappingPairCache()->getOverlappingPairArray(); @@ -399,7 +400,7 @@ void PhysicsEngine::computeCollisionEvents() { } } } - +#endif @@ -667,15 +668,3 @@ void PhysicsEngine::setAvatarData(AvatarData *avatarData) { btGhostPairCallback* ghostPairCallback = new btGhostPairCallback(); _dynamicsWorld->getPairCache()->setInternalGhostPairCallback(ghostPairCallback); } - - -// void PhysicsEngine::setAvatarMotion() { - /* - float dt = getDeltaTimeMicroseconds() * 0.000001f; - btVector3 walkDirection = btVector3(0.0, 0.0, 0.0); - btScalar walkVelocity = btScalar(1.1) * 4.0; // 4 km/h -> 1.1 m/s - btScalar walkSpeed = walkVelocity * dt; - - _characterController->setWalkDirection(walkDirection*walkSpeed); - */ -// }