Cocos2d-x 3.x中自带物理引擎使用教程

分类栏目:cocos教程

156

一、Cocos2d-x 3.x物理引擎使用介绍
游戏的物理引擎是非常重要的,在Cocos2d_x 2.x我们可以使用其所提供的库,在Cocos2d_x 3.x中,除了其所提供的第三方库,我们还可以使用其内置引擎,极大地方便了我们的学习和使用。
我们可以在项目的.cpp文件init()的return true上方添加代码检测.
代码如下:

setPhysicsBody(cocos2d:: PhysicsBody *body);
return true;

二、Cocos2d-x 3.x中使用物理引擎创建有物理特性的scene
无需多言,直接上代码:

 // 'scene' is an autorelease object
     auto scene = Scene::createWithPhysics();
     scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

三、Cocos2d-x 3.x中使用物理引擎创建边界
我们给这个方法取个名字:addEdge();在.h文件中添加此方法的声明。

void addEdge();

然后再.cpp文件中对它进行定义。

void HelloWorld::addEdge()
{

    auto body = PhysicsBody::createEdgeBox(size,PHYSICSBODY_MATERIAL_DEFAULT,3);//加入屏幕尺寸
    auto edgeshape = Node::create();
    edgeshape->setPhysicsBody(body);
    edgeshape->setPosition(size.width/2,size.height/2);
    addChild(edgeshape);//添加物理边界
}

这里的尺寸size,我在init()中已经定义了,并且在.h文件中已经声明了。

private:
    cocos2d:: LabelTTF *label;
    cocos2d::Size size;

size = Director::getInstance()->getVisibleSize();//获取屏幕大小
return true;

四、Cocos2d-x 3.x中使用物理引擎创建物理元素
即在显示框中显现出一个小球,小球的位置我们事先已定义好,如要随意位置,则在(五、Cocos2d-x 3.x中使用物理引擎动态添加元素(点击事件的响应))会介绍。这个方法我们称它为addBall();
代码如下:
.h文件中:

void addBall(float positiionX,float positionY );

.cpp文件中:

void HelloWorld::addBall(float positionX,float positionY)
{
    auto b = Sprite::create("ball.png");

        b->setPhysicsBody(PhysicsBody::createBox(b->getContentSize()));
        b->setPosition( positionX, positionY);
        addChild(b);

}

![运行结果]

Cocos2d-x 3.x中自带物理引擎使用教程

五、Cocos2d-x 3.x中使用物理引擎动态添加元素(点击事件的响应)
这个过程的原理很简单,就是事件的响应,把点击的坐标传到addBall()中即可。
头文件:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
private:
    cocos2d:: LabelTTF *label;
    cocos2d::Size size;
public:
    static cocos2d::Scene* createScene();

    virtual bool init();
    virtual void onEnter();
    void addEdge();
    void addBall(float positiionX,float positionY );
    void addBall(cocos2d::Vec2 position);
    void menuCloseCallback(cocos2d::Ref* pSender);



    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__


.cpp文件

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::createWithPhysics();
    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////
    // 1. super init first
    if (!Layer::init())
    {
        return false;
    }
    size = Director::getInstance()->getVisibleSize();//获取屏幕大小
    return true;
};
/*void HelloWorld::update(float dt)
{
    label->setPosition(label->getPosition() + Point(1, 1));

void HelloWorld::timehander(float dt)
{
    log("未曾相逢先一笑,初识便已许平生");
}*/
void HelloWorld::onEnter() {

    Layer::onEnter();
    addEdge();
    addBall(size.width / 2, size.height / 2);
    auto Listener = EventListenerTouchOneByOne ::create ();
    Listener->onTouchBegan = [this](Touch * touch,Event *event) {
        this->addBall(touch->getLocation());
        return false;


    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(Listener, this);
}

void HelloWorld::addEdge()
{

    auto body = PhysicsBody::createEdgeBox(size,PHYSICSBODY_MATERIAL_DEFAULT,3);//加入屏幕尺寸
    auto edgeshape = Node::create();
    edgeshape->setPhysicsBody(body);
    edgeshape->setPosition(size.width/2,size.height/2);
    addChild(edgeshape);//添加物理边界
}
void HelloWorld::addBall(float positionX,float positionY)
{
    auto b = Sprite::create("ball.png");

        b->setPhysicsBody(PhysicsBody::createBox(b->getContentSize()));
        b->setPosition( positionX, positionY);
        addChild(b);

}
void HelloWorld::addBall(cocos2d::Vec2 position)
{
    addBall(position.x,position.y);
}
void HelloWorld::menuCloseCallback(Ref* pSender)

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

在下才疏学浅,感谢大家阅读!