• Editor
  • Box2D integration (cocos2d-x)

Related Discussions
...

Hi!

I'm currently working on spine integration and want to use Box2D physics for skeletons.
I read this https://github.com/EsotericSoftware/spine-workshop/blob/master/src/com/esotericsoftware/spine/workshop/I_Box2D.java code and try to implement such behavior in my C++ project.

For now I have the following result:

You can see, box2d bodies are positioning relatively to the Bones point, but I want to place the bodies where the Attachments placed.

My code in function "draw":

        // for every slot in skeleton
        spine::Slot *currentSlot = m_skeleton->skeleton->slots[i];
        if (currentSlot->attachment->type != spine::ATTACHMENT_REGION)
            continue;

    float x = currentSlot->bone->worldX / PTM_RATIO;
    float y = currentSlot->bone->worldY / PTM_RATIO;

    // Just 0 for now (test code)
    float rot = 0;//DegToRad(currentSlot->bone->worldRotation);

    // m_skeletonParts is box2d bodies, one for each "spine" slot.
    m_skeletonParts[i]->SetTransform(b2Vec2(x, y), rot);

How can I get the positions of the attachments?
Thank you!

Region attachments have an SRT that is their offset relative to the bone. This doesn't change, so apply it to the polygon shape:
https://github.com/EsotericSoftware/spi ... D.java#L92

Then you transform the polygon shape by the bone SRT every frame:
https://github.com/EsotericSoftware/spi ... .java#L130

You can also get the region attachment vertices. For cocos2d-x get the vertices from ((Cocos2dxRegionAttachment*)attachment)->quad.

Thanks! It works.

One more question. How fast is this framework? On my Acer A500 it gives me about 20-25 FPS when I start playing animation... (without any physics handling, as I did above)

The processing for skeletons and animations isn't much. Hard to say why you have such low framerate, there are too many variables with how you might have implemented it.

9 jours plus tard

Hey this thread really helped me implementing the same functionality.

One thing that got me stuck, I am using _attachment->super.scaleX to set the scale of my box2d bodies (after casting this attachment to a Cocos2dxRegionAttachment)

This works if the scale of the animation has been set via the overloaded constructor in CCSkeletonNode which takes scale, but not after I have set the scale via _skeletonNode->setScale(0.5f); right after the constructor.

After some breakpoints it looks like _attachment->super.width and _attachment.super.height are only changed for the scale when it is initialized via the constructor but not via CCSkeletonNode->setScale but the _attachment->quad IS updated.

Let me know if I am using this wrong.

skeletonNode->setScale is a cocos2d-x method for scaling a scene graph node. The scale in the CCSkeleton ctor is for setting the scale on SkeletonJson, the JSON loader. It scales the skeleton, animations, and attachments. This allows you to design in the Spine editor at one unit scale, and use a different unit scale at runtime.

The attachment width, scaleX and other fields do not change at runtime. These are the attachment offset SRT relative to the bone, and the attachment size. The attachment is first positioned, rotated, and scaled based on the attachment offset. Then the bone's world SRT is applied to these points. Note that if the attachment is not aligned to the bone coordinate system that the attachment quad can be skewed from a rectangle to a rhomboid if non-uniform scaling is used. If you are using the box2d body's SRT, don't use non-uniform scaling in your setup pose or animations.

TLDR; use the attachment offset SRT to setup your initial shape. Use the bone SRT for the box2d body.

Thanks for the reply!

I think what was confusing me was that setting the scale via the CCSkeleton ctor was having the same effect as CCSkeleton::setScale() when looking at the spine debug draw but then the draw is based of the attachment quads which I'm not using.

Thanks for clearing up what each are used for, will be useful as I try to create a generic structure for this.

We are doing universal scaling all round so happy days 😃