关于Cocos2d-x中多边形物理刚体的设置

1.如果想要设置某个物体有多边形的刚体,这样可以更精确地进行碰撞检测,可以用以下的方法

auto hero = PlaneHero::create();
addChild(hero, 0, HERO_TAG);
hero->setPosition(Vec2(winSize.width / 2, hero->getContentSize().height / 2 + 10));
//auto herobody = PhysicsBody::createBox(hero->getContentSize()); //这样设置不太精准
auto herobody = PhysicsBody::create();
Vec2 verts[] = {Vec2(0, 55), Vec2(50, -30), Vec2(-50, -30)}; //根据点组成一个多边形,这样设置的PhysicsBody是一个三角形,这里面的点的先后顺序必须可以连成一条线,不能随便写的,不然会停止运行
//herobody->addShape(PhysicsShapeEdgePolygon::create(verts, 3));(如果是边界的话用这句)

herobody->addShape(PhysicsShapePolygon::create(verts, 3));(如果是精灵的话用这句)
herobody->setCollisionBitmask(0x0); //不进行碰撞模拟
herobody->setContactTestBitmask(HERO_CONTACTMASKBIT);

herobody->setPositionOffset(Vec2(30, 0));(设置刚体和精灵节点绑定的相对位置,可以让它不总是在精灵的中心对齐)
hero->setPhysicsBody(herobody);

2.关于PhysicsShapeEdgePolygon函数

static PhysicsShapeEdgePolygon* create ( const Vec2 * points , int count , const PhysicsMaterial & material = PHYSICSSHAPE_MATERIAL_DEFAULT , float border = 1 )

points 多边形顶点数组。
count 多边形顶点数量。
material 物理材质PhysicsMaterial对象,默认值是PHYSICSSHAPE_MATERIAL_DEFAULT。
border 这是多边形的边框宽度。

例子

Vec2 verts[] = {Vec2(0, 55), Vec2(50, -30), Vec2(-50, -30)};  //根据点组成一个多边形

herobody->addShape(PhysicsShapeEdgePolygon::create(verts, 3));

原文地址:https://www.cnblogs.com/HangZhe/p/5762552.html