一、环境:
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。
测试代码如下:
- #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反弹力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);
- }
效果如下:
三、demo 和 库下载
Github 传送门:https://github.com/baibai2013/PhysicsEditor-Loader-for-cocos2d-x-3.0