AnimationPlayer relative positions
Hello! I feel AnimationPlayer is missing a "relative position" feature. To be more clear: the actual location of the animation would be relative to the local position instead of the absolute world position.
For instance, it would allow to create platforms that move in a circular pattern without remaking the animation every time for each platform.
It surely can be obtained with Tween but is not as immediate and user friendly as it would be to have it in the AnimationPlayer directly.
Which would be the most elegant way to implement it?
@Alex-doc ~ That's too much complexity IMO, and can be done with just adding an additional parent Node2D...
That being said, it would be nice to be able to, say, have a move_up animation that would both animate a sprite and move it up from its current location. :wink:
I see, you are right. Adding a parent Node2D does exactly what I meant, so the implementation in the AnimationPlayer would be pointless.
Can you specify a bit on the move_up animation? Can't that also be achieved with an animated sprite with AnimationPlayer attached?
(apart the discussion about the move_up, I feel the issue can be closed)
@Alex-doc ~ I mean, if this feature request gets implemented, it would open a wide range of nice ways to get movement code out of _process/_fixed_process.
@bojidar-bg I think something like this move(vector2(50,50),5.0) 50,50 is offset and the second one is seconds
@mohaalak wouldn't that better belong to a Schedule module?
Something with two main functions, a delayed call and a call over time.
For instance:
Schedule.delay(function,time) so it can be done in a single for loop (scheduling set_pos(10*i,10*i))
Or better, with the other main function I have in mind which essentially does the same thing over time:
Schedule.task(function,time).
Each of the functions would get the current time but do opposite things:
delay would wait for the time to end and then call this function the other one same but would call it (each frame?) for time
Sorry for the off topic in the issue but I think is quite worthy as idea(and some kind related), if you are interested I'd open a new issue over it.
P.S. I'm not sure if Object::set() can actually call script functions, but the idea is something similar.
this one might actually better fit
@mohaalak @Alex-doc You can already make the so-called "function tracks" in the animation player... also, Tweens are just going to get better :wink:
I guess this issue should be left open though, since it is a nice feature too.
@Alex-doc look at tween example in demos
You are misunderstanding the way the locations in the nodes work. Every node is positioned relative to its parent. When you animate the position of the root node you are animating (and thereby overwriting) the position of the scene relative to its parent. I understand that it's confusing at first (I made the mistake in the beginning as well), but once you understand how it works it's obvious that the right way to solve this is by simply not animating the position of the root node but use a Node2D as a root and animate the other nodes relative to it.
@Warlaan, read the comments above, bojidar-bg already suggested the solution and the topic was then solved, we then discussed about some kind of scheduling function. Which is out of the scope of this issue. Closing it now, since I thought it was already...
Actually, I believe this would be a useful addition.
Having to add a parent node so that the animations become "relative" makes me have to change my scene hierarchy when I come back to add animations to polish the game, which causes necessary code changes.
Yes, I could use Tweens instead, but the strong point of the AnimationPlayer system is that you can preview the animation in the editor (instead of guessing the numbers that look good and running the game to see results when using Tweens).
It would also help with creation of re-usable animations. For example I just created a "shake" animation. It moves the root item left and right quickly (the only track is ".:transform/pos"). If a "relative" mode was implemented, I could use it immediately on any node with an AnimationPlayer. However because there is no relative mode, then I have to create a parent node of the original node instead so that it doesn't get it's position reset to 0,0
Without a relative mode, I think it is limiting the places where AnimationPlayer could be used.
@bojidar-bg also stated it should be left open
I guess this issue should be left open though, since it is a nice feature too.
Reusable animations sounds pretty good.. Reopened. The upcoming 3.0 excuses breakages that could be a nice occasion to discuss this feature.
Note that we have to discuss the following:
- Should the animation revert back to the initial state after playing by default, or should we leave people wonder what the initial state was (or worse, have them manually click the play backwards button).
- What should happen if we start moving the object while the animation is not yet complete (hey, that might be useful for making special RigidBody character actions!).
- What should happen if two AnimationPlayer nodes are playing a relative animation on the same property/object?
- How should one mark the animation as "relative"? :smiley:
-
I think the artist should be setting the last frame to match the initial state (which I think it should be on frame 0) so, no auto-reset (unless looping)
-
the animation's "ghost parent"(read as point from which relative animations will move) should move its location. If two are playing on the same object, I would be expecting the same behaviour we have with non-relative right now.
-
I'd put a flag in the animation's property.
Note: My opinions are expressed before actually going to read the current animation's code. Will likely change after I'll take a better look into it.
I think you are underestimating the importance of the first point. When you run an animation in the editor the scene gets changed. You have to manually reset the scene (or depending on how your animation is built reset it to frame 0) before you can save it again, otherwise you will save the wrong initial state for the scene. For example if you have an animation that turns an object invisible and you play it in the editor you need to make the object visible again before saving the scene or else your object will be invisible everytime it is instanced.
Also I don't understand what you mean that the artist should be setting the last frame to match the initial state. The first use case that came to my mind was doing a walk animation for a game where you can only walk from square to square (like in Pokemon or Sokoban). That animation would of course not end with the same state as in frame 0 because modifying the position would be the whole point. And I would certainly hope that if that animation was looped it did not reset its position at the end but continue from the new position.
This example shows how dangerous it is to try and build a magic solution for specific use cases because you can never know what use cases other people are going to come up with. So I would strongly suggest using a very simple mechanism that works in a predictable way and improving it from there on.
In this case I would suggest that a "relative" animation track simply performs one step whenever it passes frame 0: It takes the property it is meant to modify and adds the current value to every value in the track as an offset. That way a single relative animation would work just as requested while unexpected use cases (like two animations running at the same time) would work predictably the same way as current animations because they would be using the exact same system.
In order to allow more than one relative animation at a time that solution could be extended later so that after applying one frame the animationplayer notes which value it set the property to, and when the next frame is to be applied it adds the difference between the value it set and the value it finds (modified by physics, other animations or scripts) to the offset.
The biggest drawback would be that not for every property the correct operation to apply an offset would be an addition. For example if you were to animate a matrix you would have to multiply the offset rather than add it. Or if you were to modify a bool flag you would probably want the flag to be flipped (otherwise a relative animation would be the same as an absolute one).
In this case I would suggest that a "relative" animation track simply performs one step whenever it passes frame 0: It takes the property it is meant to modify and adds the current value to every value in the track as an offset.
Yes, this is how I imagined it would work. The current value at frame 0 of the property of the object that is being animated gets added to all key frame values in the animation track.
And yes, different property types might require different operations.
Example of how the adding object value to keyframe would work: (I guess you all get it already though)
If a relative animation had keyframes on pos time = 0 seconds, pos = (0, 0) time = 5 seconds, pos = (500, 0)
An object has pos currently (200, 100) gets the above relative animation played on it, then the keyframes become time = 0 seconds, pos = (0 + 200, 0 + 100) = (200, 100) time = 5 seconds, pos = (500 + 200, 0 + 100) = (700, 100)
And the animation is played as normal, resulting in the object being moved right 500 units in 5 seconds from its initial location.
Regarding point 1) the problem (as I understand it) is that when editing if the relative animation is being played in 'relative' mode, then just playing the object multiple times would keep moving the object around possibly further and further from beginning point.
The way to fix that is to have 'relative' mode be a flag that is used at runtime only. When editing, it would only be a flag to set that is saved, or maybe 'relative' is just an option in the play() method to call at runtime.
The downside of having 'relative' mode available only at runtime is that you wouldn't be able to edit 'relative' animations in abritary scenes where the object to be animated in the editor is not at (0, 0). When editing 'relative' animations you'd have to use a separate scene with the object to animate at (0, 0) (well it doesn't have to be at 0,0 - depends on the animation to create if you want it to jump at the beginning) ... but this would be worth it because the resulting animation would be re-usable and applicable to multiple scenes / objects in different starting positions later.
Well, otherwise editing relative animations would have to make the editor go into a special mode where it remembers the initial position and resets its as @bojidar-bg said. I think that could be confusing though as the object position in the inspector wouldn't match the values in the keyframes.
After reading @Alex-doc 's comment again:
the animation's "ghost parent"(read as point from which relative animations will move) should move its location.
I kind of get what you're saying. How to implement though I have no idea yet.
EDIT: Actually I think @Warlaan has the answer here:
In order to allow more than one relative animation at a time that solution could be extended later so that after applying one frame the animationplayer notes which value it set the property to, and when the next frame is to be applied it adds the difference between the value it set and the value it finds (modified by physics, other animations or scripts) to the offset.
This will correctly allow other movement code to work at the same time as relative animations
Another thought: Relativeness might even be set on a per-track basis. Ex. absolute rotation / scale / opacity, but relative position.
Going deeper: It could even be per key!
Ex. an animation to move from wherever you are to pos (200, 200)
- first key would be a relative key with position value = (0, 0)
- the other key would be an absolute key with value = (200, 200)
(you could even have it move back to start position with a third relative key = (0, 0) )
This would be immensely useful for a lot of things and is easily implemented using the same ideas above and just adding a flag per key.
EDIT: (crazy thought) ... then instead of a flag on each key of either absolute or relative, there would be a weight of absoluteness from 0 to 1. Values in between would be weighted. .... I'm not even sure if that is useful LOL (I'm imagining swirling coins dropped by monsters on the floor up into the gold amount display at the top of the screen .. absoluteness would gradually increase.), but being able to set either absolute or relative per key would definitely be useful as in example above.
EDIT2: With the addition of relative / absolute keys, then the Animation system would almost be able to replace entirely the Tweening system with the advantage of being able to preview the animations and ease of creation using the Editor. Tweening's advantage was/is that you could tween from current values to absolute values. With relative keys, the Animation system will also allow that and also allow creation of complex finely tuned reusable animations that if one were to try hand coding with Tween it would be too painful to even try. Add some tween-like methods to dynamically create animation tracks to the Animation system and then it could completely replace Tweening.