Animation backwards, ElapsedTime returns strange value

I am playing an animation forward and backward. To determine the current position of the animation, I use ElapsedTimeInCycle, as I need to move the sprite based on the current frame.

I wrote, because I don’t know how to get the current frame

ElapseTime := TCastleScene(Parent).CurrentAnimation.ElapsedTimeInCycle;

if ElapseTime < 1.3 then
begin
TCastleScene(Parent).Translation := FPosicionActual + Vector3(0, -15, 0);
end
else if (ElapseTime > 1.3) and (ElapseTime < 1.8) then
begin
TCastleScene(Parent).Translation := FPosicionActual + Vector3(0, -5, 0);
end
else
begin
TCastleScene(Parent).Translation := FPosicionActual;
end;

This works when the animation runs forwards, but when goes backwards, the first value is 2.60, and the next value is 0.1, 0.2.., 2.30 as I can see in the log.

Is this a bug?, For workaround I wrote

if ElapseTime > 2.30 then
begin
Exit;
end;

Is there any way to know which frame is running?.

Thanks

/BlueIcaro

The way playing animation backward works (e.g. when you set TPlayAnimationParameters.Forward=false) is that most of the animation parameters (including ElapsedTimeInCycle) still move “forward”, but instead of “fraction in animation” (in 0..1 range) we send “1.0 - fraction in animation” to the interpolator nodes.

Basically, to know “frame in animation” and account for this, you need to do sthg like

AnimationNode := TCastleScene(Parent).CurrentAnimation;
if AnimationNode = nil then
  Exit; // not playing any animation now
if AnimationNode.FractionIncreasing then // FractionIncreasing corresponds to TPlayAnimationParameters.Forward
  Time := AnimationNode.ElapsedTimeInCycle
else
  Time := AnimationNode.CycleInterval - AnimationNode.ElapsedTimeInCycle;
...
// and then your code can follow, treating Time to indicate point in animation

This can be confirmed e.g. by our Castle Model Viewer, it shows CurrentAnimation.ElapsedTimeInCycle when it plays ( castle-model-viewer/code/v3dscenenamedanimations.pas at 229794ff9527f3a2fb32c6e4e6afc28314395c9c · castle-engine/castle-model-viewer · GitHub ).

I’m not sure does this explain everything you observe – you observe weird values :slight_smile: Please let me know if this doesn’t explain all issues, and send the testcase (program code + data) to reproduce the problem, I’ll see can I help better then :slight_smile:

1 Like

P.S. I improved our docs to document how animation playing backward and ElapsedTimeInCycle interact: Document how ElapsedTimeInCycle interacts with playing animation back… · castle-engine/castle-engine@6caed57 · GitHub .

1 Like