cocos2d-x 3.0 PhysicsEditor 加载plist PEShapeCache_X3_0

cocos2d-x 3.0 PhysicsEditor 加载plist PEShapeCache_X3_0,第1张

概述转载来自http://www.58player.com/blog-2479-100819.html 一、环境: cocos2d-x 3.0 PhysicsEditor 1.0.10 二、cocos2d-x 3.0 篇: 从cocos2d-x 3.0 可以在scene 中直接添加物理世界后,开发物理效果的2d游戏相对轻松多了。可是编辑多边形的body 就成了问题,之前-x 2.x 版的P

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

一、环境:
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 反d力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,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,1.0f)));    //    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)};    //    shengzibody->addShape(PhysiCSShapepolygon::create(vert2,6,1.0f)));    //    Point vert3[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,1.0f)));    //    Point vert4[4] = {Point(-67.50000,Point(-55.00000,172.50000),Point(-54.00000,77.00000)};    //    shengzibody->addShape(PhysiCSShapepolygon::create(vert4,4,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,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 PhysicsEditor 加载plist PEShapeCache_X3_0全部内容,希望文章能够帮你解决cocos2d-x 3.0 PhysicsEditor 加载plist PEShapeCache_X3_0所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1065384.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存