Page 1 of 1

Please help with an animation logic problem

Posted: Mon Jun 05, 2017 9:57 am
by garcia1000
Hello, I have attached a file showing my problem with an animation. It is basically several image files, with a body, a head, two arms, and a wand.

I created some parent/child relationship. So the arms and head are children of the body. The wand is child of an arm.

Then, I created two functions, one function is "move", which moves the image. This one is no problem. The other function is "rotate", which rotates the image about a pivot point. This is a problem at the moment.

When you run my file, you can press "s" to rotate the body, which moves all its attached children along with it. No problem so far. But then, try pressing "f" to rotate the head. The problem here is that the head's rotation is always the same, but it should be relative to the body.

Could you please take a look at the code and see what is wrong with the coding logic? Here is brief description of all the files:

animations.lua: the main code for the animation.
main.lua: very short, this should be self-explanatory.
middleclass.lua: this is from kikito
pic.lua: very short class for animations, mainly it just draws the images along the center of the image.
sandbox.lua: the controls for moving the animations.
tween.lua: this is from kikito too.

Re: Please help with an animation logic problem

Posted: Fri Jun 09, 2017 9:08 pm
by Semicolon
Heya, interesting problem! I'm not sure if you're still looking for help with this, and I'm afraid I'm not advanced enough to find my way through the .love you provided (the libraries especially are beyond my comprehension) but I thought I'd try to help.

I've put together a simple .love with a rudimentary parent/child "Drawable object" system. Maybe it can serve as inspiration, at least?
parentChildAnimation.love
(20.01 KiB) Downloaded 161 times

The important bits are in Drawable.update and Drawable.draw. The gist of it is that every child object has a "pivot" and an "offset".
Pivot is the point around which the object rotates. (Relative to its center. In a 128x128 image, the top left corner would be x = -64, y = -64)
Offset is the point at which the pivot resides. (Relative to the pivot point of its parent)

Controls:
Q and E keys change the angle of the active object
WASD moves the offset of the active object (Has no effect on "body", since it isn't the child of anything. If you wanted to move it, you'd change its x/y position instead)
Right/Left arrow keys change which object is active
F12 toggles drawing the pivot points

Let me know if the code makes sense! I only added comments in a couple parts, but I tried to keep everything reasonably simple.

Re: Please help with an animation logic problem

Posted: Tue Jun 13, 2017 9:51 pm
by JoshGrams
I'm guessing you figured this out already, but the problem is that you're using a pivot point that is in world coordinates, not one that takes into account all of its ancestors' rotations.

Re: Please help with an animation logic problem

Posted: Mon Jun 26, 2017 8:34 am
by garcia1000
Thanks for these replies Semicolon and JoshGrams. We solved it already and yes the problem was that the pivot point was anchored to the world coordinates and didn't take into account parent rotations.