cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
分类cocos2d-http://blog.csdn.net/frabbit_on_fire/article/details/39025097
1185人阅读 评论(2) 收藏 举报

一、环境:

cocos2d-x 3.0

PhysicsEditor 1.0.10

二、cocos2d-x 3.0 篇:

从cocos2d-x 3.0 可以在scene 中直接添加物理世界后,开发物理效果的2d游戏相对轻松多了。可是编辑多边形的body 就成了问题,之前-x 2.x 版的PE loader 没有目前 CCPhysicsBody 的封装。本人就参考 之前的-x 2.x 版的loader 写了一个-x 3.x版的PE laoder,加载 chipmunk shapes ,并根据图片名 获得CCPhysicsBody。

测试代码如下:

  1. #include"HelloWorldScene.h"
  2. #include"VisibleRect.h"
  3. #include"ResourcesPath.h"
  4. #include"PEShapeCache_X3_0.h"
  5. USING_NS_CC;
  6. staticconstintDRAG_BODYS_TAG=0x79;
  7. intDRAG_BODYS_TAG_Y=0x80;
  8. intDRAG_BODYS_TAG1=0x81;
  9. Scene*HelloWorld::createScene()
  10. {
  11. autoscene=Scene::createWithPhysics();
  12. autolayer=HelloWorld::create();
  13. scene->addChild(layer);
  14. //Device::setAccelerometerEnabled(true);
  15. returnscene;
  16. }
  17. voidHelloWorld::onEnter()
  18. Layer::onEnter();
  19. //初始化父节点
  20. _scene=dynamic_cast<Scene*>(this->getParent());
  21. //注册触摸监听器
  22. autotouchListener=EventListenerTouchOneByOne::create();
  23. touchListener->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
  24. touchListener->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,153); font-weight:bold; background-color:inherit">this);
  25. touchListener->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _eventDispatcher->addEventListenerWithSceneGraPHPriority(touchListener,0); background-color:inherit">//autoaccListener=EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::onAcceleration,this));初始一个重力感应监听
  26. //_eventDispatcher->addEventListenerWithSceneGraPHPriority(accListener,this);注册重力感应监听
  27. //设置物理世界
  28. _scene->getPhysicsWorld()->setGravity(Vect(0,0));//设置重力
  29. _scene->getPhysicsWorld()->setSpeed(1.0f);//物理世界速度速度越快刚体越快
  30. _scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);//绘制调试绘制
  31. //添加拼图模板图片
  32. autobg=Sprite::create(shizi_di_png);
  33. bg->setPosition(VisibleRect::center());
  34. this->addChild(bg);
  35. PEShapeCache::getInstance()->addBodysWithFile(buttonsbodys_plist);
  36. this->addPhysicsSprite();
  37. }
  38. voidHelloWorld::addPhysicsSprite()
  39. {
  40. //_scene->getPhysicsWorld()->setUpdateRate(5.0f);
  41. //wall添加物理边境
  42. Sizesize=VisibleRect::getVisibleRect().size;
  43. autowall=Node::create();
  44. //给节点添加静态矩形刚体(PhysicsBody)并带材质(PhysicsMaterial)
  45. wall->setPhysicsBody(PhysicsBody::createEdgeBox(Size(size.width-5,size.height-5),PhysicsMaterial(0.1f,1.0f,1.0f)));
  46. //wall->getPhysicsBody()->setDynamic(false);//设置为静态刚体(Edge
  47. //wall->getPhysicsBody()->setEnable(false);
  48. wall->getPhysicsBody()->setGroup(1);//组编号
  49. /**一个body的CategoryBitmask和另一个body的ContactTestBitmask的逻辑与的结果不等于0时,接触事件将被发出,否则不发送。
  50. *一个body的CategoryBitmask和另一个body的CollisionBitmask的逻辑与结果不等于0时,他们将碰撞,否则不碰撞
  51. */
  52. //wall->getPhysicsBody()->setCategoryBitmask(0x03);//0011碰撞系数编号
  53. //wall->getPhysicsBody()->setContactTestBitmask(0x03);//0001碰撞检测编号
  54. //wall->getPhysicsBody()->setCollisionBitmask(0x01);//0001碰撞编号
  55. wall->setPosition(VisibleRect::center());//位置可见区域中心
  56. this->addChild(wall);
  57. //多纳的头
  58. autotou=Sprite::create(shizi_tou_png);
  59. //tou->setPhysicsBody(PhysicsBody::createCircle(271,PhysicsMaterial(1000.1f,0.0f,1.0f)));//添加半径为271动态圆形刚体并赋予材质密度1000.1f反弹力0.0f摩擦力1.0f
  60. autotoubody=PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_tou");
  61. tou->setPhysicsBody(toubody);
  62. tou->getPhysicsBody()->setTag(DRAG_BODYS_TAG);//给刚体设置标签
  63. tou->getPhysicsBody()->setMass(0.1);//刚体设置质量
  64. tou->getPhysicsBody()->setGroup(2);//刚体组编号
  65. tou->getPhysicsBody()->setCategoryBitmask(0x01);//0001
  66. tou->getPhysicsBody()->setContactTestBitmask(0x01);//0001
  67. tou->getPhysicsBody()->setCollisionBitmask(0x01);//tou->getPhysicsBody()->setDynamic(false);动态刚体是可以设置为静态
  68. tou->getPhysicsBody()->setRotationEnable(false);//设置不可旋转刚体碰撞后也不会旋转刚体
  69. //tou->getPhysicsBody()->setGravityEnable(false);//设置是否接受重力影响
  70. tou->getPhysicsBody()->setLinearDamping(3.0f);//设置线性阻尼系数理论是0-1但是可以大于1值越大惯性越小
  71. this->addChild(tou);
  72. tou->setPosition(VisibleRect::center());
  73. autotouyingzi=Node::create();
  74. touyingzi->setPhysicsBody(PhysicsBody::createCircle(271,1.0f)));
  75. touyingzi->getPhysicsBody()->setTag(DRAG_BODYS_TAG_Y);
  76. //多纳的身子
  77. //PhysicsShapePolygon是通过点数组来构建不规则的凸多边形;用工具PhysicsEditor编辑shap让后导出Chipmunk格式的plist中的数据注:PhysicsEditorRelative为锚点Anchor设置为cocos默认值(0.5,0.5)才行,
  78. autoshengzi=Sprite::create(shizi_shenzi_png);
  79. //autoshengzibody=PhysicsBody::create();
  80. //Pointvert1[3]={Point(109.50000,71.00000),Point(14.00000,77.00000),Point(117.50000,147.00000)};
  81. //shengzibody->addShape(PhysicsShapePolygon::create(vert1,3,1.0f)));
  82. //Pointvert2[6]={Point(-130.50000,-154.00000),Point(-120.50000,46.00000),Point(-67.50000,102.00000),Point(-4.00000,-93.00000),Point(-63.00000,-178.50000)};
  83. //shengzibody->addShape(PhysicsShapePolygon::create(vert2,6,0); background-color:inherit">//Pointvert3[6]={Point(138.50000,18.00000),Point(110.50000,-177.00000),Point(51.50000,-175.00000),Point(109.50000,71.00000)};
  84. //shengzibody->addShape(PhysicsShapePolygon::create(vert3,0); background-color:inherit">//Pointvert4[4]={Point(-67.50000,Point(-55.00000,172.50000),Point(-54.00000,77.00000)};
  85. //shengzibody->addShape(PhysicsShapePolygon::create(vert4,4,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> autoshengzibody=PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_shenzi");
  86. shengzi->setPhysicsBody(shengzibody);
  87. shengzibody->setTag(DRAG_BODYS_TAG1);
  88. shengzibody->setRotationEnable(false);
  89. shengzibody->setGroup(2);
  90. shengzibody->setMass(0.1);
  91. shengzibody->setCategoryBitmask(0x02);//0010
  92. shengzibody->setContactTestBitmask(0x02);//0010
  93. shengzibody->setCollisionBitmask(0x02); shengzibody->setLinearDamping(3.0f);
  94. this->addChild(shengzi);
  95. shengzi->setPosition(VisibleRect::bottom()-Point(0,-300));
  96. //autocontactListener=EventListenerPhysicsContactWithBodies::create(tou->getPhysicsBody(),shengzi->getPhysicsBody());
  97. //contactListener->onContactBegin=CC_CALLBACK_1(HelloWorld::onContactBegin,this);
  98. //_eventDispatcher->addEventListenerWithSceneGraPHPriority(contactListener,this);
  99. //多纳的头
  100. autob1=Sprite::create(shizi_youshou_png);
  101. b1->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_youshou"));
  102. b1->setPosition(VisibleRect::center()+Point(300,0));
  103. b1->getPhysicsBody()->setTag(1);
  104. b1->getPhysicsBody()->setRotationEnable(false);
  105. this->addChild(b1);
  106. autob2=Sprite::create(shizi_zuoshou_png);
  107. b2->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_zuoshou"));
  108. b2->setPosition(VisibleRect::center()-Point(300,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> b2->getPhysicsBody()->setRotationEnable( b2->getPhysicsBody()->setTag(1);
  109. this->addChild(b2);
  110. //LabelTTF
  111. autolabel2=LabelTTF::create("多纳小狮子爱学习","Arial",64);
  112. label2->setPhysicsBody(PhysicsBody::createBox(label2->getBoundingBox().size,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> label2->getPhysicsBody()->setTag(1);
  113. //label2->getPhysicsBody()->setRotationEnable(false);
  114. label2->setPosition(VisibleRect::center()+Point(0,300));
  115. addChild(label2,0);
  116. PEShapeCache::getInstance()->removeBodysWithWithFile(buttonsbodys_plist);
  117. //注册碰撞检测监听
  118. autocontactListener1=EventListenerPhysicsContact::create();
  119. contactListener1->onContactBegin=CC_CALLBACK_1(HelloWorld::onContactBegin,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> contactListener1->onContactPostSolve=CC_CALLBACK_2(HelloWorld::onContactPostSolve,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _eventDispatcher->addEventListenerWithSceneGraPHPriority(contactListener1,153); font-weight:bold; background-color:inherit">voidHelloWorld::onContactPostSolve(PhysicsContact&contact,constPhysicsContactPostSolve&solve)
  120. CCLOG("%s","##################onContactPostSolve");
  121. boolHelloWorld::onContactBegin(PhysicsContact&contact)
  122. "##################onContactBegin");
  123. returntrue;//contact.getContactData()->normal.y<0;
  124. voidHelloWorld::onAcceleration(Acceleration*acc,Event*event)
  125. staticfloatprevX=0,prevY=0;
  126. #definekFilterFactor0.05f
  127. floataccelX=(float)acc->x*kFilterFactor+(1-kFilterFactor)*prevX;
  128. floataccelY=(float)acc->y*kFilterFactor+(1-kFilterFactor)*prevY;
  129. prevX=accelX;
  130. prevY=accelY;
  131. autov=Point(accelX,accelY);
  132. v=v*200;
  133. if(_scene!=nullptr)
  134. _scene->getPhysicsWorld()->setGravity(v);
  135. boolHelloWorld::onTouchBegan(cocos2d::Touch*touch,cocos2d::Event*event)
  136. "touch");
  137. autolocation=touch->getLocation();
  138. autoarr=_scene->getPhysicsWorld()->getShapes(location);//从物理世界得到多边形
  139. PhysicsBody*body=nullptr;
  140. for(auto&obj:arr)
  141. if((obj->getBody()->getTag()&DRAG_BODYS_TAG)!=0)//得到刚体
  142. body=obj->getBody();
  143. break;
  144. if(body!=nullptr)
  145. //创建一个刚体
  146. Node*mouse=Node::create();
  147. mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY,PHYSICS_INFINITY));
  148. mouse->getPhysicsBody()->setDynamic( mouse->setPosition(location);
  149. this->addChild(mouse);
  150. body->setLinearDamping(0.0f);
  151. //用图钉关节与点中刚体绑定赋予力可以拖动
  152. PhysicsJointPin*joint=PhysicsJointPin::construct(mouse->getPhysicsBody(),body,location);
  153. joint->setMaxForce(5000.0f*body->getMass());
  154. _scene->getPhysicsWorld()->addJoint(joint);
  155. _mouses.insert(std::make_pair(touch->getID(),mouse));
  156. true;
  157. false;
  158. voidHelloWorld::onTouchMoved(Touch*touch,Event*event)
  159. autoit=_mouses.find(touch->getID());
  160. if(it!=_mouses.end())
  161. it->second->setPosition(touch->getLocation());
  162. voidHelloWorld::onTouchEnded(Touch*touch,153); font-weight:bold; background-color:inherit">this->removeChild(it->second);
  163. _mouses.erase(it);
  164. //增加摩擦阻尼减小惯性
  165. PhysicsBody*body=_scene->getPhysicsWorld()->getBody(DRAG_BODYS_TAG);
  166. if(body!=nullptr)
  167. body->setLinearDamping(2.5f);
  168. PhysicsBody*body1=_scene->getPhysicsWorld()->getBody(DRAG_BODYS_TAG1);
  169. if(body1!=nullptr)
  170. body1->setLinearDamping(2.5f);
  171. voidHelloWorld::menuCloseCallback(Ref*pSender)
  172. #if(CC_TARGET_PLATFORM==CC_PLATFORM_WP8)||(CC_TARGET_PLATFORM==CC_PLATFORM_WINRT)
  173. MessageBox("Youpressedtheclosebutton.WindowsStoreAppsdonotimplementaclosebutton.","Alert");
  174. return;
  175. #endif
  176. Director::getInstance()->end();
  177. #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
  178. exit(0);
  179. }

效果如下:







三、demo 和 库下载

Github 传送门:https://github.com/baibai2013/PhysicsEditor-Loader-for-cocos2d-x-3.0

原文链接:https://www.f2er.com/cocos2dx/343954.html

猜你在找的Cocos2d-x相关文章