• Editor
  • BB position like in SPINE

Related Discussions
...

For the foot bb the yellow dot looks like the bone position, which is correct. How are you setting the position? Maybe you should create the PolygonShape with local coordinates, boundingBox.getVertices(), which is relative to the bone. Then you position the PolygonShape at the bone world position and set its rotation to the bone's world rotation using the bone's world position as the origin.

Seriously it's very strange and drive me nuts.
If spineboy has not set animation and body is set this way of computeWorldVertices:

.//get slot of bb
.
 testB = (BoundingBoxAttachment)slot.getAttachment();   
boxPoly2 = new PolygonShape(); float[] polygon = new float[testB.getVertices().length]; testB.computeWorldVertices(skeleton.getX(), skeleton.getY(), slot.getBone(), polygon); boxPoly2.set(polygon); BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.StaticBody; test2 = swiat.createBody(boxBodyDef); test2.createFixture(boxPoly2, 0); boxPoly2.dispose();

The body is perfectly set. This is done in show() metod See pic.

Image removed due to the lack of support for HTTPS. | Show Anyway

If I setTransform for the body in the render metod in the same pose it's look like this:

.//render metod 
.//get right slot 
if ( s.equals("bbnoga")){

	float x = skeleton.getX()  + slot.getBone().getWorldX()  ;
   	float y = skeleton.getY()   + slot.getBone().getWorldY();
  	float rotation = slot.getBone().getWorldRotation() ;
     	test1.setTransform(x, y, rotation * MathUtils.degRad);
}

Image removed due to the lack of support for HTTPS. | Show Anyway

Green is the body black is the bb of debugrenderer ๐Ÿ˜ข

There is even beter when I set the animation. What I found is if you setAnimation like this

 
               state = new AnimationState(stateData); 
               state.setAnimation(0, "walk", true);

            state.apply(skeleton); // Poses skeleton using current animations. 
            skeleton.updateWorldTransform();
           bounds = new SkeletonBounds(); 
           bounds.update(skeleton, true);

It's look like this:

Image removed due to the lack of support for HTTPS. | Show Anyway

If I move setAnimation at the end:

 state = new AnimationState(stateData); 
               state.apply(skeleton); // Poses skeleton using current animations. 
                skeleton.updateWorldTransform();
               bounds = new SkeletonBounds(); 
               bounds.update(skeleton, true);
               state.setAnimation(0, "walk", true);

It's look like this:

Image removed due to the lack of support for HTTPS. | Show Anyway

What the heck is going an. :bang:
Does I'm have only this problems?

The code makes sense:

state = new AnimationState(stateData); 
// set the animation to walk
state.setAnimation(0, "walk", true);
// pose skeleton with walk animation
state.apply(skeleton);
// update skeleton's world SRT, which will be for the walk animation pose
skeleton.updateWorldTransform();
// new bounds and update it with the skeleton's world SRT
bounds = new SkeletonBounds(); 
bounds.update(skeleton, true);
// new animation state with no animation
state = new AnimationState(stateData); 
// does nothing, the animation state has no animation
state.apply(skeleton);
// update skeleton's world SRT, which will be for the setup pose
skeleton.updateWorldTransform();
// new bounds and update it with the skeleton's world SRT
bounds = new SkeletonBounds(); 
bounds.update(skeleton, true);
// set animation to walk, won't affect the skeleton until state.apply(skeleton) later
state.setAnimation(0, "walk", true);

Now, back to making your BB show up in the right place. ๐Ÿ™‚

BoundingBoxAttachment bb = ...
float[] polygon = new float[bb.getVertices().length];
bb.computeWorldVertices(skeleton.getX(), skeleton.getY(), slot.getBone(), polygon);
PolygonShape shape = new PolygonShape();
shape.set(polygon);

This computes the world vertices for a BB and creates a shape. This is going to show up in the correct place for the current skeleton pose, which is probably the setup pose. However, it is probably not what you want. Let's say one of the BB world vertices is at 50,100. When you position the PolygonShape at 0,0 then the vertex is at 50,100. When you position the PolygonShape at 100,100 then the vertex is at 150,200. This makes it hard to position and rotate the shape to match the BB, which is relative to a bone.

BoundingBoxAttachment bb = ...
PolygonShape shape = new PolygonShape();
shape.set(bb.getVertices());

This creates a shape using the BB local vertices. I steered you away from this initial, my apologies. :$ Next you should use setTransform(boneWorldX, boneWorldY, boneWorldRotation). This will position the shape at the bone's world position. The shape's vertices that we specified when we create the shape are relative to this position, which is exactly what we want. setTransform also rotates the shape around the position, and since the position is the bone location, this is also exactly what we want.

Have you seen this? It creates bodies for region attachments, but the concept is the same.
https://github.com/EsotericSoftware/spi ... ample.java

I hope you get it working!

BoundingBoxAttachment bb = ...
PolygonShape shape = new PolygonShape();
shape.set(bb.getVertices());

