- 已编辑
Help checking if a polygon bounding box contains point...
Trying to work out if a polygon bounding box contains a point... (cocos2dx / c++)
So far this line gets the Bounding attachment...
spBoundingBoxAttachment *boundat = (spBoundingBoxAttachment *)this->panAnim->getAttachment("pan1bounds", "pan1bounds");
i assume i need to do something like
auto bounds = spSkeletonBounds_getPolygon(????, boundat);
then
spSkeletonBounds_aabbContainsPoint(bounds, 0, 0);
but not sure how to get the spSkeletonBounds for the spSkeletonBounds_getPolygon...
Hope you can help...
Thanks
Update: ok this rough bit of code works, however am i getting the sbBoundingPolygon the best way possible?
Note: the /4, *2 & *4 are due to spine not interpreting content scale factor
bool BookPage7Scene::isPointInBounds(Point location, const char *boneNamme, const char *slotName, const char *attachmentName)
{
Size visibleSize = Director::getInstance()->getVisibleSize();
spBone *pan1bone = this->panAnim->findBone(boneNamme);
spBoundingBoxAttachment *boundingAttachment = (spBoundingBoxAttachment *)this->panAnim->getAttachment(slotName, attachmentName);
auto boundingPolygon = spBoundingPolygon_create(boundingAttachment->verticesCount);
for (int i = 0; i<boundingAttachment->verticesCount; i++) {
boundingPolygon->vertices[i]=boundingAttachment->vertices[i] / 4;
boundingPolygon->count+=1;
}
spBoundingBoxAttachment_computeWorldVertices(boundingAttachment, visibleSize.width * 2, visibleSize.height * 2, pan1bone, boundingPolygon->vertices);
bool clicked = spBoundingPolygon_containsPoint(boundingPolygon, location.x * 4, location.y * 4);
spBoundingPolygon_dispose(boundingPolygon);
return clicked;
}
That is one way to do it. Another way is here. You give a Skeleton to a SkeletonBounds and it computes all the world vertices for the attached bounding polygons. Then SkeletonBounds_containsPoint returns the first BoundingBoxAttachment that contains the specified point, if any.
The /4. *2. *4 are a bit odd. Could you use 1 / contentScaleFactor as a scale for SkeletonJson when you load the SkeletonData?