move some stuff out of Avatar::simulate()

call it explicity in AvatarManager::updateOtherAvatars()
This commit is contained in:
Andrew Meadows 2017-01-23 16:12:47 -08:00
parent b8afc87ad9
commit 69a7b1ef76
4 changed files with 42 additions and 15 deletions

View file

@ -188,25 +188,35 @@ AABox Avatar::getBounds() const {
}
void Avatar::animateScaleChanges(float deltaTime) {
float currentScale = getUniformScale();
auto desiredScale = getDomainLimitedScale();
if (currentScale != desiredScale) {
if (_isAnimatingScale) {
float currentScale = getUniformScale();
float desiredScale = getDomainLimitedScale();
// use exponential decay toward the domain limit clamped scale
const float SCALE_ANIMATION_TIMESCALE = 0.5f;
float blendFactor = glm::clamp(deltaTime / SCALE_ANIMATION_TIMESCALE, 0.0f, 1.0f);
float animatedScale = (1.0f - blendFactor) * currentScale + blendFactor * desiredScale;
// snap to the end when we get close enough
const float MIN_RELATIVE_SCALE_ERROR = 0.03f;
if (fabsf(desiredScale - currentScale) / desiredScale < MIN_RELATIVE_SCALE_ERROR) {
const float MIN_RELATIVE_ERROR = 0.03f;
float relativeError = fabsf(desiredScale - currentScale) / desiredScale;
if (relativeError < MIN_RELATIVE_ERROR) {
animatedScale = desiredScale;
_isAnimatingScale = false;
}
setScale(glm::vec3(animatedScale)); // avatar scale is uniform
// TODO: rebuilding the shape constantly is somehwat expensive.
// We should only rebuild after significant change.
rebuildCollisionShape();
}
}
void Avatar::setTargetScale(float targetScale) {
AvatarData::setTargetScale(targetScale);
_isAnimatingScale = true;
}
void Avatar::updateAvatarEntities() {
PerformanceTimer perfTimer("attachments");
// - if queueEditEntityMessage sees clientOnly flag it does _myAvatar->updateAvatarEntity()
@ -311,12 +321,6 @@ bool Avatar::shouldDie() const {
void Avatar::simulate(float deltaTime, bool inView) {
PROFILE_RANGE(simulation, "simulate");
PerformanceTimer perfTimer("simulate");
if (!isDead() && !_motionState) {
DependencyManager::get<AvatarManager>()->addAvatarToSimulation(this);
}
animateScaleChanges(deltaTime);
{
PROFILE_RANGE(simulation, "updateJoints");
uint64_t start = usecTimestampNow();

View file

@ -181,6 +181,8 @@ public:
void setLastRenderUpdateTime(uint64_t time) { _lastRenderUpdateTime = time; }
bool shouldDie() const;
void animateScaleChanges(float deltaTime);
void setTargetScale(float targetScale) override;
public slots:
@ -231,8 +233,6 @@ protected:
// protected methods...
bool isLookingAtMe(AvatarSharedPointer avatar) const;
virtual void animateScaleChanges(float deltaTime);
glm::vec3 getBodyRightDirection() const { return getOrientation() * IDENTITY_RIGHT; }
glm::vec3 getBodyUpDirection() const { return getOrientation() * IDENTITY_UP; }
glm::vec3 getBodyFrontDirection() const { return getOrientation() * IDENTITY_FRONT; }
@ -269,6 +269,7 @@ private:
bool _initialized;
bool _isLookAtTarget { false };
bool _inScene { false };
bool _isAnimatingScale { false };
float getBoundingRadius() const;

View file

@ -175,6 +175,9 @@ void AvatarManager::updateOtherAvatars(float deltaTime) {
removeAvatar(avatar->getID());
continue;
}
if (avatar->isDead()) {
continue;
}
// priority = weighted linear combination of:
// (a) apparentSize
@ -205,7 +208,24 @@ void AvatarManager::updateOtherAvatars(float deltaTime) {
while (!sortedAvatars.empty()) {
const AvatarPriority& sortData = sortedAvatars.top();
const auto& avatar = std::static_pointer_cast<Avatar>(sortData.avatar);
// for ALL avatars...
avatar->ensureInScene(avatar);
if (!avatar->getMotionState()) {
ShapeInfo shapeInfo;
avatar->computeShapeInfo(shapeInfo);
btCollisionShape* shape = const_cast<btCollisionShape*>(ObjectMotionState::getShapeManager()->getShape(shapeInfo));
if (shape) {
// don't add to the simulation now, instead put it on a list to be added later
AvatarMotionState* motionState = new AvatarMotionState(avatar.get(), shape);
avatar->setMotionState(motionState);
_motionStatesToAddToPhysics.insert(motionState);
_motionStatesThatMightUpdate.insert(motionState);
}
}
avatar->animateScaleChanges(deltaTime);
// for avatars in view...
const bool inView = sortData.priority > 0.5f * OUT_OF_VIEW_PENALTY;
avatar->simulate(deltaTime, inView);
if (expiry > usecTimestampNow()) {
@ -429,6 +449,7 @@ void AvatarManager::handleCollisionEvents(const CollisionEvents& collisionEvents
}
}
/*
void AvatarManager::addAvatarToSimulation(Avatar* avatar) {
assert(!avatar->getMotionState());
@ -443,6 +464,7 @@ void AvatarManager::addAvatarToSimulation(Avatar* avatar) {
_motionStatesThatMightUpdate.insert(motionState);
}
}
*/
void AvatarManager::updateAvatarRenderStatus(bool shouldRenderAvatars) {
if (DependencyManager::get<SceneScriptingInterface>()->shouldRenderAvatars()) {

View file

@ -69,7 +69,7 @@ public:
void handleOutgoingChanges(const VectorOfMotionStates& motionStates);
void handleCollisionEvents(const CollisionEvents& collisionEvents);
void addAvatarToSimulation(Avatar* avatar);
//void addAvatarToSimulation(Avatar* avatar);
Q_INVOKABLE RayToAvatarIntersectionResult findRayIntersection(const PickRay& ray,
const QScriptValue& avatarIdsToInclude = QScriptValue(),