This was my first approach see beginning of my post. Really strange, because now it's works like a charm and I didn't change anything. I must to revise code where is the trick THANKS NATE!! :whew:

Aye, not sure why it wasn't working before and sorry again for steering you wrong! Glad it's working! ๐Ÿ™‚

Nate wrote

Aye, not sure why it wasn't working before and sorry again for steering you wrong! Glad it's working! ๐Ÿ™‚

No problem I'm really glad there is someone to help even if it's took long time you not give up ๐Ÿ˜‰ Keep on going.

๐Ÿ˜‰

I've been following this thread because I'm having a similar problem in Cocos2D and Box2D where I'm setting up physics bodies based on bounding box attachments created in Spine. My goal is to be able to update the physics bodies to match the bone positions. I'm not going to use physics to control the bone positions at the moment.

What I'm noticing is that the world position for bones for each frame is not being updated for rotation where rotation is the angle of the skeleton. The end result is that polygons are rotating around their positions for the skeleton at zero degrees. This is fine for polygons positioned at (0, 0) but not correct for other positions.

I've tried the following in an attempt to get them to update without success.

spBone_updateWorldTransform(bone, 0, 0);

spBoundingBoxAttachment_computeWorldVertices(bounding_box, skeletonAnim.position.x, skeletonAnim.position.y, bone, bounding_box->vertices);

[skeletonAnimation updateWorldTransform];

Through some trial and error, I've come up with transforms similar to the following form that can get some polygons to follow rotation but only in a limited number of situations.

d = sqrtf(powf(bone->worldX, 2) + powf(bone->worldY, 2));

b2Position = b2Vec2(( skeletonAnim.position.x + d * cosf(CC_DEGREES_TO_RADIANS(skeletonAnim.rotation)) ) / PTM_RATIO , ( skeletonAnim.y + d * sinf(CC_DEGREES_TO_RADIANS(skeletonAnim.rotation)) ) / PTM_RATIO );

I'm aware of a general formula but I'm hoping there is an easier way based on what is provided by the runtime. If it is necessary to implement, I'm interested in any runtime specifics that might affect its implementation.

Polygons attached to the bone at (0, 0) work fine using:

b2Angle = skeletonAnim.rotation;

b2Position = b2Vec2( ( bone->worldX + skeletonAnim.position.x ) / PTM_RATIO , ( bone->worldY + skeletonAnim.position.y ) / PTM_RATIO );

b->SetTransform(b2Position, b2Angle + CC_DEGREES_TO_RADIANS(boneAng));

I'd like to have a solution for the general case of positioning all the polygons based on the bone's world position. Right now only zero rotation is working and I'd rather not develop formulas to handle each bone for other rotations.

Is there a way to get the bone's world position with rotation factored in for every frame using the Cocos2D runtime? Is that what bone->worldX and bone->worldY are intended to be? I may be confused in my understanding but I feel like I'm close to getting correct bounding box placement as all the angles are appearing correctly using the debug drawing. It's just with the positions where I'm missing something.

When you define your shape, you do it relative to the bone (use BB local vertices). Then you always position your shape at the bone world position and apply the bone's world rotation. If it doesn't work, show your code for creating the shape and your setTransform call.

Hi, Nate. Thanks for your reply.

I solved my problem! It was a matter of getting the bone's world coordinate transformed according to the rotation of the skeleton. The code that I posted was the positioning code I was using. The setTransform sets the origin, i.e. the bone, of the shape. I didn't have any issues in creating the shape from its vertices defined in Spine.

What I ended up doing is this:

CGPoint boneWorldPos = [skeletonAnim convertToWorldSpace:ccp(bone->worldX, bone->worldY)];
b2Vec2 b2Position = b2Vec2(boneWorldPos.x / PTM_RATIO, boneWorldPos.y / PTM_RATIO);
(b2Body *)b->SetTransform(b2Position, b2Angle + CC_DEGREES_TO_RADIANS(boneAng));

One of the reasons I was having so much trouble was due to my following the code in CCSkeleton.m where the bones somehow magically update themselves according the skeleton's rotation. In my code, the same calls to bone->worldX and bone->worldY are only good for my skeleton's setup angle. Is that how it is supposed to be? In any case, I'm happy to have something that works for me.

Ah I see. The skeleton is in a cocos2d node. All of the bone's world SRT are defined relative to that node. If you rotate the node, the bone world SRT is not affected. You need to take the node rotation into account, as you do. Note this is not an issue with the setup pose rotation, you are rotating the container for the skeleton, not the skeleton itself.

This is the same as if you have a parent cocos2d node and a child cocos2d node. When you rotate the parent, the child position is not affected because it is defined relative to the parent.

Thanks for the explanation, Nate. That makes sense that I wasn't getting matching coordinates since I was rotating the node and not the skeleton itself. It is clear in retrospect but quite elusive when I didn't know what was happening.