支持PhysicsEditor生成的关于box2d的pilst文件的解析文件 ,支持cocos2d-x-2.1.5 亲测通过

Box2D刚体绑定精灵 cocos2d-x-3.2版本Box2D刚体绑定精灵示例 立即下载

GB2ShapeCache_x.h

#ifndef __CCSHAPECACHE_H__
#define __CCSHAPECACHE_H__
#include "cocos2d.h"
USING_NS_CC;
class BodyDef;
class b2Body;
namespace cocos2d {
    class GB2ShapeCache : public CCObject{
    public:
        static GB2ShapeCache* sharedGB2ShapeCache(void);
    public:
        bool init();
        void addShapesWithFile(const std::string &plist);
        void addFixturesToBody(b2Body *body, const std::string &shape);
        cocos2d::CCPoint anchorPointForShape(const std::string &shape);
        void reset();
        float getPtmRatio() { return ptmRatio; }
        ~GB2ShapeCache() {}
    private:
        std::map<std::string, BodyDef *> shapeObjects;
        GB2ShapeCache(void) {}
        float ptmRatio;
    };
}
#endif /* defined(__CCSHAPECACHE_H__) */

GB2ShapeCache_x.cpp
#include "GB2ShapeCache_x.h"
#include "Box2D/Box2D.h"
#include "..\cocoa\CCNS.h"


using namespace cocos2d;


/**
 * Internal class to hold the fixtures
 */
class FixtureDef {
public:
    FixtureDef()
    : next(NULL) {}
    
    ~FixtureDef() {
        delete next;
        delete fixture.shape;
    }
    
    FixtureDef *next;
    b2FixtureDef fixture;
    int callbackData;
};


class BodyDef {
public:
	BodyDef()
	: fixtures(NULL) {}
	
	~BodyDef() {
		if (fixtures)
			delete fixtures;
	}
	
	FixtureDef *fixtures;
	CCPoint anchorPoint;
};


static GB2ShapeCache *_sharedGB2ShapeCache = NULL;


GB2ShapeCache* GB2ShapeCache::sharedGB2ShapeCache(void) {
	if (!_sharedGB2ShapeCache) {
		_sharedGB2ShapeCache = new GB2ShapeCache();
        _sharedGB2ShapeCache->init();
	}
	
	return _sharedGB2ShapeCache;
}


bool GB2ShapeCache::init() {
	return true;
}


void GB2ShapeCache::reset() {
	std::map<std::string, BodyDef *>::iterator iter;
	for (iter = shapeObjects.begin() ; iter != shapeObjects.end() ; ++iter) {
		delete iter->second;
	}
	shapeObjects.clear();
}


void GB2ShapeCache::addFixturesToBody(b2Body *body, const std::string &shape) {
	std::map<std::string, BodyDef *>::iterator pos = shapeObjects.find(shape);
	assert(pos != shapeObjects.end());
	
	BodyDef *so = (*pos).second;


	FixtureDef *fix = so->fixtures;
    while (fix) {
        body->CreateFixture(&fix->fixture);
        fix = fix->next;
    }
}


cocos2d::CCPoint GB2ShapeCache::anchorPointForShape(const std::string &shape) {
	std::map<std::string, BodyDef *>::iterator pos = shapeObjects.find(shape);
	assert(pos != shapeObjects.end());
	
	BodyDef *bd = (*pos).second;
	return bd->anchorPoint;
}


