Skip to content

Commit

Permalink
use transition only when it has been selected
Browse files Browse the repository at this point in the history
fix for randomized transitions.
When transitions are selected at random we only have to consume the triggers on the transition that has been selected of out the random candidates.
If we consume the first one, the remaining ones are no longer candidates if they share the same trigger.

Diffs=
6d8854ec04 use transition only when it has been selected (#8921)

Co-authored-by: hernan <hernan@rive.app>
  • Loading branch information
bodymovin and bodymovin committed Jan 26, 2025
1 parent 201833a commit 3ae65f7
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9a43fe0b417d009ade6057b326e4feb44edb8096
6d8854ec047020794d0d4babbcad8343d216256a
4 changes: 4 additions & 0 deletions include/rive/animation/state_transition.hpp
Original file line number Diff line number Diff line change
@@ -134,6 +134,10 @@ class StateTransition : public StateTransitionBase
/// correct time to the animation instance in the stateFrom, when
/// applicable (when it's an AnimationState).
bool applyExitCondition(StateInstance* stateFrom) const;

/// Marks any trigger based condition as used for this layer
void useLayerInConditions(StateMachineInstance* stateMachineInstance,
StateMachineLayerInstance* layerInstance) const;
};
} // namespace rive

8 changes: 8 additions & 0 deletions src/animation/state_machine_instance.cpp
Original file line number Diff line number Diff line change
@@ -238,6 +238,10 @@ class StateMachineLayerInstance
}
}
}
else
{
transition->evaluatedRandomWeight(0);
}
}
if (totalWeight > 0)
{
@@ -251,6 +255,8 @@ class StateMachineLayerInstance
auto transitionWeight = transition->evaluatedRandomWeight();
if (currentWeight + transitionWeight > randomWeight)
{
transition->useLayerInConditions(m_stateMachineInstance,
this);
return transition;
}
currentWeight += transitionWeight;
@@ -284,6 +290,8 @@ class StateMachineLayerInstance
{
transition->evaluatedRandomWeight(
transition->randomWeight());
transition->useLayerInConditions(m_stateMachineInstance,
this);
return transition;
}
else
24 changes: 15 additions & 9 deletions src/animation/state_transition.cpp
Original file line number Diff line number Diff line change
@@ -202,15 +202,6 @@ AllowTransition StateTransition::allowed(
}
}
}
for (auto condition : m_Conditions)
{
if (condition->is<TransitionTriggerCondition>())
{
condition->as<TransitionTriggerCondition>()->useInLayer(
stateMachineInstance,
layerInstance);
}
}
return AllowTransition::yes;
}

@@ -227,4 +218,19 @@ bool StateTransition::applyExitCondition(StateInstance* from) const
return true;
}
return useExitTime;
}

void StateTransition::useLayerInConditions(
StateMachineInstance* stateMachineInstance,
StateMachineLayerInstance* layerInstance) const
{
for (auto condition : m_Conditions)
{
if (condition->is<TransitionTriggerCondition>())
{
condition->as<TransitionTriggerCondition>()->useInLayer(
stateMachineInstance,
layerInstance);
}
}
}

0 comments on commit 3ae65f7

Please sign in to comment.