• Runtimes
  • [Cocos2d-iPhone] Question about rotation for sprite

How to set the rotation angle for sprite with a bone at current animation frame? I tried with the code but didn't work. Most of the sprites are all right, but some of them are not the same rotation angle with the bone.

spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
spAtlasRegion *region = (spAtlasRegion *)attachment->rendererObject;
CGRect rect = CGRectZero;
if(region->rotate) {
     rect = CGRectMake(region->x * 0.5, region->y * 0.5, region->height * 0.5, region->width * 0.5);
}
else {
     rect = CGRectMake(region->x * 0.5, region->y * 0.5, region->width * 0.5, region->height * 0.5);
}
CCSprite *sprite = [CCSprite spriteWithTexture:batchNode.texture rect:rect];
sprite.rotation = ((region->rotate ? 90 : 0) - attachment->rotation - slot->bone->worldRotation);

Image supprimée en raison de l'absence de support de HTTPS. | Afficher quand même

The right is the spine skeleton, which is right.
The left are the sprites I created from the skeleton, but the rotation angle of some bones are not right, such as the right arm, the right leg. I don't know why.

Thanks!

Related Discussions
...
  • Modifié

The rotation looks correct, I expect the translation is off. Are you positioning the CCSprite using both slot->bone->worldX/Y and attachment->x/y?

Nate a écrit

The rotation looks correct, I expect the translation is off. Are you positioning the CCSprite using both slot->bone->worldX/Y and attachment->x/y?

Yes, you are right, the rotation is right but the position is wrong. Here is my code for positioning CCSprite:

float minX = FLT_MAX, minY = FLT_MAX, maxX = FLT_MIN, maxY = FLT_MIN;
float *vertices = (float*)malloc(sizeof(float) * 10);
spRegionAttachment_computeWorldVertices(attachment,slot->bone,vertices);
for (int ii = 0; ii < 8; ii += 2) {
    float x = vertices[ii], y = vertices[ii + 1];
    minX = min(minX, x);
    minY = min(minY, y);
    maxX = max(maxX, x);
    maxY = max(maxY, y);
}
CGPoint point = ccp((maxX + minX) *0.5, (maxY + minY) *0.5);
free(vertices);

point = [_skeleton convertToWorldSpace:point];
point = [batchNode convertToNodeSpace:point];
sprite.position = point;

Can you show me some details about positioning of the CCSprite using both slot->bone->worldX/Y and attachment->x/y? I don't know how, thank you.


The rotation looks correct on above, but the rotation angle of some bones are not right when I test another one.

Image supprimée en raison de l'absence de support de HTTPS. | Afficher quand même

The right is the spine skeleton, which is right.
The left are the sprites I created from the skeleton, the rotation angle of some bones are still not right, such as the neck, the right hand. I don't know why.

Thanks!

FWIW, the second to last tail segment is facing the wrong direction. Is that region rotated? Maybe you need to use -90?

You can simplify your positioning code, you don't need spRegionAttachment_computeWorldVertices. Try:

float x = bone->worldX + attachment->x * bone->m00 + attachment->y * bone->m01;
float y = bone->worldY + attachment->x * bone->m10 + attachment->y * bone->m11;

This is the bone world position plus the attachment offset rotated and scaled by the bone's world rotation and scale. The resulting point is the center of the attachment.

What is the reason you convert a skeleton to CCSprites? Maybe you could render to an FBO (render to texture)? If you are doing it for batching purposes, batching can be done better than a batching node:
http://discuss.cocos2d-x.org/t/current- ... ng/16985/8

I tried -90, the bones of dragon are all right, but some bones of King of Skeleton are wrong. I exchanged the rotated region's scaleX and scaleY, and I fixed it. 🙂