From 97bcc543605eb343d7d71c47545a3c05a14dcd9c Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Mon, 8 Feb 2016 16:01:36 -0800 Subject: [PATCH] CharacterController: separate target velocity from parent velocity. Also, disable damping on the rigidBody used by the CharacterController. --- examples/moving-platform.js | 41 +++++++++++++++++++ interface/src/avatar/MyAvatar.cpp | 3 +- .../src/avatar/MyCharacterController.cpp | 1 + libraries/physics/src/CharacterController.cpp | 15 ++++--- libraries/physics/src/CharacterController.h | 4 +- 5 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 examples/moving-platform.js diff --git a/examples/moving-platform.js b/examples/moving-platform.js new file mode 100644 index 0000000000..94e45bb571 --- /dev/null +++ b/examples/moving-platform.js @@ -0,0 +1,41 @@ +var platform; + +function init() { + platform = Entities.addEntity({ + name: "platform", + type: "Box", + position: { x: 0, y: 0, z: 0 }, + dimensions: { x: 10, y: 2, z: 10 }, + color: { red: 0, green: 0, blue: 255 }, + gravity: { x: 0, y: 0, z: 0 }, + visible: true, + locked: false, + lifetime: 6000, + velocity: { x: 1.0, y: 0, z: 0 }, + damping: 0, + isDynamic: false + }); + if (platform) { + MyAvatar.position = { x: 0, y: 3, z: 0 }; + if (MyAvatar.getParentID() != platform) { + MyAvatar.setParentID(platform); + } + } +} + +function update(dt) { + +} + +function shutdown() { + if (platform) { + MyAvatar.setParentID(0); + Entities.deleteEntity(platform); + } +} + + +init(); + +Script.update.connect(update); +Script.scriptEnding.connect(shutdown); diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index c7b415ba09..1436c192b5 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -1089,7 +1089,8 @@ void MyAvatar::prepareForPhysicsSimulation() { } } - _characterController.setTargetVelocity(getTargetVelocity() + parentVelocity); + _characterController.setParentVelocity(parentVelocity); + _characterController.setTargetVelocity(getTargetVelocity()); _characterController.setPositionAndOrientation(getPosition(), getOrientation()); if (qApp->isHMDMode()) { _follow.prePhysicsUpdate(*this, deriveBodyFromHMDSensor(), _bodySensorMatrix); diff --git a/interface/src/avatar/MyCharacterController.cpp b/interface/src/avatar/MyCharacterController.cpp index 07156e9294..ee77859337 100644 --- a/interface/src/avatar/MyCharacterController.cpp +++ b/interface/src/avatar/MyCharacterController.cpp @@ -67,6 +67,7 @@ void MyCharacterController::updateShapeIfNecessary() { _rigidBody->setAngularFactor(0.0f); _rigidBody->setWorldTransform(btTransform(glmToBullet(_avatar->getOrientation()), glmToBullet(_avatar->getPosition()))); + _rigidBody->setDamping(0.0f, 0.0f); if (_state == State::Hover) { _rigidBody->setGravity(btVector3(0.0f, 0.0f, 0.0f)); } else { diff --git a/libraries/physics/src/CharacterController.cpp b/libraries/physics/src/CharacterController.cpp index 1f61bb9a59..9dd40a35a7 100644 --- a/libraries/physics/src/CharacterController.cpp +++ b/libraries/physics/src/CharacterController.cpp @@ -54,7 +54,7 @@ CharacterController::CharacterController() { _floorDistance = MAX_FALL_HEIGHT; - _walkVelocity.setValue(0.0f, 0.0f, 0.0f); + _targetVelocity.setValue(0.0f, 0.0f, 0.0f); _followDesiredBodyTransform.setIdentity(); _followTimeRemaining = 0.0f; _jumpSpeed = JUMP_SPEED; @@ -166,12 +166,12 @@ void CharacterController::playerStep(btCollisionWorld* dynaWorld, btScalar dt) { const btScalar MIN_SPEED = 0.001f; - btVector3 actualVelocity = _rigidBody->getLinearVelocity(); + btVector3 actualVelocity = _rigidBody->getLinearVelocity() - _parentVelocity; if (actualVelocity.length() < MIN_SPEED) { actualVelocity = btVector3(0.0f, 0.0f, 0.0f); } - btVector3 desiredVelocity = _walkVelocity; + btVector3 desiredVelocity = _targetVelocity; if (desiredVelocity.length() < MIN_SPEED) { desiredVelocity = btVector3(0.0f, 0.0f, 0.0f); } @@ -212,7 +212,7 @@ void CharacterController::playerStep(btCollisionWorld* dynaWorld, btScalar dt) { break; } - _rigidBody->setLinearVelocity(finalVelocity); + _rigidBody->setLinearVelocity(finalVelocity + _parentVelocity); // Dynamicaly compute a follow velocity to move this body toward the _followDesiredBodyTransform. // Rather then add this velocity to velocity the RigidBody, we explicitly teleport the RigidBody towards its goal. @@ -383,8 +383,11 @@ void CharacterController::getPositionAndOrientation(glm::vec3& position, glm::qu } void CharacterController::setTargetVelocity(const glm::vec3& velocity) { - //_walkVelocity = glmToBullet(_avatarData->getTargetVelocity()); - _walkVelocity = glmToBullet(velocity); + _targetVelocity = glmToBullet(velocity); +} + +void CharacterController::setParentVelocity(const glm::vec3& velocity) { + _parentVelocity = glmToBullet(velocity); } void CharacterController::setFollowParameters(const glm::mat4& desiredWorldBodyMatrix, float timeRemaining) { diff --git a/libraries/physics/src/CharacterController.h b/libraries/physics/src/CharacterController.h index 86ef350812..5362ca52e4 100644 --- a/libraries/physics/src/CharacterController.h +++ b/libraries/physics/src/CharacterController.h @@ -69,6 +69,7 @@ public: void getPositionAndOrientation(glm::vec3& position, glm::quat& rotation) const; void setTargetVelocity(const glm::vec3& velocity); + void setParentVelocity(const glm::vec3& parentVelocity); void setFollowParameters(const glm::mat4& desiredWorldMatrix, float timeRemaining); float getFollowTime() const { return _followTime; } glm::vec3 getFollowLinearDisplacement() const; @@ -105,7 +106,8 @@ protected: protected: btVector3 _currentUp; - btVector3 _walkVelocity; + btVector3 _targetVelocity; + btVector3 _parentVelocity; btTransform _followDesiredBodyTransform; btScalar _followTimeRemaining; btTransform _characterBodyTransform;