Inverting animation in the Alternativa3D

Hi! If you wish to reverse (play from the end to the beginning) some of your animations in the Alternativa3D engine, you could try to set the AnimationClip‘s property speed to the negative value, but it could be harmful in most cases and don’t officially supported.

Unfortunately, there is no any information or suggests about inverting animations like so on the public yet and nothing about it in the official documentation. I think, current engine build (7.7) have no lightweight native implementation of this functionality. It could be nice to see something like boolean reverse property at the AnimationClip class, isn’t it? Bot there is nothing similar yet =(

So, with great tip from the Vladimir Babushkin (about rebuilding animation by keyframes with reversed time), I came to this piece of code:

var sourceAnimations:Vector.<AnimationClip> = collada.animations;
if (sourceAnimations)
{
	var len:uint = sourceAnimations.length;
	
	var _forwardAnimationClips:Vector.<AnimationClip>;
	var _backwardAnimationClips:Vector.<AnimationClip>;
	var _forwardAnimationControllers:Vector.<AnimationController>;
	var _backwardAnimationControllers:Vector.<AnimationController>;
	
	_forwardAnimationClips = sourceAnimations.concat();
	_backwardAnimationClips = new Vector.<AnimationClip>(len, true);
	_forwardAnimationControllers = new Vector.<AnimationController>(len, true);
	_backwardAnimationControllers = new Vector.<AnimationController>(len, true);
	
	var j:uint = 0;
	
	for (j; j < len; j++ )
	{
		var forwardAnimation:AnimationClip = _forwardAnimationClips[j];
		var backwardAnimation:AnimationClip = new AnimationClip();
		
		var forwardController:AnimationController = new AnimationController();
		var backwardController:AnimationController = new AnimationController();
		
		var tracks:uint = forwardAnimation.numTracks;
		var k:uint = 0;
		
		for (k; k < tracks; k++ )
		{
			var forwardTrack:TransformTrack = TransformTrack(forwardAnimation.getTrackAt(k));
			var backwardTrack:TransformTrack = new TransformTrack(forwardTrack.object);
			
			var keys:uint = forwardTrack.keys.length;
			var m:uint = 0;
			
			for (m; m < keys; m++)
			{
				var key:Keyframe = forwardTrack.keys[m];
				backwardTrack.addKey(forwardTrack.length - key.time, Matrix3D(key.value));
			}
			
			backwardAnimation.addTrack(backwardTrack);
		}
		
		backwardAnimation.objects = forwardAnimation.objects;
		backwardAnimation.updateLength();
		
		forwardController.root = forwardAnimation;
		backwardController.root = backwardAnimation;
		
		_backwardAnimationClips[j] = backwardAnimation;
		
		_forwardAnimationControllers[j] = forwardController;
		_backwardAnimationControllers[j] = backwardController;
	}
}

So, the _backwardAnimationControllers Vector contains your reversed animation and could be used to play it like this:

private function onEnterFrame(e:Event = null):void
{
	if (_currentAnimationControllers.length > 0)
	{
		var len:uint = _currentAnimationControllers.length;
		var i:uint = 0;
				
		for (i; i < len; i++ )
		{
			_currentAnimationControllers[i].update();
		}
		camera.render();
	}
}

This code is not optimized and not cleaned (and pulled from the current project draft I working on), just to show the idea.
If you have more accurate and nice code to deal with animations reversing in the Alternativa3D, please, feel free to share it here!

Found a typo? Please, highlight it and press Shift + Enter or click here to inform me!

Share Button

Comments

Inverting animation in the Alternativa3D — 4 Comments

  1. Yes, this is ugly “solution”, if solution at all, when you need to tween both backwards and forwards, possibly with random speed and start/end times. Instead, I opted for hacking into core classes to add required functionality there: http://pastie.org/2526831 + http://pastie.org/2526834

    This hack allows me to write something like:
    for (var i:int = 0; i < animations.length; i++) {
    animations [i].time = mouseX; // was: .update ();
    }

    • Thanks for reply, it was fast and really horrible solution =) You did a great workaround! But hacking core classes is not less ugly though 🙁 Alternativa guys should provide a good native options to handle such things within a standard public APIs I think.
      Anyway, it’s good to see this issue is noticed by people, hack-style options are options too!)

    • BTW, this solution was used for building a new reversed animation, not for tweening it or like so. For the standard playing with keyframes and triggered events only.

      • sure, it’s enough for stuff like opening/closing doors. but I have to seek into animation based on mouse movement. hacking may be no less ugly indeed, but I see zero other options.