
一、环境:
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。
测试代码如下:
[cpp] view plain copy print ? #include"HelloWorldScene.h" #include"VisibleRect.h" #include"ResourcesPath.h" #include"PEShapeCache_X3_0.h" USING_NS_CC; staticconstintDRAG_BODYS_TAG=0x79; intDRAG_BODYS_TAG_Y=0x80; intDRAG_BODYS_TAG1=0x81; Scene*HelloWorld::createScene() { autoscene=Scene::createWithPhysics(); autolayer=HelloWorld::create(); scene->addChild(layer); //Device::setAccelerometerEnabled(true); returnscene; } voIDHelloWorld::onEnter() Layer::onEnter(); //初始化父节点 _scene=dynamic_cast<Scene*>(this->getParent()); //注册触摸监听器 autotouchListener=EventListenertouchOneByOne::create(); touchListener->ontouchBegan=CC_CALLBACK_2(HelloWorld::ontouchBegan,this); touchListener->ontouchmoved=CC_CALLBACK_2(HelloWorld::ontouchmoved,153); Font-weight:bold; background-color:inherit">this); 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));初始一个重力感应监听 //_eventdispatcher->addEventListenerWithSceneGraPHPriority(accListener,this);注册重力感应监听 //设置物理世界 _scene->getPhysicsWorld()->setGravity(Vect(0,0));//设置重力 _scene->getPhysicsWorld()->setSpeed(1.0f);//物理世界速度速度越快刚体越快 _scene->getPhysicsWorld()->setDeBUGDrawMask(PhysicsWorld::DEBUGDRAW_ALL);//绘制调试绘制 //添加拼图模板图片 autobg=Sprite::create(shizi_di_png); bg->setposition(VisibleRect::center()); this->addChild(bg); PEShapeCache::getInstance()->addBodysWithfile(buttonsbodys_pList); this->addPhysicssprite(); } voIDHelloWorld::addPhysicssprite() { //_scene->getPhysicsWorld()->setUpdaterate(5.0f); //wall添加物理边境 Sizesize=VisibleRect::getVisibleRect().size; autowall=Node::create(); //给节点添加静态矩形刚体(PhysicsBody)并带材质(PhysicsMaterial) wall->setPhysicsBody(PhysicsBody::createEdgeBox(Size(size.wIDth-5,size.height-5),PhysicsMaterial(0.1f,1.0f,1.0f))); //wall->getPhysicsBody()->setDynamic(false);//设置为静态刚体(Edge //wall->getPhysicsBody()->setEnable(false); wall->getPhysicsBody()->setGroup(1);//组编号 /**一个body的categoryBitmask和另一个body的ContactTestBitmask的逻辑与的结果不等于0时,接触事件将被发出,否则不发送。 *一个body的categoryBitmask和另一个body的CollisionBitmask的逻辑与结果不等于0时,他们将碰撞,否则不碰撞 */ //wall->getPhysicsBody()->setcategoryBitmask(0x03);//0011碰撞系数编号 //wall->getPhysicsBody()->setContactTestBitmask(0x03);//0001碰撞检测编号 //wall->getPhysicsBody()->setCollisionBitmask(0x01);//0001碰撞编号 wall->setposition(VisibleRect::center());//位置可见区域中心 this->addChild(wall); //多纳的头 autotou=Sprite::create(shizi_tou_png); //tou->setPhysicsBody(PhysicsBody::createCircle(271,PhysicsMaterial(1000.1f,0.0f,1.0f)));//添加半径为271动态圆形刚体并赋予材质密度1000.1f反d力0.0f摩擦力1.0f autotoubody=PEShapeCache::getInstance()->getPhysicsBodyByname("shizi_tou"); tou->setPhysicsBody(toubody); tou->getPhysicsBody()->setTag(DRAG_BODYS_TAG);//给刚体设置标签 tou->getPhysicsBody()->setMass(0.1);//刚体设置质量 tou->getPhysicsBody()->setGroup(2);//刚体组编号 tou->getPhysicsBody()->setcategoryBitmask(0x01);//0001 tou->getPhysicsBody()->setContactTestBitmask(0x01);//0001 tou->getPhysicsBody()->setCollisionBitmask(0x01);//tou->getPhysicsBody()->setDynamic(false);动态刚体是可以设置为静态 tou->getPhysicsBody()->setRotationEnable(false);//设置不可旋转刚体碰撞后也不会旋转刚体 //tou->getPhysicsBody()->setGravityEnable(false);//设置是否接受重力影响 tou->getPhysicsBody()->setlineardamPing(3.0f);//设置线性阻尼系数理论是0-1但是可以大于1值越大惯性越小 this->addChild(tou); tou->setposition(VisibleRect::center()); autotouyingzi=Node::create(); touyingzi->setPhysicsBody(PhysicsBody::createCircle(271,1.0f))); touyingzi->getPhysicsBody()->setTag(DRAG_BODYS_TAG_Y); //多纳的身子 //PhysiCSShapepolygon是通过点数组来构建不规则的凸多边形;用工具PhysicsEditor编辑shap让后导出Chipmunk格式的pList中的数据注:PhysicsEditorrelative为锚点Anchor设置为cocos默认值(0.5,0.5)才行, autoshengzi=Sprite::create(shizi_shenzi_png); //autoshengzibody=PhysicsBody::create(); //Pointvert1[3]={Point(109.50000,71.00000),Point(14.00000,77.00000),Point(117.50000,147.00000)}; //shengzibody->addShape(PhysiCSShapepolygon::create(vert1,3,1.0f))); //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)}; //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)}; //shengzibody->addShape(PhysiCSShapepolygon::create(vert3,0); background-color:inherit">//Pointvert4[4]={Point(-67.50000,Point(-55.00000,172.50000),Point(-54.00000,77.00000)}; //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"); shengzi->setPhysicsBody(shengzibody); shengzibody->setTag(DRAG_BODYS_TAG1); shengzibody->setRotationEnable(false); shengzibody->setGroup(2); shengzibody->setMass(0.1); shengzibody->setcategoryBitmask(0x02);//0010 shengzibody->setContactTestBitmask(0x02);//0010 shengzibody->setCollisionBitmask(0x02); shengzibody->setlineardamPing(3.0f); this->addChild(shengzi); shengzi->setposition(VisibleRect::bottom()-Point(0,-300)); //autocontactListener=EventListenerPhysicsContactWithBodIEs::create(tou->getPhysicsBody(),shengzi->getPhysicsBody()); //contactListener->onContactBegin=CC_CALLBACK_1(HelloWorld::onContactBegin,this); //_eventdispatcher->addEventListenerWithSceneGraPHPriority(contactListener,this); //多纳的头 autob1=Sprite::create(shizi_youshou_png); b1->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByname("shizi_youshou")); b1->setposition(VisibleRect::center()+Point(300,0)); b1->getPhysicsBody()->setTag(1); b1->getPhysicsBody()->setRotationEnable(false); this->addChild(b1); autob2=Sprite::create(shizi_zuoshou_png); b2->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByname("shizi_zuoshou")); 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); this->addChild(b2); //LabelTTF autolabel2=LabelTTF::create("多纳小狮子爱学习","Arial",64); 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); //label2->getPhysicsBody()->setRotationEnable(false); label2->setposition(VisibleRect::center()+Point(0,300)); addChild(label2,0); PEShapeCache::getInstance()->removeBodysWithWithfile(buttonsbodys_pList); //注册碰撞检测监听 autocontactListener1=EventListenerPhysicsContact::create(); 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) cclOG("%s","##################onContactpostsolve"); boolHelloWorld::onContactBegin(PhysicsContact&contact) "##################onContactBegin"); returntrue;//contact.getContactData()->normal.y<0; voIDHelloWorld::onacceleration(acceleration*acc,Event*event) staticfloatprevX=0,prevY=0; #definekFilterFactor0.05f floataccelX=(float)acc->x*kFilterFactor+(1-kFilterFactor)*prevX; floataccelY=(float)acc->y*kFilterFactor+(1-kFilterFactor)*prevY; prevX=accelX; prevY=accelY; autov=Point(accelX,accelY); v=v*200; if(_scene!=nullptr) _scene->getPhysicsWorld()->setGravity(v); boolHelloWorld::ontouchBegan(cocos2d::touch*touch,cocos2d::Event*event) "touch"); autolocation=touch->getLocation(); autoarr=_scene->getPhysicsWorld()->getShapes(location);//从物理世界得到多边形 PhysicsBody*body=nullptr; for(auto&obj:arr) if((obj->getbody()->getTag()&DRAG_BODYS_TAG)!=0)//得到刚体 body=obj->getbody(); break; if(body!=nullptr) //创建一个刚体 Node*mouse=Node::create(); mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY,PHYSICS_INFINITY)); mouse->getPhysicsBody()->setDynamic( mouse->setposition(location); this->addChild(mouse); body->setlineardamPing(0.0f); //用图钉关节与点中刚体绑定赋予力可以拖动 PhysicsJointPin*joint=PhysicsJointPin::construct(mouse->getPhysicsBody(),body,location); joint->setMaxForce(5000.0f*body->getMass()); _scene->getPhysicsWorld()->addJoint(joint); _mouses.insert(std::make_pair(touch->getID(),mouse)); true; false; voIDHelloWorld::ontouchmoved(touch*touch,Event*event) autoit=_mouses.find(touch->getID()); if(it!=_mouses.end()) it->second->setposition(touch->getLocation()); voIDHelloWorld::ontouchended(touch*touch,153); Font-weight:bold; background-color:inherit">this->removeChild(it->second); _mouses.erase(it); //增加摩擦阻尼减小惯性 PhysicsBody*body=_scene->getPhysicsWorld()->getbody(DRAG_BODYS_TAG); if(body!=nullptr) body->setlineardamPing(2.5f); PhysicsBody*body1=_scene->getPhysicsWorld()->getbody(DRAG_BODYS_TAG1); if(body1!=nullptr) body1->setlineardamPing(2.5f); voIDHelloWorld::menuCloseCallback(Ref*pSender) #if(CC_TARGET_PLATFORM==CC_PLATFORM_WP8)||(CC_TARGET_PLATFORM==CC_PLATFORM_WINRT) MessageBox("Youpressedtheclosebutton.windowsStoreAppsdonotimplementaclosebutton.","Alert"); return; #endif Director::getInstance()->end(); #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS) exit(0); } @H_404_1143@效果如下:
三、demo 和 库下载
Github 传送门:https://github.com/baibai2013/PhysicsEditor-Loader-for-cocos2d-x-3.0
总结以上是内存溢出为你收集整理的cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody全部内容,希望文章能够帮你解决cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)