void GB2ShapeCache::addShapesWithFile(const std::string &plist) {


	CCDictionary *dict = CCDictionary::createWithContentsOfFileThreadSafe(plist.c_str());
	CCAssert(dict != NULL, "Shape-file not found"); // not triggered - cocos2dx delivers empty dict if non was found
    CCAssert(dict->count() != 0, "plist file empty or not existing");
	
	CCDictionary *metadataDict = (CCDictionary *)dict->objectForKey("metadata");
    int format = static_cast<CCString *>(metadataDict->objectForKey("format"))->intValue();
    ptmRatio = static_cast<CCString *>(metadataDict->objectForKey("ptm_ratio"))->floatValue();
	CCAssert(format == 1, "Format not supported");


	CCDictionary *bodyDict = (CCDictionary *)dict->objectForKey("bodies");


    b2Vec2 vertices[b2_maxPolygonVertices];


	CCDictElement* pElement = NULL;
	CCDICT_FOREACH(bodyDict, pElement)
	{
		BodyDef *bodyDef = new BodyDef();


		CCString *bodyName = ccs(pElement->getStrKey());


		CCDictionary *bodyData = (CCDictionary *)pElement->getObject();
		bodyDef->anchorPoint = CCPointFromString(static_cast<CCString *>(bodyData->objectForKey("anchorpoint"))->getCString());
		
		CCArray *fixtureList = (CCArray *)bodyData->objectForKey("fixtures");
		FixtureDef **nextFixtureDef = &(bodyDef->fixtures);
		
		CCObject *fixture = NULL;
		CCARRAY_FOREACH(fixtureList, fixture)
		{
		
			b2FixtureDef basicData;
			CCDictionary *fixtureData = (CCDictionary *)fixture;
			int callbackData = 0;


			basicData.filter.categoryBits = static_cast<CCString *>(fixtureData->objectForKey("filter_categoryBits"))->intValue();
            basicData.filter.maskBits = static_cast<CCString *>(fixtureData->objectForKey("filter_maskBits"))->intValue();
            basicData.filter.groupIndex = static_cast<CCString *>(fixtureData->objectForKey("filter_groupIndex"))->intValue();
            basicData.friction = static_cast<CCString *>(fixtureData->objectForKey("friction"))->floatValue();
            basicData.density = static_cast<CCString *>(fixtureData->objectForKey("density"))->floatValue();
            basicData.restitution = static_cast<CCString *>(fixtureData->objectForKey("restitution"))->floatValue();
            basicData.isSensor = (bool)static_cast<CCString *>(fixtureData->objectForKey("isSensor"))->intValue();
			if(fixtureData->objectForKey("id")){
				basicData.userData = static_cast<CCString *>(fixtureData->objectForKey("id"));
				callbackData = static_cast<CCString *>(fixtureData->objectForKey("id"))->intValue();
			}


			std::string fixtureType = static_cast<CCString *>(fixtureData->objectForKey("fixture_type"))->getCString();
			//CCString *fixtureType = static_cast<CCString *>(fixtureData->objectForKey("fixture_type"))->getCString();


			if (fixtureType == "POLYGON") {
				//CCDictionary *polygons = (CCDictionary *)fixtureData->objectForKey("polygons");
				CCArray *polygons = (CCArray *)fixtureData->objectForKey("polygons");
				//CCDictElement *polygon = NULL;
				CCObject *polygon = NULL;
				//CCDICT_FOREACH(polygons, polygon)
				CCARRAY_FOREACH(polygons, polygon)
				{
					FixtureDef *fix = new FixtureDef();
					fix->fixture = basicData; // copy basic data
					fix->callbackData = callbackData;


					b2PolygonShape *polyshape = new b2PolygonShape();
                    int vindex = 0;


					//CCDictionary *polygonData = (CCDictionary *)polygon->getObject();
					CCArray *polygonData = (CCArray *)polygon;


					assert(polygonData->count() <= b2_maxPolygonVertices);


					//CCDictElement *offset = NULL;
					CCObject *offset = NULL;
					//CCDICT_FOREACH(polygonData, offset)
					CCARRAY_FOREACH(polygonData, offset)
					{


						CCString *pStr = (CCString *)offset;
						CCPoint p = CCPointFromString(pStr->getCString());


						vertices[vindex].x = (p.x / ptmRatio) ; 
                        vertices[vindex].y = (p.y / ptmRatio) ; 
                        vindex++;


					}


					polyshape->Set(vertices, vindex);
                    fix->fixture.shape = polyshape;


					*nextFixtureDef = fix;
                    nextFixtureDef = &(fix->next);
				}
				
			} else if (fixtureType == "CIRCLE") {
				FixtureDef *fix = new FixtureDef();
                fix->fixture = basicData; // copy basic data
                fix->callbackData = callbackData;
                
				CCDictionary *circleData = (CCDictionary *)fixtureData->objectForKey("circle");


                b2CircleShape *circleShape = new b2CircleShape();
				
                circleShape->m_radius = static_cast<CCString *>(circleData->objectForKey("radius"))->floatValue() / ptmRatio;
				CCPoint p = CCPointFromString(static_cast<CCString *>(circleData->objectForKey("position"))->getCString());
                circleShape->m_p = b2Vec2(p.x / ptmRatio, p.y / ptmRatio);
                fix->fixture.shape = circleShape;
				
                // create a list
                *nextFixtureDef = fix;
                nextFixtureDef = &(fix->next);


			} else {
				CCAssert(0, "Unknown fixtureType");
			}
			
			// add the body element to the hash
			shapeObjects[bodyName->getCString()] = bodyDef;


		}


	}


}


