基於cocos2d-x的含有物理屬性的物件創建模板(優化版)

//
//  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();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章