don't blend avatar position when far from target

This commit is contained in:
Andrew Meadows 2018-05-14 09:58:16 -07:00
parent 6de175e794
commit a30e06d594
2 changed files with 7 additions and 11 deletions

View file

@ -59,7 +59,7 @@ const btCollisionShape* AvatarMotionState::computeNewShape() {
std::static_pointer_cast<Avatar>(_avatar)->computeShapeInfo(shapeInfo);
glm::vec3 halfExtents = shapeInfo.getHalfExtents();
halfExtents.y = 0.0f;
_radius2 = glm::length2(halfExtents);
_diameter = 2.0f * glm::length(halfExtents);
return getShapeManager()->getShape(shapeInfo);
}
@ -80,25 +80,21 @@ void AvatarMotionState::getWorldTransform(btTransform& worldTrans) const {
// virtual
void AvatarMotionState::setWorldTransform(const btTransform& worldTrans) {
// HACK: The PhysicsEngine does not actually move OTHER avatars -- instead it slaves their local RigidBody to the transform
// as specified by a remote simulation. However, to give the remote simulation time to respond to our own objects we tie
// the other avatar's body to its true position with a simple spring. This is a HACK that will have to be improved later.
const float SPRING_TIMESCALE = 0.5f;
float tau = PHYSICS_ENGINE_FIXED_SUBSTEP / SPRING_TIMESCALE;
btVector3 currentPosition = worldTrans.getOrigin();
btVector3 offsetToTarget = glmToBullet(getObjectPosition()) - currentPosition;
float distance2 = offsetToTarget.length2();
const float TWO_SQUARED = 4.0f;
if (distance2 > TWO_SQUARED * _radius2) {
// reduce the offsetToTarget by slamming the position
float distance = offsetToTarget.length();
if ((1.0f - tau) * distance > _diameter) {
// the avatar body is far from its target --> slam position
btTransform newTransform;
newTransform.setOrigin(currentPosition + tau * offsetToTarget);
newTransform.setOrigin(currentPosition + offsetToTarget);
newTransform.setRotation(glmToBullet(getObjectRotation()));
_body->setWorldTransform(newTransform);
_body->setLinearVelocity(glmToBullet(getObjectLinearVelocity()));
_body->setAngularVelocity(glmToBullet(getObjectAngularVelocity()));
} else {
// reduce the offsetToTarget by slamming the velocity
// the avatar body is near its target --> slam velocity
btVector3 velocity = glmToBullet(getObjectLinearVelocity()) + (1.0f / SPRING_TIMESCALE) * offsetToTarget;
_body->setLinearVelocity(velocity);
_body->setAngularVelocity(glmToBullet(getObjectAngularVelocity()));

View file

@ -81,7 +81,7 @@ protected:
const btCollisionShape* computeNewShape() override;
AvatarSharedPointer _avatar;
float _radius2 { 0.0f };
float _diameter { 0.0f };
uint32_t _dirtyFlags;
};