GB2ShapeCache-x 对应cocos2dx 3.x版本创建接口 官方提供了Cocos2dx的第三方库,但是采用的是2.x的接口,3.x下面依然是可以使用的,不过在这添加了采用3.x接口封装的方法 立即下载

相关推荐

GB2ShapeCache-x

使用coco2d-x的Box2d物理引擎时,用PhysicsEditor编辑器可以方便的导出需要的物体Fixture框架、Shape形状等;因为cocos2d-x更新太快原来的GB2ShapeCache-x无法解析出导出的plist了。这个版本的 GB2ShapeCache-x,亲测,适用于cocos2d-x2.2.4。

用于解析PhysicsEditor生成的plist文件 ,支持cocos2d-x-2.1.5 亲测通过

GB2ShapeCache_x.h #ifndef __CCSHAPECACHE_H__ #define __CCSHAPECACHE_H__ #include "cocos2d.h" USING_NS_CC; class BodyDef; class b2Body; namespace cocos2d { class GB2ShapeCache : public CCObject{

abc519469030的专栏 1272

GB2ShapeCache-x.cpp

cocos2dx 2.x PhysicsEditor 使用GB2ShapeCache-x.cpp

【Cocos2d-x】PhysicsEditor的使用

下载与安装PhysicsEditor 物理编辑器介绍 1.VertexHelper 免费、开源的物理编辑器,只能在Mac平台下使用。 2.PhysicsEditor 收费,支持windows与mac。可免费使用,有功能限制。 这里使用的是PhysicsEditor。 PhysicsEditor下载地址: 下载地址一:https://www.codeandweb.com/physicseditor 下载地址二:http://pan.baidu.com/s/1mgp8eHa 在PhysicsEdito

3193

拯救者R7000P(2021)装双系统Ubuntu20.04 解决联发科MT7921无线网卡在Ubuntu中不能联网

拯救者R7000P(2021)装双系统Ubuntu20.04/Ubuntu18.04 电脑配置:联想拯救者R7000P CPU:AMD R7-5800H GPU:RTX3060-6G 内存:16GB 固态:500G(原装)+500(加装) 1.做系统盘(本文以Ubuntu20.04为例) 1.下载Ubuntu20.04镜像 直接去Ubuntu官网或者一些国内的镜像去下载都可以,这里给出一个网址https://ubuntu.com/download/desktop,根据需要下载自己学要的Ubuntu版本,后缀

qq_41108639的博客 1万+

Eplan2022 复制已有的宏文件生成新的原理图宏文件

Eplan2022 新建原理图宏文件

haier123888的博客 1万+

cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody

一、环境: cocos2d-x 3.0 PhysicsEditor 1.0.10 二、cocos2d-x 3.0 篇: 从cocos2d-x 3.0 可以在scene 中直接添加物理世界后,开发物理效果的2d游戏相对轻松多了。可是编辑多边形的body 就成了问题,之前-x 2.x 版的PE loader 没有目前 CCPhysicsBody 的封装。本人就参考 之前的...

modabobo 279

什么是PON(无源光纤网络)、PON的发展及演进

全球知名媒体Omdia在光纤和铜缆接入设备预测报告中对2022年2月的预测显示,PON设备端口收入预计在2020年至2027年期间以12.3%的年复合增长率增长,到2027年将达到163亿美元,远高于2020年的82亿美元。 此前,Omdia有份报告称,在3月份举行的美国OFC展会中,众多 CSP 正在顺利部署 XGS PON(10G 对称),其他 CSP 专注于基于 GPON 的低速 FTTH 服务。另外关于25G PON 和 50G PON 之间的争论仍在继续,而Omdia 认为 25G PON 和 5

