• Runtimes
  • [cocos2d-x] bounding box not moved

Hi,

I have multiple skeletons on the screen:

for(int i=0; i<4; i++){
        auto shark = SkeletonAnimation::createWithData(_sharkSkeletonData, false);
        
shark->setPosition(sharkData[i].pos); shark->setScale(sharkData[i].scale); shark->setAnimation(0, "Swim", true); sharks.pushBack(shark); }

These skeletons all have a bounding box and are moved using

setPosition

in coco2d-x. When I try to do hit testing on the bounding boxes, their position does not seem to be taken into account:

// The location of the touch
auto location = touch->getLocation();

for(int i=0; i<sharks.size(); i++){

// We then need to update the bounds to get their current position
spSkeletonBounds* bounds = spSkeletonBounds_create();
    
spSkeletonBounds_update(bounds, sharks.at(i)->getSkeleton(), true);
    
// Then we have to do is check if the click was within the bounds!
spBoundingBoxAttachment* attachment = spSkeletonBounds_containsPoint(bounds, location.x, location.y);
    
if(attachment){
    
    //if we have hit our bounding box called SharkBox
    if (strcmp(attachment->super.super.name, "SharkBox") == 0) {
            
        sharks.at(i)->setAnimation(0, "SwimFast", false);
    }
        
}
}

Even though the skeleton animations positions are being set - their bounding boxes are still positioned at 0,0.

Can someone let me know what I am doing wrong/how to get around this?

Thanks!

Related Discussions
...
c_h_r_i_s wrote

Even though the skeleton animations positions are being set - their bounding boxes are still positioned at 0,0.

Is there debug rendering you can enable so you can see the bounding boxes? Bounding boxes don't have a single position, instead they have any number of vertices. They have local vertices (relative to the bone) and world vertices (relative to the skeleton position). It may be that location is in the wrong coordinate system. When location is 0,0 it should correspond to the skeleton position. Is that what you see?

Ah got it thanks! Just needed to adjust the position of location for the position of each skeleton:

// The location of the click
auto location = touch->getLocation() - BookRect::leftBottom();
location.x -= sharks.at(i)->getPosition().x;
location.y -= sharks.at(i)->getPosition().y;

Just a quick note

spSkeletonBounds_update(bounds, sharks.at(i)->getSkeleton(), true);

is the spine-c API, which takes into account spSkeleton->x/y, but not the cocos2d-x Node position. One would have to adjust the bounds by the node position after updating them like above.