1. 程式人生 > >cocos2d-x 獲取spine邊界框

cocos2d-x 獲取spine邊界框

Rect SkeletonRenderer::getBoundingBox2(const std::string& name) const {
    float minX = FLT_MAX, minY = FLT_MAX, maxX = FLT_MIN, maxY = FLT_MIN;
    float scaleX = getScaleX(), scaleY = getScaleY();
    for (int i = 0; i < _skeleton->slotsCount; ++i) {
        spSlot* slot = _skeleton->slots;
        if (!slot->attachment) continue;
        int verticesCount;
        if (slot->attachment->type == SP_ATTACHMENT_BOUNDING_BOX)
        {
            if (slot->attachment->name == name)
            {
                spBoundingBoxAttachment* spa = (spBoundingBoxAttachment*)slot->attachment;
                spBoundingBoxAttachment_computeWorldVertices(spa, slot->bone, _worldVertices);
                verticesCount = spa->verticesCount;
                for (int ii = 0; ii < verticesCount; ii += 2) {
                    float x = _worldVertices[ii] * scaleX, y = _worldVertices[ii + 1] * scaleY;
                    minX = min(minX, x);
                    minY = min(minY, y);
                    maxX = max(maxX, x);
                    maxY = max(maxY, y);
                }


            }
        }

    }
    Vec2 position = getPosition();
    return Rect(position.x + minX, position.y + minY, maxX - minX, maxY - minY);
}