cocos2d-x 不规则图形碰撞检测
2014-01-13 18:24
260 查看
原创文章,转载请注明出处!
在游戏中,有时会用到判断是否点击到一张图的有色部分,即非透明区域; 或者是判断不规则图片的碰撞的判断。
其中,p_point是相对 Sprite锚点的偏移。
记录以备忘!
在游戏中,有时会用到判断是否点击到一张图的有色部分,即非透明区域; 或者是判断不规则图片的碰撞的判断。
//判断有没有点到有材质的部分, p_point相对, CCSprite坐标 bool Tools::isClickTheRealSprite(cocos2d::CCSprite* p_sprite, const cocos2d::CCPoint p_point){ CCSize l_sizeInPixel = p_sprite->getContentSize(); //得到相对,Sprite左下的偏移 int l_iX = (int)(l_sizeInPixel.width/2.0f + p_point.x); int l_iY = (int)(l_sizeInPixel.height/2.0f + (int)p_point.y); CCPoint l_pointBL = ccp(l_iX, l_iY); CCRect l_rect = CCRect(0, 0, l_sizeInPixel.width, l_sizeInPixel.height); if(!(l_rect.containsPoint(l_pointBL))){ return false; } //开辟出空内存buffer //unsigned char* l_buffer = (unsigned char*)malloc(l_sizeInPixel.width * l_sizeInPixel.height * 4); //memset(l_buffer, 0, l_sizeInPixel.width * l_sizeInPixel.height * 4); uint8_t l_buffer[4]; //写内存 CCSize size = p_sprite->getParent()->getContentSize(); CCRenderTexture* l_renderTexture = CCRenderTexture::create(l_sizeInPixel.width, l_sizeInPixel.height, kCCTexture2DPixelFormat_RGBA8888); //l_renderTexture->begin(); l_renderTexture->beginWithClear(0, 0, 0, 0); p_sprite->draw(); glReadPixels(l_pointBL.x, l_pointBL.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, l_buffer); l_renderTexture->end(); //透明度 //int l_iValue_A = l_buffer[4*(int)(l_pointBL.y*l_sizeInPixel.width + l_pointBL.x)+3]; int l_iValue_A = l_buffer[3]; //free(l_buffer); if(l_iValue_A < 5 ){ return false; } return true; }
其中,p_point是相对 Sprite锚点的偏移。
记录以备忘!
相关文章推荐
- Box2d_无规则图形的碰撞检测 和 cocos2d_粒子效果制作以及粒子效果的辅助工具
- (转)cocos2d-x 不规则图形碰撞检测(像素级碰撞检测)
- cocos2d-x 不规则碰撞检测 【转载】
- 【AS3代码】精确到点的不规则图形碰撞检测
- 【cocos2d-x-3.2】【高仿微信打飞机系列二】【敌机 碰撞检测 爆炸管理】
- cocos2d-x 菜鸟学习笔记十三(简单的碰撞检测)
- cocos2d碰撞检测
- cocos2d-x游戏开发系列教程-坦克大战游戏之子弹的碰撞检测处理
- cocos2d-x for xna 基于地图的碰撞检测和信息提示
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测
- (译)碰撞检测和收集物品:如何使用cocos2d制作基于tiled地图的游戏:第二部分
- cocos2d-x-3.3-020-碰撞检测2-物理引擎初探
- 分享一位国外大牛写的不规则物体像素级碰撞检测算法及详解
- cocos2d-js基础 本地数据存储 碰撞检测几种方法
- quick-cocos2d-x lua中碰撞检测的实现
- cocos2d AABB碰撞检测
- cocos2d-x的碰撞检测
- Cocos2d-x Tiled地图编辑器(二)精灵走动起来、碰撞检测
- 不规则图形碰撞检测
- 不规则图形的碰撞检测