此项目用cocos2d-x 3.2 编写,极其简单
(我也想做有难度点的,可是能力还不足0.0)~~
首先创建一个名为Ball的项目。。。
修改HelloWorldScene.h文件为PhysicsWorld.h:
#ifndef __PHYSICSWORLD_H__
#define __PHYSICSWORLD_H__
#include "cocos2d.h"
USING_NS_CC;
class Physicsworld : public Layer
{
public:
static Scene* createScene();
virtual bool init();
CREATE_FUNC(Physicsworld);
private:
Size winSize;
};
#endif
HelloWorldScene.cpp改为PhysicsWorld.cpp,首先创建一个物理世界(其实就是把窗口设置成盒子,
盒子里有重力,里面空心,就像小黑屋-@_@-)
#include "PhysicsWorld.h"
Scene* Physicsworld::createScene()
{
//物理场景
auto scene = Scene::createWithPhysics();
//重力设置为水平向下
Vect gravity(0, -10.0f);
scene->getPhysicsWorld()->setGravity(gravity);
//开启测试模式
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
Size winSize = Director::getInstance()->getWinSize();
//创建边界,为空心盒子刚体,参数分别为刚体大小,材质,边线厚度
auto body = PhysicsBody::createEdgeBox(Size(winSize.width,
winSize.height), PHYSICSBODY_MATERIAL_DEFAULT, 2);
//创建一个节点, 用于承载刚体
auto node = Node::create();
node->setPosition(Point(winSize.width/2, winSize.height/2));
node->setPhysicsBody(body);
scene->addChild(node);
auto layer = Physicsworld::create();
scene->addChild(layer, 1);
return scene;
}
bool Physicsworld::init()
{
if ( !Layer::init() )
{
return false;
}
return true;
}
其实很简单啦,我懂的都写上注释了,没注释的都是很简单的
(其实是我不知道怎么注释,不要揭穿我(>_<)~~)
最后别忘了在AppDelegate.h中修改
#include "PhysicsWorld.h"
和
auto scene = Physicsworld::createScene();
// run
director->runWithScene(scene);
好了,物理世界创建好了,看看效果图:
嗯~~ 白色的背景是因为我在AppDelegate.h中添加了
glClearColor(1.0, 1.0, 1.0, 1.0);
对了,这个物理世界是借鉴了笨木头前辈的
http://www.benmutou.com/blog/archives/804
3578



被折叠的 条评论
为什么被折叠?



