- Modificato
[cocos2d-x] multiple bounding boxes
I am using spine to layout some animated buttons in my game. I was simply drawing some transparent rectangles over the top to detect when they had been pressed but I'd like to use bounding boxes now since the positions of the buttons move.
I can't seem to find any examples of using bounding boxes in cocos2d-x. I can see that I can getBoundingBox on a skeleton but I'd also like to know whether it's possible to use multiple bounding boxes and find out which one has been hit/collided with.
Can anyone help?
You will have to use the spine-c API to intersect rays against bounding box attachments on your skeleton. For that, you'll need the pointer to an spSkeleton stored by SkeletonRenderer/SkeletonAnimation. You can use SkeletonXXX::getSkeleton() for that.
With the spSkeleton in hand, you can then create a spSkeletonBounds to get the bounding boxes and polygons for a skeleton
spSkeletonBounds* bounds = spSkeletonBounds_create();
// each frame update the bounds after you've updated your skeleton
// last parameter will update the AABB for the entire skeleton
spSkeletonBounds_update(bounds, skeleton, 1);
// then use the bounds to do intersection testing, which will return
// the first bounding box attachment that contains the point (x, y).
// you can then look at the attachment to figure out what button
// was pressed
spBoundingBoxAttachment* attachment = spSkeletonBounds_containsPoint(bounds, x, y);
See the full API here spine-runtimes/SkeletonBounds.h at master
Great thanks!