1.使用 cocos 引擎建立工程,打开 cocosstudio 修改分辨率为 960*640 ,删掉背景图片 发布到vs 工程 1.打开 appdelegate 设置分辨率,并运行 director-getopenglview()-setdesignresolutionsize(960,640, resolutionpolicy :: exact_fit ); 2.配置bullet(win
1.使用cocos引擎建立工程,打开cocos studio 修改分辨率为960*640,删掉背景图片
发布到vs工程
1.打开appdelegate设置分辨率,并运行
director->getopenglview()->setdesignresolutionsize(960, 640, resolutionpolicy::exact_fit);
2.配置bullet(win32)到cocos2dx
首先要将bullet3-master的头文件复制到cocos\frameworks\cocos2d-x\external\bullet;具体方法请看
如下图bullet3之交叉编译android 复制头文件
4.将编译好的bullet库(win32)release库复制到
cocos\frameworks\cocos2d-x\prebuilt\win32下分别更名为
bulletcollision_vs2012.lib
bulletdynamics_vs2012.lib
bulletsoftbody_vs2012.lib
linearmath_vs2012.lib
如何得到bullet库,这个就要去看bullet3之hello world(vs2012)补充如何生成bullet库
5.打开刚才生成的项目的属性,选择链接器->输入->附加依赖项,输入生成的bullet库,如图
将physicsworld3d类复制到classes/physics3d并添加到工程中,编译运行,成功表明配置成功,
有可能会出现error lnk2038: 检测到“runtimelibrary”的不匹配项: 值“mtd_staticdebug”不匹配值“mdd_dynamicdebug”
将bullet生成时的c/c++->代码生成->运行库,改为md
5.测试physicsworld3d
在helloworldscene.h添加
#include physics3d/physicsworld3d.hpublic: void onexit(); void update(float delta);private: bool initphysics3d(); // 初始化物理世界 bool initcamera(); // 初始化摄像机private: physicsworld3d* _world; // 3d 物理世界 btrigidbody* _box; // 盒子 cocos2d::sprite3d* _spbox; // 盒子模型 cocos2d::camera* _camera; // 摄像机
在helloworld::init()
先初始化摄像机,再初始化物理世界,
关于初始化物理世界,首先创建,然后添加一个地面,再添加一个box作为测试用
bool helloworld::initphysics3d(){ _world = physicsworld3d::create(); // 创建3d物理世界 if (_world == nullptr) { return false; } // 载入plane模型 auto spplane = sprite3d::create(model/ground.c3b); this->addchild(spplane); spplane->setposition3d(vec3::zero); // add a plane 方向向上,位置(0,0,0), 0.5的摩擦,0.5的弹性 _world->addplane(btvector3(0, 1, 0), btvector3(0, 0, 0), physicsmaterial3d(0, 0.5, 0.5, 0)); // 载入盒子模型 _spbox = sprite3d::create(model/box.c3b); this->addchild(_spbox); _spbox->setposition3d(vec3(0, 20, 0)); // add a box _box = _world->addbox(btvector3(1, 1, 1), btvector3(0, 20, 0)); _box->setuserpointer(_spbox); // 设置2摄像机可见 this->setcameramask(2); return true;}
每一帧去更新物理世界,同时更新box的位置,当然可以使用motionstate去优化,这个不着急
void helloworld::update(float delta){ static float m[16]; _world->update(delta); auto trans = _box->getworldtransform(); // 获取box的变换矩阵 trans.getopenglmatrix(m); _spbox->setnodetoparenttransform(mat4(m)); // 设置box模型的变换矩阵,但是getposition3d不会得到正确位置,这个以后讨论}
当helloworld退出时要销毁物理世界
void helloworld::onexit(){ layer::onexit(); _world->destroy(); // 销毁物理世界 _world = nullptr;}
当运行程序,会看到一个box从天而降,重重的摔在地上
源代码及其资源下载