4931

cocos2d-x 2.14 运行 PhysicsEditor Demo(更新GB2ShapeCache-x)

由于PhysicsEditor1.09不支持cocos2d-x2.14,运行Demo报错,所以上网找资料,发现千篇一律的垃圾代码. 最后终于找到一个能用的也只支持cocos2d-2-1.0-x-2.0.4. 遂修改之,以下是GB2ShapeCache-x的代码.   // // HelloWorldScene.h // Demo // // Created by Andrea

菘淏的专栏 886

SAP-PP MD04详解一

MD04是SAP运行MRP后的结果查询事务码,其他功能强大,标准功能比如:查询/更改物料主数据、转换计划订单到生产订单/采购申请、查询/更改各个MRP元素对应的单据等等。而且还可以附加标准的菜单或者自定义添加事务码上去。老铁将做一个MD04详细说明系列,本篇为第一篇之MRP元素说明。 我们来看看MD04基本界面 可以看到主要的数据显示界面,有日期、MRP元素、MRP元素数据、再计划日期、收货/需求、可用数量、工厂、库存地点等栏位。光标定位到MRP元素列,按F1即可看到各类MRP元素的说...

weixin_52158549的博客 1万+

【深度学习】肺结节分割项目实战一:处理数据集

一、LIDC-IDRI数据集简介 官方网站 肺图像数据库协会的图像收集(LIDC-IDRI)包括诊断和带有病变注释标记的肺癌筛查胸部CT。这是一个网络公开的国际资源,用于肺癌检测和诊断的计算机辅助诊断(CAD)方法的开发、培训和评估。 数据集包含1018个病例,每个病例包括来自临床胸部CT扫描的图像和一个相关的XML文件,该文件记录了由四名经验丰富的胸部放射科医生进行的两阶段图像注释过程的结果。在最初的盲读阶段,每个放射科医生独立审查每个CT扫描,并标记出属于三种类型之一的病变(“结节>= 3mm”,

weixin_42182906的博客 1万+

cocos2d-x+box2d+PhysicsEditor+win7强大的box2d工具

用到的工具:PhysicsEditor 首先我们要达到的效果图: 在游戏当中加入了物理世界的话会让你的游戏更真实更好玩,所以就让我们去用一款强大的软件轻松做画吧 打开软件 在box2d中多边形最多能由8个顶点,所以要构造一个复杂的花瓶的话,我们就采用多个多边形拼接在一起。下一步我们点publish As得到plist文件,放入cocos...

weixin_30525825的博客 126

GVINS数据采集(u-blox fp9 + D435i)

1、配置千寻的RTK账号 采集数据当天购买千寻的RTK账号,然后在Window下,输入账号和密码 (具体的步骤如之前面连接所示),查看u-center软件上的信息,确保接收机可以收到RTK信息; 2、查看Linux系统下u-blox是否可以正常接受GPS信号和RTK数据流 1、GPS信号信息 // 打开端口 1 cd ublox_f9p/src // 或者在终端1下也可以 source devel/setup.bash r.....................

努力努力努力 2609

让 cocos2d-x 2.X 版本能使用 physicsEditor (修改文件 GB2ShapeCache-x.cpp)

原文链接:http://blog.csdn.net/zhangxaochen/article/details/8070773 附件1: vs2010+cocos2d-2.0-x-2.0.2 工程下载 附件2: 单独 GB2ShapeCache-x.cpp 文件下载 physicsEditor 用来制作 box2d 的 fixture 实在是太方便了,软件的具体使用方法参考: h

zhangxaochen的专栏 2661

华为OD机试新系统真题【终端设备档位差异统计】

华为OD机试 新系统真题【终端设备档位差异统计】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【终端设备档位差异统计】多语言题解。

qq_45776114的博客 790

小梅哥Xilinx ZYNQ学习笔记2——38译码器

译码器(Decoder)是一种多输入多输出的组合逻辑电路,负责将二进制代码翻译为特定的对象(如逻辑电平等),功能与编码器相反。译码器一般分为通用译码器和数字显示译码器两大类。本设计的是通用译码器。三八译码器,即是 3 种输入状态翻译成 8 种输出状态。1.case开始开始,endcase结束;2.数字声明时,合法的基数格式有 4 中,包括:十进制('d 或 'D),十六进制('h 或 'H),二进制('b 或 'B),八进制('o 或 'O)。数值可指明位宽,也可不指明位宽。

m0_51430584的博客 1636

如何使用 PhysicsEditor 加快 cocos2dx-Box2D 开发

附件: vs2010工程下载 原文链接:http://blog.csdn.net/zhangxaochen/article/details/8018489 看这里: http://is.gd/VoBVUJ PhysicsEditor 是一款很好的box2d 形状生成工具,通过图形界面,点点鼠标,生成形状的plist文件,然后使用他的api生成fixtures加到需要的body中去。 软

zhangxaochen的专栏 1万+

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

python使用PIL模块获取图片像素点

from PIL import Image########获取图片指定像素点的像素def getPngPix(pngPath = "aa.png",pixelX = 1,pixelY = 1):    img_src = Image.open(pngPath)    img_src = img_src.convert('RGBA')    str_strlist = img_src.load() ...

caobin0825的博客 1万+

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

python PIL.Image获取图像像素时, 图像的原点及x,y的方向

是以图像左下角为原点 x方向为从左往右 y方向为从下往上 参见: 深入浅出python机器学习_8.3_神经网络实例_手写识别_MNIST数据集的使用 https://blog.csdn.net/Dontla/article/details/99708055 ...

Dontla的博客 7084

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

flv文件元信息(metadata)

flv 元信息(metadata) flv文件中的元信息,是一些描述flv文件各类属性的信息。这些信息以AMF格式保存在文件的起始部分。adobe官方的标准flv元信息项目如下(来源:http://help.adobe.com/en_US/FlashMediaLiveEncoder/3.0/Using/WS5b3ccc516d4fbf351e63e3d11c104ba878-7ff5.htm

Zicheng's tech blog 8609

plist文件生成与读取

废话不多说,这里简明扼要的介绍一下plist文件的生成与读取1、创建plist文件 2、代码读取plist文件 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@”myPropertyList” ofType:@”plist”]; //读取出来的可以是数组,也可以是字典 //读取出来的字典

huoshes的博客 1123

cocos2d-x plist文件读取

plist文件为: texture width 1024 height 1024 frames main_bg_c@2x.png id 1002 x 1 y 1 width 640 height 760 offsetX 0 offsetY 0 origina

yihaiyiren的专栏 2967

【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法

上一节讲述了粒子的相关问题,当然啦,不示弱,今天继续将物理系统给大家进行简单的介绍和讲述;        首先先介绍,如何在cocos2d中加入box2d开发lib包,因为一般使用cocos2d引擎进行开发游戏时,大家创建项目都会选用cocos2d框架,而不是直接采用物理系统的cocos2d框架,但是后期忽然需要在项目中使用物理系统(这种情况很经常发生,至于为什么,童鞋们都懂得~),OK,首

wanglang3081的专栏 859

python 利用xml文件中bndbox坐标截图并保存

import cv2 import numpy as np import xml.dom.minidom import os import argparse img_path = 'images/' anno_path = 'annotations/' cut_path = 'cut/' imagelist = os.listdir(img_path) for image in imag...

cunyizhang的博客 2253

cocos2dx-深度解析plist文件(一)(游戏对象的数据如何从plist创建获取)

cocos2dx的精灵缓存在创建一组精灵帧,加载瓦片地图,普通动画的创建、骨骼动画等等都会通过plist(parameter list)文件获得需要的信息,建立器游戏中需要的类对象。本文从CCSpriteFrameCache读取plist创建精灵帧研究起。其中代码使用到了tinyXML2第三方库,以及SAX(simple api xml)。然后在从精灵帧创建精灵反向研究,plist文件数据的含义。

追求游戏之道 1万+
上一篇: cocos2d-x使用TexturePacker打包图片及使用生成的plist文件创建精灵动画
下一篇: 关于CCControlSwitch设置在弹窗上后的触摸优先级问题
Handz_cpp
博客等级 码龄14年 6粉丝 6原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值