首页 > 其他 > 详细

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

时间:2016-08-11 22:18:44      阅读:167      评论:0      收藏:0      [点我收藏+]

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->setCollisionBitmask(0x0); //不进行碰撞模拟
herobody->setContactTestBitmask(HERO_CONTACTMASKBIT);
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));

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

原文:http://www.cnblogs.com/HangZhe/p/5762552.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!