Bullet(Cocos2dx)之使用cocos2dx测试PhysicsWorld3D

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库(win32release库复制到

Cocos\frameworks\cocos2d-x\prebuilt\win32下分别更名为

BulletCollision_vs2012.lib

BulletDynamics_vs2012.lib

BulletSoftBody_vs2012.lib

LinearMath_vs2012.lib

如何得到Bullet库,这个就要去看Bullet3Hello Worldvs2012补充如何生成Bullet

5.打开刚才生成的项目的属性,选择链接器->输入->附加依赖项,输入生成的Bullet库,如图


将PhysicsWorld3D类复制到classes/physics3d并添加到工程中,编译运行,成功表明配置成功,

有可能会出现error LNK2038: 检测到“RuntimeLibrary”的不匹配项MTd_StaticDebug不匹配值“MDd_DynamicDebug”

Bullet生成时的c/c++->代码生成->运行库,改为MD

 

5.测试PhysicsWorld3D

HelloWorldScene.h添加

#include "physics3d/PhysicsWorld3D.h"
public:
	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从天而降,重重的摔在地上


源代码及其资源下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos2d-x 是一个开源的跨平台游戏开发框架,支持 C++、Lua 和 JavaScript 等语言。以下是一个简单的飞机大战游戏的制作流程: 1. 创建一个新的 Cocos2d-x 项目,在命令行中使用以下命令: ``` cocos new MyGame -p com.your_company.mygame -l cpp -d /path/to/your/project ``` 其中,`MyGame` 是项目的名称,`com.your_company.mygame` 是项目的包名,`/path/to/your/project` 是项目的路径。 2. 在 `Classes` 文件夹下创建游戏场景类和游戏层类。游戏场景类负责管理游戏层,游戏层类负责绘制游戏界面和处理用户输入事件。可以参考以下代码: ```c++ class GameScene : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(GameScene); }; class GameLayer : public cocos2d::Layer { public: virtual bool init(); void update(float delta); CREATE_FUNC(GameLayer); }; ``` 3. 在游戏层中添加背景图和玩家飞机。可以使用 `Sprite` 类来加载图片资源,并使用 `addChild()` 方法将其添加到层中。例如: ```c++ auto background = Sprite::create("background.png"); background->setPosition(visibleSize.width / 2, visibleSize.height / 2); addChild(background); player = Sprite::create("player.png"); player->setPosition(visibleSize.width / 2, player->getContentSize().height / 2); addChild(player); ``` 4. 实现玩家飞机的移动和射击功能。可以使用 `EventKeyboard` 类来监听键盘事件,并在 `update()` 方法中更新玩家飞机的位置和子弹的位置。例如: ```c++ void GameLayer::update(float delta) { // 移动玩家飞机 if (isKeyPressed(EventKeyboard::KeyCode::KEY_UP_ARROW)) { player->setPositionY(player->getPositionY() + 5.0f); } if (isKeyPressed(EventKeyboard::KeyCode::KEY_DOWN_ARROW)) { player->setPositionY(player->getPositionY() - 5.0f); } if (isKeyPressed(EventKeyboard::KeyCode::KEY_LEFT_ARROW)) { player->setPositionX(player->getPositionX() - 5.0f); } if (isKeyPressed(EventKeyboard::KeyCode::KEY_RIGHT_ARROW)) { player->setPositionX(player->getPositionX() + 5.0f); } // 发射子弹 if (isKeyPressed(EventKeyboard::KeyCode::KEY_SPACE)) { auto bullet = Sprite::create("bullet.png"); bullet->setPosition(player->getPositionX(), player->getPositionY() + player->getContentSize().height / 2); addChild(bullet); auto moveBy = MoveBy::create(1.0f, Vec2(0, visibleSize.height)); auto removeSelf = RemoveSelf::create(); auto sequence = Sequence::create(moveBy, removeSelf, nullptr); bullet->runAction(sequence); } } ``` 5. 添加敌机和碰撞检测功能。可以使用定时器来定期生成敌机,并使用 `Rect` 类来判断玩家飞机和子弹是否与敌机发生碰撞。例如: ```c++ void GameLayer::addEnemy(float delta) { auto enemy = Sprite::create("enemy.png"); float x = CCRANDOM_0_1() * visibleSize.width; enemy->setPosition(x, visibleSize.height + enemy->getContentSize().height / 2); addChild(enemy); auto moveBy = MoveBy::create(2.0f, Vec2(0, -visibleSize.height - enemy->getContentSize().height)); auto removeSelf = RemoveSelf::create(); auto sequence = Sequence::create(moveBy, removeSelf, nullptr); enemy->runAction(sequence); } void GameLayer::checkCollision() { for (auto enemy : enemies) { if (player->getBoundingBox().intersectsRect(enemy->getBoundingBox())) { // 碰撞处理 } for (auto bullet : bullets) { if (bullet->getBoundingBox().intersectsRect(enemy->getBoundingBox())) { // 碰撞处理 } } } } ``` 6. 最后,在游戏场景类的 `init()` 方法中添加游戏层、定时器和碰撞检测函数。例如: ```c++ bool GameScene::init() { if (!Scene::init()) { return false; } auto gameLayer = GameLayer::create(); addChild(gameLayer); schedule(schedule_selector(GameLayer::addEnemy), 1.0f); scheduleUpdate(); return true; } void GameLayer::update(float delta) { checkCollision(); } ``` 以上是一个简单的飞机大战游戏的制作流程,具体的实现细节还需要结合实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值