基于cocos2d-x的含有物理属性的物件创建模板(优化版)
2016-02-19 17:48
337 查看
// // PhysicalWorld.hpp // gameProjectAlpha_01 // // Created by 胡博豪 on 16/2/1. // // #ifndef PhysicalWorld_hpp #define PhysicalWorld_hpp #include "cocos2d.h" #include "ui/CocosGUI.h" #include "external/Box2D/Box2D.h" #define BODY_TYPE_DYNAM b2_dynamicBody #define BODY_TYPE_STA b2_staticBody #define BODY_TYPE_KINE b2_kinematicBody class PhysicalWorld : publicb2ContactListener { protected: ~PhysicalWorld(); cocos2d::Layer *usedSceneClass; b2World *world; float ratio_mToPixels; std::map<std::string, b2Body *> bodys; float dt; int velocityIterations; int positionIterations; public: void createPhysicalWorld(cocos2d::Layer *this_ptr,float gravity_X = 0, float gravity_Y =9.8, float ratio_mToPix =1, float dt =1.0 / 60,int velocityIterations =8, int positionIterations =3); void addRect(std::string name,b2BodyType type, float px, float py,float width, float height,float linearVelocityX = 0, float linearVelocityY =0, cocos2d::Color3B color =cocos2d::Color3B(255,255, 255),float density = 1,float friction = 0.1, float restitution =0); void bodysUpdate()const; b2Body *getBodyByName(std::string name)const; cocos2d::Sprite *getSpriteByName(std::string name) const; }; #endif /* PhysicalWorld_hpp */ // // PhysicalWorld.cpp // gameProjectAlpha_01 // // Created by 胡博豪 on 16/2/1. // // #include "PhysicalWorld.hpp" void PhysicalWorld::createPhysicalWorld(cocos2d::Layer *this_ptr, float gravity_X,float gravity_Y, float ratio_mToPix, float dt,int velocityIterations, int positionIterations) { world =new b2World(b2Vec2(gravity_X, -gravity_Y)); world ->SetContactListener(this); ratio_mToPixels = ratio_mToPix; this ->dt = dt; this ->velocityIterations = velocityIterations; this ->positionIterations = positionIterations; usedSceneClass = this_ptr; } PhysicalWorld::~PhysicalWorld() { deleteworld; } void PhysicalWorld::addRect(std::string name, b2BodyType type, float px, float py,float width, float height,float linearVelocityX, float linearVelocityY, cocos2d::Color3B color,float density, float friction,float restitution) { auto def =new b2BodyDef; def -> type = type; def -> position =b2Vec2(px, py); auto rect =world -> CreateBody(def); rect -> SetLinearVelocity(b2Vec2(linearVelocityX, linearVelocityY)); bodys.insert(std::make_pair(name, rect)); delete def; def = nullptr; auto sprite =cocos2d::Sprite::create(); sprite -> setTextureRect(cocos2d::Rect(0,0, width * 2 *ratio_mToPixels, height *2 * ratio_mToPixels)); sprite -> setColor(color); sprite -> setPosition(cocos2d::Vec2(px *ratio_mToPixels, py * ratio_mToPixels)); usedSceneClass ->addChild(sprite); rect -> SetUserData(sprite); sprite -> setUserData(rect); auto rectShape =new b2PolygonShape; rectShape -> SetAsBox(width, height); auto fixDef =new b2FixtureDef; fixDef -> shape = rectShape; fixDef -> density = density; fixDef -> friction = friction; fixDef -> restitution = restitution; rect -> CreateFixture(fixDef); delete rectShape; rectShape = nullptr; delete fixDef; fixDef = nullptr; } void PhysicalWorld::bodysUpdate()const { usedSceneClass ->schedule([&, this](float dt) { for (std::pair<std::string,b2Body *> temp : bodys) { world ->Step(dt, velocityIterations, positionIterations); auto sp_temp = (cocos2d::Sprite *)temp.second -> GetUserData(); sp_temp -> setPosition(cocos2d::Vec2(temp.second -> GetPosition().x *ratio_mToPixels, temp.second ->GetPosition().y *ratio_mToPixels)); } }, "bodySchedule "); } b2Body *PhysicalWorld::getBodyByName(std::string name) const { returnbodys.find(name) ->second; } cocos2d::Sprite *PhysicalWorld::getSpriteByName(std::string name) const { return (cocos2d::Sprite *)bodys.find(name) -> second -> GetUserData(); }
相关文章推荐
- cocos2d-js关于对话框Layer的屏幕适配
- 2、创建第一个cocos2d-x项目
- quick-cocos2dx学习笔记(一)引擎结构总览和创建项目
- cocos2dx ——浅析cocos2dx3.2引擎目录
- Cocos2dx一步一步实现Android端热更新(C++)
- 第一章cocos2d-x
- quickcocos2dx scheduler妙用
- cocos2dx C++ 绑定到 Lua时两个问题
- cocos ide 调试lua 程序自动关闭问题的解决
- 使用BabeLua在cocos2d-x中调试Lua
- cocos2d-x 3.9 android studio项目命令行打包
- Cocos2d-x LayoutComponent的使用
- Cocos2d-x Layout使用三
- Cocos2d-x Layout的使用二
- Cocos2d-x Layout使用
- cocos2dx的runAction: 重复执行,多个动作连接执行,多个动作同时执行的实现(lua的应用)
- cocos2dx 3.x 之移植到android平台
- cocos2d 编译显示MSBuild3073
- 虚拟按键隐藏之后cocos游戏的适配问题
- Cocos2d-x 3.x 场景切换