希望对大家有用。
//http://blog.csdn.net/hjh2005/article/details/9246967
static bool pointInPolygon(const std::vector<Point> &vertices, const cocos2d::Point &p) {
int polySides = vertices.size();
float x = p.x, y = p.y;
int i,j=polySides-1 ;
bool oddNodes= false ;
for (i=0;i<polySides; i++) {
auto vi = vertices*, vj = vertices;
if((vi.y < y && vj.y>=y || vj.y<y && vi.y>=y) && (vi.x<=x || vj.x<=x)) {
oddNodes^=(vi.x+(y-vi.y)/(vj.y-vi.y)*(vj.x-vi.x)<x);
}
j=i;
}
return oddNodes;
}
//判断某个点是否在Armature内,比如touch event
bool JSWrapper::hitArmature(cocostudio::Armature *armature, const cocos2d::Point &worldPoint) {
if( !armature || !armature->getParent() )
return false;
const Map<std::string, cocostudio::Bone*>& map = armature->getBoneDic();
Point localPoint = armature->getParent()->convertToNodeSpace(worldPoint);
for(const auto& element : map) {
cocostudio::Bone *bone = element.second;
cocostudio::ColliderDetector *detector = bone->getColliderDetector();
if (!detector)
continue;
const cocos2d::Vector<cocostudio::ColliderBody*>& bodyList = detector->getColliderBodyList();
if( bodyList.size() == 0 )
continue;
//Point localPoint = bone->convertToNodeSpace(worldPoint);
for (const auto& object : bodyList)
{
cocostudio::ColliderBody *body = static_cast<cocostudio::ColliderBody*>(object);
const std::vector<Point> &vertexList = body->getCalculatedVertexList();
if( pointInPolygon(vertexList, localPoint) )
return true;
}
}
return false;
}
*