cocos2d-x 3.0 PhysicsEditor 加载plist PEShapeCache_X3_0

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.0 PhysicsEditor 加载plist PEShapeCache_X3_0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载来自http://www.58player.com/blog-2479-100819.html

一、环境:

  1. cocos2d-x 3.0
  2. PhysicsEditor 1.0.10

二、cocos2d-x 3.0 篇:

  1. 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
  2. 测试代码如下:` #include "HelloWorldScene.h"
  3. #include "VisibleRect.h"
  4. #include "ResourcesPath.h"
  5. #include "PEShapeCache_X3_0.h"
  6. USING_NS_CC;
  7. static const int DRAG_BODYS_TAG = 0x79;
  8. static const int DRAG_BODYS_TAG_Y = 0x80;
  9. static const int DRAG_BODYS_TAG1 = 0x81;
  10. Scene *HelloWorld::createScene()
  11. {
  12. auto scene = Scene::createWithPhysics();
  13. auto layer = HelloWorld::create();
  14. scene->addChild(layer);
  15. // Device::setAccelerometerEnabled(true);
  16. return scene;
  17. }
  18. void HelloWorld::onEnter()
  19. {
  20. Layer::onEnter();
  21. //初始化父节点
  22. _scene = dynamic_cast<Scene *>(this->getParent());
  23. //注册触摸监听器
  24. auto touchListener = EventListenerTouchOneByOne::create();
  25. touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
  26. touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
  27. touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
  28. _eventDispatcher->addEventListenerWithSceneGraPHPriority(touchListener,this);
  29. // auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::onAcceleration,this));初始一个重力感应监听
  30. // _eventDispatcher->addEventListenerWithSceneGraPHPriority(accListener,this);注册重力感应监听
  31. //设置物理世界
  32. _scene->getPhysicsWorld()->setGravity(Vect(0,0)); //设置重力
  33. _scene->getPhysicsWorld()->setSpeed(1.0f); //物理世界速度 速度越快刚体越快
  34. _scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); //绘制调试绘制
  35. //添加拼图模板图片
  36. auto bg = Sprite::create(shizi_di_png);
  37. bg->setPosition(VisibleRect::center());
  38. this->addChild(bg);
  39.  
  40. PEShapeCache::getInstance()->addBodysWithFile(buttonsbodys_plist);
  41.  
  42.  
  43. this->addPhysicsSprite();
  44. }
  45. void HelloWorld::addPhysicsSprite()
  46. {
  47. // _scene->getPhysicsWorld()->setUpdateRate(5.0f);
  48. // wall 添加物理边境
  49. Size size = VisibleRect::getVisibleRect().size;
  50. auto wall = Node::create();
  51. //给节点添加静态矩形刚体(PhysicsBody)并带材质(PhysicsMaterial)
  52. wall->setPhysicsBody(PhysicsBody::createEdgeBox(Size(size.width - 5,size.height - 5),PhysicsMaterial(0.1f,1.0f,1.0f)));
  53. // wall->getPhysicsBody()->setDynamic(false);//设置为静态刚体(Edge
  54. // wall->getPhysicsBody()->setEnable(false);
  55. wall->getPhysicsBody()->setGroup(1); //组编号
  56. /**一个body的CategoryBitmask和另一个body的ContactTestBitmask的逻辑与的结果不等于0时,接触事件将被发出,否则不发送。
  57. *一个body的CategoryBitmask和另一个body的CollisionBitmask的逻辑与结果不等于0时,他们将碰撞,否则不碰撞
  58. */
  59. // wall->getPhysicsBody()->setCategoryBitmask(0x03);// 0011 碰撞系数编号
  60. // wall->getPhysicsBody()->setContactTestBitmask(0x03);// 0001 碰撞检测编号
  61. // wall->getPhysicsBody()->setCollisionBitmask(0x01);// 0001 碰撞编号
  62. wall->setPosition(VisibleRect::center()); //位置可见区域中心
  63. this->addChild(wall);
  64.  
  65. //多纳的头
  66. auto tou = Sprite::create(shizi_tou_png);
  67. // tou->setPhysicsBody(PhysicsBody::createCircle(271,PhysicsMaterial(1000.1f,0.0f,1.0f)));//添加半径为271动态圆形刚体 并赋予材质密度1000.1f 反弹力0.0f 摩擦力1.0f
  68. auto toubody = PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_tou");
  69. tou->setPhysicsBody(toubody);
  70. tou->getPhysicsBody()->setTag(DRAG_BODYS_TAG);//给刚体设置标签
  71. tou->getPhysicsBody()->setMass(0.1);//刚体设置质量
  72. tou->getPhysicsBody()->setGroup(2);//刚体组编号
  73. tou->getPhysicsBody()->setCategoryBitmask(0x01); // 0001
  74. tou->getPhysicsBody()->setContactTestBitmask(0x01); // 0001
  75. tou->getPhysicsBody()->setCollisionBitmask(0x01); // 0001
  76. // tou->getPhysicsBody()->setDynamic(false); 动态刚体是可以设置为静态
  77. tou->getPhysicsBody()->setRotationEnable(false); //设置不可旋转刚体 碰撞后也不会旋转刚体
  78. // tou->getPhysicsBody()->setGravityEnable(false);//设置是否接受重力影响
  79. tou->getPhysicsBody()->setLinearDamping(3.0f); //设置线性阻尼系数 理论是0-1 但是可以大于1 值越大惯性越小
  80. this->addChild(tou);
  81. tou->setPosition(VisibleRect::center());
  82. auto touyingzi = Node::create();
  83. touyingzi->setPhysicsBody(PhysicsBody::createCircle(271,1.0f)));
  84. touyingzi->getPhysicsBody()->setTag(DRAG_BODYS_TAG_Y);
  85.  
  86. //多纳的身子
  87. //PhysicsShapePolygon 是通过点数组来构建不规则的凸多边形;用工具PhysicsEditor 编辑shap 让后导出Chipmunk 格式的plist 中的数据 注:PhysicsEditor Relative为锚点Anchor 设置为cocos默认值(0.5,0.5)才行 ,
  88. auto shengzi = Sprite::create(shizi_shenzi_png);
  89. // auto shengzibody = PhysicsBody::create();
  90. // Point vert1[3] = {Point(109.50000,71.00000),Point(14.00000,77.00000),Point(117.50000,147.00000)};
  91. // shengzibody->addShape(PhysicsShapePolygon::create(vert1,3,1.0f)));
  92. // Point vert2[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)};
  93. // shengzibody->addShape(PhysicsShapePolygon::create(vert2,6,1.0f)));
  94. // Point vert3[6] = {Point(138.50000,18.00000),Point(110.50000,-177.00000),Point(51.50000,-175.00000),Point(109.50000,71.00000)};
  95. // shengzibody->addShape(PhysicsShapePolygon::create(vert3,1.0f)));
  96. // Point vert4[4] = {Point(-67.50000,Point(-55.00000,172.50000),Point(-54.00000,77.00000)};
  97. // shengzibody->addShape(PhysicsShapePolygon::create(vert4,4,1.0f)));
  98.  
  99. auto shengzibody = PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_shenzi");
  100. shengzi->setPhysicsBody(shengzibody);
  101. shengzibody->setTag(DRAG_BODYS_TAG1);
  102. shengzibody->setRotationEnable(false);
  103. shengzibody->setGroup(2);
  104. shengzibody->setMass(0.1);
  105. shengzibody->setCategoryBitmask(0x02); //0010
  106. shengzibody->setContactTestBitmask(0x02); //0010
  107. shengzibody->setCollisionBitmask(0x02); //0010
  108. shengzibody->setLinearDamping(3.0f);
  109. this->addChild(shengzi);
  110. shengzi->setPosition(VisibleRect::bottom() - Point(0,-300));
  111. // auto contactListener = EventListenerPhysicsContactWithBodies::create(tou->getPhysicsBody(),shengzi->getPhysicsBody());
  112. // contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin,this);
  113. // _eventDispatcher->addEventListenerWithSceneGraPHPriority(contactListener,this);
  114.  
  115.  
  116. //多纳的头
  117. auto b1 = Sprite::create(shizi_youshou_png);
  118. b1->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_youshou"));
  119. b1->setPosition(VisibleRect::center()+Point(300,0));
  120. b1->getPhysicsBody()->setTag(1);
  121. b1->getPhysicsBody()->setRotationEnable(false);
  122. this->addChild(b1);
  123. auto b2 = Sprite::create(shizi_zuoshou_png);
  124. b2->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_zuoshou"));
  125. b2->setPosition(VisibleRect::center()-Point(300,0));
  126. b2->getPhysicsBody()->setRotationEnable(false);
  127. b2->getPhysicsBody()->setTag(1);
  128. this->addChild(b2);
  129.  
  130.  
  131.  
  132. // LabelTTF
  133. auto label2 = LabelTTF::create("多纳小狮子爱学习","Arial",64);
  134. label2->setPhysicsBody(PhysicsBody::createBox(label2->getBoundingBox().size,1.0f)));
  135. label2->getPhysicsBody()->setTag(1);
  136. // label2->getPhysicsBody()->setRotationEnable(false);
  137. label2->setPosition(VisibleRect::center()+Point(0,300));
  138. addChild(label2,0);
  139.  
  140. PEShapeCache::getInstance()->removeBodysWithWithFile(buttonsbodys_plist);
  141.  
  142. //注册碰撞检测监听
  143. auto contactListener1 = EventListenerPhysicsContact::create();
  144. contactListener1->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin,this);
  145. contactListener1->onContactPostSolve = CC_CALLBACK_2(HelloWorld::onContactPostSolve,this);
  146. _eventDispatcher->addEventListenerWithSceneGraPHPriority(contactListener1,this);
  147. }
  148.  
  149. void HelloWorld::onContactPostSolve(PhysicsContact &contact,const PhysicsContactPostSolve &solve)
  150. {
  151. CCLOG("%s","################## onContactPostSolve");
  152. }
  153. bool HelloWorld::onContactBegin(PhysicsContact &contact)
  154. {
  155. CCLOG("%s","################## onContactBegin");
  156. return true; // contact.getContactData()->normal.y < 0;
  157. }
  158. void HelloWorld::onAcceleration(Acceleration *acc,Event *event)
  159. {
  160. static float prevX = 0,prevY = 0;
  161. #define kFilterFactor 0.05f
  162. float accelX = (float)acc->x * kFilterFactor + (1 - kFilterFactor) * prevX;
  163. float accelY = (float)acc->y * kFilterFactor + (1 - kFilterFactor) * prevY;
  164. prevX = accelX;
  165. prevY = accelY;
  166. auto v = Point(accelX,accelY);
  167. v = v * 200;
  168. if (_scene != nullptr)
  169. {
  170. _scene->getPhysicsWorld()->setGravity(v);
  171. }
  172. }
  173. bool HelloWorld::onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *event)
  174. {
  175. CCLOG("%s","touch");
  176. auto location = touch->getLocation();
  177. auto arr = _scene->getPhysicsWorld()->getShapes(location);//从物理世界得到多边形
  178. PhysicsBody *body = nullptr;
  179. for (auto &obj : arr)
  180. {
  181. if ((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0) //得到刚体
  182. {
  183. body = obj->getBody();
  184. break;
  185. }
  186. }
  187. if (body != nullptr)
  188. {
  189. //创建一个刚体
  190. Node *mouse = Node::create();
  191. mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY,PHYSICS_INFINITY));
  192. mouse->getPhysicsBody()->setDynamic(false);
  193. mouse->setPosition(location);
  194. this->addChild(mouse);
  195. body->setLinearDamping(0.0f);
  196. //用图钉关节与点中刚体绑定 赋予力 可以拖动
  197. PhysicsJointPin *joint = PhysicsJointPin::construct(mouse->getPhysicsBody(),body,location);
  198. joint->setMaxForce(5000.0f * body->getMass());
  199. _scene->getPhysicsWorld()->addJoint(joint);
  200. _mouses.insert(std::make_pair(touch->getID(),mouse));
  201. return true;
  202. }
  203. return false;
  204. }
  205. void HelloWorld::onTouchMoved(Touch *touch,Event *event)
  206. {
  207. auto it = _mouses.find(touch->getID());
  208. if (it != _mouses.end())
  209. {
  210. it->second->setPosition(touch->getLocation());
  211. }
  212. }
  213. void HelloWorld::onTouchEnded(Touch *touch,Event *event)
  214. {
  215. auto it = _mouses.find(touch->getID());
  216. if (it != _mouses.end())
  217. {
  218. this->removeChild(it->second);
  219. _mouses.erase(it);
  220. }
  221.  
  222. //增加摩擦阻尼 减小惯性
  223. PhysicsBody *body = _scene->getPhysicsWorld()->getBody(DRAG_BODYS_TAG);
  224. if (body != nullptr)
  225. {
  226. body->setLinearDamping(2.5f);
  227. }
  228. PhysicsBody *body1 = _scene->getPhysicsWorld()->getBody(DRAG_BODYS_TAG1);
  229. if (body1 != nullptr)
  230. {
  231. body1->setLinearDamping(2.5f);
  232. }
  233. }
  234. void HelloWorld::menuCloseCallback(Ref *pSender)
  235. {
  236. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  237. MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  238. return;
  239. #endif
  240. Director::getInstance()->end();
  241. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  242. exit(0);
  243. #endif
  244. }`

三、demo 和 库下载

  1. Github 传送门:

https://github.com/baibai2013/PhysicsEditor-Loader-for-cocos2d-x-3.0

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