Search Unity

Problem with rotation artifacts when creating animation clip procedurally

Discussion in 'Animation' started by Caspilar, Nov 20, 2013.

  1. Caspilar

    Caspilar

    Joined:
    Jul 19, 2013
    Posts:
    17
    Hey guys, so here's the scenario: I'm attempting to create a plugin that will retrieve the information of 2d skeletal bone animation from a json file and transform it to Unity .anim files.

    I've successfully read the file and created the animation but some of the tangents presents a really odd behaviour, check the following picture:

    (Image of the Animation window with the procedurally generated AnimationClip)
    $problem.png

    The strange thing is that when I change the value of any of the keys or tangent, the curve sorts of fit nicely. If I put the original value back again the curve continues OK. See image below:

    (Image of the Animation window with the procedurally generated AnimationClip after a key value has been changed by the slightest ammount)
    $solved.png

    Therefore, I am able to manually fix things and get the animation playing like it should but I wanted to fix this via scripting if possible.

    I've already tried to use AnimationCurve.SmoothTangents and had no success. I'm not sure if I'm missing a step here... Has anyone ever bumped into this problem? Do you guys think this is a bug?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    Looks like a problem in conversion between euler angles and quaternions. Are you working entirely with quaternions? If so, are you ensuring they stay normalized throughout an interpolated movement? If not, are your animations done entirely with euler angles? How do you avoid gimbal lock problems?
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    The split curve is a "modulo 360º math" problem. The data is giving coordinates that are exceeding 360º, and thus wrapping around to lower numbers, so you get the discontinuous curve. When you adjust any key, the program gets to re-apply its own bezier curves instead of the original data, so you get something that stays inside the 0º-360º range.

    $r0h530q.png

    For example, if a cube is slowly rotating more than one full circuit around local Y, then you "fix" the curve as shown, the rotation would no longer turn all the way around but would kind of wobble around loosely instead.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    Sorry for spamming-- it won't let me edit my posts for some reason tonight.

    Was just going to say: this is one more reason that euler angles are evil.
     
  5. Caspilar

    Caspilar

    Joined:
    Jul 19, 2013
    Posts:
    17
    @halley, thanks for the fast answer!

    I hate euler angles just like anyone, the problem is that the file that I use to extract the data presents the rotation as Euler Angles.
    So I read them as a regular Vector3 and then I convert it to Quaternion, using Quaternion.Euler.

    The quaternion resulting from the euler angle convertion is the one I use to set the keys of the 4 animation curves (x,y,w,z). I had to convert the euler angles to quaternion because as far as I know, the AnimationClip.SetCurve does not accept "localEulerAngles" as a parameter for the "propertyName" field, it only accepts "localRotation".

    I'm trusting entirely in Unity to get deal with them. But I'm not too worried about that at the moment because this is a 2D plugin. The rotations will only occur on the Z axis, reducing, perhaps even erasing, the chance of gimbal locks.

    This might actually be happening, because as rotations are hierarchy related, the rotations might be going over 360 or under 0 and I'm not even noticing it. I'll double check if that's happening and will post back here. Thanks, again!
     
  6. Caspilar

    Caspilar

    Joined:
    Jul 19, 2013
    Posts:
    17
    @halley. I ensured that the angles I was setting on the curves were between 0 and 360, it solved some problems but created others.

    I've got to give props to code auto completion on this one. I've discovered a function that is not documented on the scripting manual/documentation

    The function is AnimationClip.EnsureQuaternionContinuity() and it solved the stated problem perfectly with just a single line of code. I'm not sure why it's not documented anywhere, though... It's not marked as deprecated or anything like that...

    Problem is solved!