- 浏览: 511711 次
文章分类
最新评论
cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody
一、环境:
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; static const int DRAG_BODYS_TAG = 0x79; static const int DRAG_BODYS_TAG_Y = 0x80; static const int DRAG_BODYS_TAG1 = 0x81; Scene *HelloWorld::createScene() { auto scene = Scene::createWithPhysics(); auto layer = HelloWorld::create(); scene->addChild(layer); // Device::setAccelerometerEnabled(true); return scene; } void HelloWorld::onEnter() { Layer::onEnter(); //初始化父节点 _scene = dynamic_cast<Scene *>(this->getParent()); //注册触摸监听器 auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this); touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); // auto accListener = 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); //绘制调试绘制 //添加拼图模板图片 auto bg = Sprite::create(shizi_di_png); bg->setPosition(VisibleRect::center()); this->addChild(bg); PEShapeCache::getInstance()->addBodysWithFile(buttonsbodys_plist); this->addPhysicsSprite(); } void HelloWorld::addPhysicsSprite() { // _scene->getPhysicsWorld()->setUpdateRate(5.0f); // wall 添加物理边境 Size size = VisibleRect::getVisibleRect().size; auto wall = 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); //多纳的头 auto tou = Sprite::create(shizi_tou_png); // tou->setPhysicsBody(PhysicsBody::createCircle(271, PhysicsMaterial(1000.1f, 0.0f, 1.0f)));//添加半径为271动态圆形刚体 并赋予材质密度1000.1f 反弹力0.0f 摩擦力1.0f auto toubody = 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); // 0001 // 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()); auto touyingzi = Node::create(); touyingzi->setPhysicsBody(PhysicsBody::createCircle(271, PhysicsMaterial(1000.1f, 0.0f, 1.0f))); touyingzi->getPhysicsBody()->setTag(DRAG_BODYS_TAG_Y); //多纳的身子 //PhysicsShapePolygon 是通过点数组来构建不规则的凸多边形;用工具PhysicsEditor 编辑shap 让后导出Chipmunk 格式的plist 中的数据 注:PhysicsEditor Relative为锚点Anchor 设置为cocos默认值(0.5,0.5)才行 , auto shengzi = Sprite::create(shizi_shenzi_png); // auto shengzibody = PhysicsBody::create(); // Point vert1[3] = {Point(109.50000, 71.00000), Point(14.00000, 77.00000), Point(117.50000, 147.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert1, 3, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert2[6] = {Point(-130.50000, -154.00000), Point(-120.50000, 46.00000), Point(-67.50000, 102.00000), Point(14.00000, 77.00000), Point(-4.00000, -93.00000), Point(-63.00000, -178.50000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert2, 6, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert3[6] = {Point(138.50000, 18.00000), Point(110.50000, -177.00000), Point(51.50000, -175.00000), Point(-4.00000, -93.00000), Point(14.00000, 77.00000), Point(109.50000, 71.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert3, 6, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert4[4] = {Point(-67.50000, 102.00000), Point(-55.00000, 172.50000), Point(-54.00000, 172.50000), Point(14.00000, 77.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert4, 4, PhysicsMaterial(0.1f, 0.0f, 1.0f))); auto shengzibody = 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); //0010 shengzibody->setLinearDamping(3.0f); this->addChild(shengzi); shengzi->setPosition(VisibleRect::bottom() - Point(0, -300)); // auto contactListener = EventListenerPhysicsContactWithBodies::create(tou->getPhysicsBody(), shengzi->getPhysicsBody()); // contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); // _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); //多纳的头 auto b1 = 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); auto b2 = Sprite::create(shizi_zuoshou_png); b2->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_zuoshou")); b2->setPosition(VisibleRect::center()-Point(300,0)); b2->getPhysicsBody()->setRotationEnable(false); b2->getPhysicsBody()->setTag(1); this->addChild(b2); // LabelTTF auto label2 = LabelTTF::create("多纳小狮子爱学习", "Arial", 64); label2->setPhysicsBody(PhysicsBody::createBox(label2->getBoundingBox().size, PhysicsMaterial(0.1f, 0.0f, 1.0f))); label2->getPhysicsBody()->setTag(1); // label2->getPhysicsBody()->setRotationEnable(false); label2->setPosition(VisibleRect::center()+Point(0,300)); addChild(label2, 0); PEShapeCache::getInstance()->removeBodysWithWithFile(buttonsbodys_plist); //注册碰撞检测监听 auto contactListener1 = EventListenerPhysicsContact::create(); contactListener1->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); contactListener1->onContactPostSolve = CC_CALLBACK_2(HelloWorld::onContactPostSolve, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener1, this); } void HelloWorld::onContactPostSolve(PhysicsContact &contact, const PhysicsContactPostSolve &solve) { CCLOG("%s", "################## onContactPostSolve"); } bool HelloWorld::onContactBegin(PhysicsContact &contact) { CCLOG("%s", "################## onContactBegin"); return true; // contact.getContactData()->normal.y < 0; } void HelloWorld::onAcceleration(Acceleration *acc, Event *event) { static float prevX = 0, prevY = 0; #define kFilterFactor 0.05f float accelX = (float)acc->x * kFilterFactor + (1 - kFilterFactor) * prevX; float accelY = (float)acc->y * kFilterFactor + (1 - kFilterFactor) * prevY; prevX = accelX; prevY = accelY; auto v = Point(accelX, accelY); v = v * 200; if (_scene != nullptr) { _scene->getPhysicsWorld()->setGravity(v); } } bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) { CCLOG("%s", "touch"); auto location = touch->getLocation(); auto arr = _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(false); 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)); return true; } return false; } void HelloWorld::onTouchMoved(Touch *touch, Event *event) { auto it = _mouses.find(touch->getID()); if (it != _mouses.end()) { it->second->setPosition(touch->getLocation()); } } void HelloWorld::onTouchEnded(Touch *touch, Event *event) { auto it = _mouses.find(touch->getID()); if (it != _mouses.end()) { 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); } } void HelloWorld::menuCloseCallback(Ref *pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert"); return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
效果如下:
三、demo 和 库下载
Github 传送门:https://github.com/baibai2013/PhysicsEditor-Loader-for-cocos2d-x-3.0
相关推荐
cocos2d-x 3.0 人物行走 . 包里有代码和 图片资源.
这是我重新弄的cocos2d-x-3.0的类图.之前别人兄台弄的,有些不全面,有些地方错误.我这个可以说是最新的了.每个类添加了中文的详细注解,同时也添加了中文的类名称翻译.这样对cocos2d-x-3.0的框架比较好上手. 有兴趣的...
Cocos2d-x3.0正式版 练练看 源码。有注释。
兄弟连cocos2d-x3.0 final视频教程 1.Cocos2d-x手机游戏开发C编程基础 2.Cocos2d-x手机游戏开发C++编程基础 3.Cocos2d-x3.0 final手机游戏开发核心技术 4.实例:贪吃蛇 5.实例:微信飞机大战
这是cocos2d-x 3.0 类关系图, 这个制作者是:gamecocos2dx 他做的Xmind,我导出的PNG格式,我推荐还是看Xmind,里面还有一些注释 gamecocos2dx Xmind下载地址:...
这是一个cocos2d-x 3.0中文开发文档.
http://blog.csdn.net/zoyzn/article/details/27251633一文的源码。cocos2d-x 3.0导出自定义类到lua,并导出自定义类的名称空间到lua。
自制的cocos2d-x 3.0alpha1项目创建包,适用于cocos2d-x 3.0alpha1版,用此资源包创建的cocos2d-x 3.0alpha1项目,可以拷贝至任意目录下
cocos2d-x 3.0 骨骼动画 demo 以及注意事项
本游戏基于cocos2d-x 3.0版本,可能不适用于cocos2d-x 2.x版本,请看清楚,再参考! 使用方法:将Classes中的类全部拷贝到新建cocos项目的Classes项目中,将Resources下的所有文件拷贝到对应的文件下,运行即可!
cocos2d-x 3.0 rapidjson 读取Json
官网中文docs文档的例子下载 创建工程的时候一定要创建一样的文件名 http://www.cocos2d-x.org/docs/tutorial/framework/native/how-to-drag-and-drop-sprites/zh
资源名称:Cocos2d-x实战:JS卷——Cocos2d-JS开发内容简介:本书是介绍Cocos2d-x游戏编程和开发技术书籍,介绍了使用Cocos2d-JS中核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、性能优化、多平台...
Cocos2d-x3.0final 自学教程的配套代码http://blog.csdn.net/column/details/cocos2dx2014.html
cocos2d-x 3.0 开发文档英文版(html)
这个资源包提供了两个类,这两个类分别可以读取plist的文件和xml的文件,对文件中的字段...所用的版本是cocos2d-x 3.0 bate版本,代码中有用到 CPubFun::系列的函数,是自己封装的,用可以可以用c++基本的函数来替换。
Cocos2d-x-3.x游戏开发之旅-钟迪龙著 全新pdf版和附书代码(代码为工程文件,可复制) 附带目录标签
Cocos2d-x3.0游戏实例《别救我》源码。 教程地址:http://blog.csdn.net/musicvs/article/details/24928929
Cocos2d-x 3.0 过渡学习 尊重原创,转载来自:star特530的CSDN博客 :http://blog.csdn.net/star530?viewmode=contents
我自己从 cocos2d-x 3.0 版中的doc目录下使用doxygen自己生成的离线文档,希望能帮助大家