From 74eacf23460a3cef407863327bbe9fd7ac6b3d43 Mon Sep 17 00:00:00 2001 From: luiscuenca Date: Wed, 7 Aug 2019 15:08:44 -0700 Subject: [PATCH 1/4] Make sure random switch does not repeat previous state --- libraries/animation/src/AnimNodeLoader.cpp | 3 -- libraries/animation/src/AnimRandomSwitch.cpp | 34 ++++++++++++++------ libraries/animation/src/AnimRandomSwitch.h | 3 +- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/libraries/animation/src/AnimNodeLoader.cpp b/libraries/animation/src/AnimNodeLoader.cpp index 6828ed1c7e..d43351fff9 100644 --- a/libraries/animation/src/AnimNodeLoader.cpp +++ b/libraries/animation/src/AnimNodeLoader.cpp @@ -943,9 +943,6 @@ bool processRandomSwitchStateMachineNode(AnimNode::Pointer node, const QJsonObje } auto randomStatePtr = std::make_shared(id, iter->second, interpTarget, interpDuration, interpTypeEnum, easingTypeEnum, priority, resume); - if (priority > 0.0f) { - smNode->addToPrioritySum(priority); - } assert(randomStatePtr); if (!interpTargetVar.isEmpty()) { diff --git a/libraries/animation/src/AnimRandomSwitch.cpp b/libraries/animation/src/AnimRandomSwitch.cpp index edc8c8dd96..92d6edb36f 100644 --- a/libraries/animation/src/AnimRandomSwitch.cpp +++ b/libraries/animation/src/AnimRandomSwitch.cpp @@ -27,22 +27,34 @@ const AnimPoseVec& AnimRandomSwitch::evaluate(const AnimVariantMap& animVars, co AnimRandomSwitch::RandomSwitchState::Pointer desiredState = _currentState; if (abs(_randomSwitchEvaluationCount - context.getEvaluationCount()) > 1 || animVars.lookup(_triggerRandomSwitchVar, false)) { - // get a random number and decide which motion to choose. + // filter states different to the last random state and with priorities. bool currentStateHasPriority = false; - float dice = randFloatInRange(0.0f, 1.0f); - float lowerBound = 0.0f; - for (const RandomSwitchState::Pointer& randState : _randomStates) { + std::vector randomStatesToConsider; + float totalPriorities = 0.0f; + for (int i = 0; i < _randomStates.size(); i++) { + auto randState = _randomStates[i]; if (randState->getPriority() > 0.0f) { - float upperBound = lowerBound + (randState->getPriority() / _totalPriorities); - if ((dice > lowerBound) && (dice < upperBound)) { - desiredState = randState; + bool isRepeatingClip = _children[randState->getChildIndex()]->getID() == _lastPlayedState; + if (!isRepeatingClip) { + randomStatesToConsider.push_back(randState); + totalPriorities += randState->getPriority(); } - lowerBound = upperBound; - // this indicates if the curent state is one that can be selected randomly, or is one that was transitioned to by the random duration timer. currentStateHasPriority = currentStateHasPriority || (_currentState == randState); } } + // get a random number and decide which motion to choose. + float dice = randFloatInRange(0.0f, 1.0f); + float lowerBound = 0.0f; + for (int i = 0; i < randomStatesToConsider.size(); i++) { + auto randState = randomStatesToConsider[i]; + float upperBound = lowerBound + (randState->getPriority() / totalPriorities); + if ((dice > lowerBound) && (dice < upperBound)) { + desiredState = randState; + break; + } + lowerBound = upperBound; + } if (abs(_randomSwitchEvaluationCount - context.getEvaluationCount()) > 1) { _duringInterp = false; switchRandomState(animVars, context, desiredState, _duringInterp); @@ -155,8 +167,10 @@ void AnimRandomSwitch::addState(RandomSwitchState::Pointer randomState) { } void AnimRandomSwitch::switchRandomState(const AnimVariantMap& animVars, const AnimContext& context, RandomSwitchState::Pointer desiredState, bool shouldInterp) { - auto nextStateNode = _children[desiredState->getChildIndex()]; + if (nextStateNode->getType() == AnimNodeType::Clip) { + _lastPlayedState = nextStateNode->getID(); + } if (shouldInterp) { const float FRAMES_PER_SECOND = 30.0f; diff --git a/libraries/animation/src/AnimRandomSwitch.h b/libraries/animation/src/AnimRandomSwitch.h index 7c185dd7cb..888ed1f6b5 100644 --- a/libraries/animation/src/AnimRandomSwitch.h +++ b/libraries/animation/src/AnimRandomSwitch.h @@ -143,7 +143,6 @@ protected: void setTransitionVar(const QString& transitionVar) { _transitionVar = transitionVar; } void setTriggerTimeMin(float triggerTimeMin) { _triggerTimeMin = triggerTimeMin; } void setTriggerTimeMax(float triggerTimeMax) { _triggerTimeMax = triggerTimeMax; } - void addToPrioritySum(float priority) { _totalPriorities += priority; } void addState(RandomSwitchState::Pointer randomState); @@ -164,7 +163,6 @@ protected: float _alpha = 0.0f; AnimPoseVec _prevPoses; AnimPoseVec _nextPoses; - float _totalPriorities { 0.0f }; RandomSwitchState::Pointer _currentState; RandomSwitchState::Pointer _previousState; @@ -179,6 +177,7 @@ protected: float _randomSwitchTimeMin { 10.0f }; float _randomSwitchTimeMax { 20.0f }; float _randomSwitchTime { 0.0f }; + QString _lastPlayedState; private: // no copies From 473af995d6d371941d4d564696aebe5fb71f1e80 Mon Sep 17 00:00:00 2001 From: luiscuenca Date: Wed, 7 Aug 2019 16:53:08 -0700 Subject: [PATCH 2/4] Fix warnings --- libraries/animation/src/AnimRandomSwitch.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/animation/src/AnimRandomSwitch.cpp b/libraries/animation/src/AnimRandomSwitch.cpp index 92d6edb36f..45e6ae0050 100644 --- a/libraries/animation/src/AnimRandomSwitch.cpp +++ b/libraries/animation/src/AnimRandomSwitch.cpp @@ -31,7 +31,7 @@ const AnimPoseVec& AnimRandomSwitch::evaluate(const AnimVariantMap& animVars, co bool currentStateHasPriority = false; std::vector randomStatesToConsider; float totalPriorities = 0.0f; - for (int i = 0; i < _randomStates.size(); i++) { + for (size_t i = 0; i < _randomStates.size(); i++) { auto randState = _randomStates[i]; if (randState->getPriority() > 0.0f) { bool isRepeatingClip = _children[randState->getChildIndex()]->getID() == _lastPlayedState; @@ -46,7 +46,7 @@ const AnimPoseVec& AnimRandomSwitch::evaluate(const AnimVariantMap& animVars, co // get a random number and decide which motion to choose. float dice = randFloatInRange(0.0f, 1.0f); float lowerBound = 0.0f; - for (int i = 0; i < randomStatesToConsider.size(); i++) { + for (size_t i = 0; i < randomStatesToConsider.size(); i++) { auto randState = randomStatesToConsider[i]; float upperBound = lowerBound + (randState->getPriority() / totalPriorities); if ((dice > lowerBound) && (dice < upperBound)) { From 3a22db0a13bcb6f22249960d8d4dba09eb722ccb Mon Sep 17 00:00:00 2001 From: luiscuenca Date: Thu, 8 Aug 2019 06:25:04 -0700 Subject: [PATCH 3/4] Last state for all node types --- libraries/animation/src/AnimRandomSwitch.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libraries/animation/src/AnimRandomSwitch.cpp b/libraries/animation/src/AnimRandomSwitch.cpp index 45e6ae0050..8b99568f07 100644 --- a/libraries/animation/src/AnimRandomSwitch.cpp +++ b/libraries/animation/src/AnimRandomSwitch.cpp @@ -168,9 +168,7 @@ void AnimRandomSwitch::addState(RandomSwitchState::Pointer randomState) { void AnimRandomSwitch::switchRandomState(const AnimVariantMap& animVars, const AnimContext& context, RandomSwitchState::Pointer desiredState, bool shouldInterp) { auto nextStateNode = _children[desiredState->getChildIndex()]; - if (nextStateNode->getType() == AnimNodeType::Clip) { - _lastPlayedState = nextStateNode->getID(); - } + _lastPlayedState = nextStateNode->getID(); if (shouldInterp) { const float FRAMES_PER_SECOND = 30.0f; From 597cff335adfcc00a161f5f8db26647d033beaaa Mon Sep 17 00:00:00 2001 From: luiscuenca Date: Thu, 8 Aug 2019 08:40:00 -0700 Subject: [PATCH 4/4] Reserve vector size --- libraries/animation/src/AnimRandomSwitch.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/animation/src/AnimRandomSwitch.cpp b/libraries/animation/src/AnimRandomSwitch.cpp index 8b99568f07..3cf402cc14 100644 --- a/libraries/animation/src/AnimRandomSwitch.cpp +++ b/libraries/animation/src/AnimRandomSwitch.cpp @@ -30,6 +30,7 @@ const AnimPoseVec& AnimRandomSwitch::evaluate(const AnimVariantMap& animVars, co // filter states different to the last random state and with priorities. bool currentStateHasPriority = false; std::vector randomStatesToConsider; + randomStatesToConsider.reserve(_randomStates.size()); float totalPriorities = 0.0f; for (size_t i = 0; i < _randomStates.size(); i++) { auto randState = _randomStates[i];