• Runtimes
  • Set custom transformation to bones by code

Related Discussions
...

Hello! We are new to this forum and we intend to use Spine for our next C++ project. We will actually be using Cocos2D-x for the rendering but that is probably not important for the following query:

We would like to be able to set custom transformation to specific (or all) bones by code for specific frames. For example:

  1. We have a normal Spine-animated character.

  2. At some point (where the character may be in a normal key frame or an interpolated/blended frame), we want to start setting transformations to bones that are code-generated, not pre-scripted (for example transformations resulting by physics calculations)

  3. After some frames that are customly generated, we want to start using Spine again the "normal" way, and interpolate back to a standard key frame

Is this easily doable? That is how I think this would work (pseudocode cause I am not quite familiar with the API, yet):

// normal character animation frame setting
character->setSpineFrame("animName", frameNum);
...
// after some frames, custom character transformation kicks-in
for each (bone in character->getBones())
{
    bone->setLocalFromWorldTransform( boneWorldTransform );
}
...
// after some frames again, we revert back to "normal" Spine animation; not abruptly, but with Spine's usual key-frame blending
// i.e, although the previous frame was custom generated, the next normal Spine frame will blend-in smoothly over the next x-frames
character->setSpineFrame("animName", frameNum);

You should be able to do this in cocos.

Fact 1: Spine rendering works by transforming images and meshes attached to the bones using the bone's internal transformation matrix.
Fact 2: Animation works by setting local bone transform values (scale, rotation, skew, translation) in Animation.apply, and then walking down the bone hierarchy to update each bone matrix Skeleton.updateWorldtransform.

So you have the option to set and lerp local transform values between animation-applied and calculated-applied numbers, then letting it update the world values/matrices.
Or setting and lerping the matrices directly.

FWIW: Spine-Unity has a module that allows users to do this. (2D physics ragdoll, or control bone positions via "GameObject" transforms)