(转)cocos2d-x 不规则图形碰撞检测(像素级碰撞检测)
2014-05-29 14:10
423 查看
原文出处:http://www.2cto.com/kf/201401/272331.html
在游戏中,有时会用到判断是否点击到一张图的有色部分,即非透明区域; 或者是判断不规则图片的碰撞的判断。
在游戏中,有时会用到判断是否点击到一张图的有色部分,即非透明区域; 或者是判断不规则图片的碰撞的判断。
//判断有没有点到有材质的部分, 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; }
相关文章推荐
- cocos2d-x 不规则图形碰撞检测
- Box2d_无规则图形的碰撞检测 和 cocos2d_粒子效果制作以及粒子效果的辅助工具
- (转)像素完美碰撞检测(使用cocos2d-x)
- COCOS2D-X 像素级碰撞检测
- 【AS3代码】精确到点的不规则图形碰撞检测
- 分享一位国外大牛写的不规则物体像素级碰撞检测算法及详解
- cocos2d-x像素碰撞检测
- cocos2d-x 不规则碰撞检测 【转载】
- 像素完美碰撞检测(使用cocos2d-x)
- Cocos2d-x3.0游戏实例之《别救我》第七篇——物理世界的碰撞检测
- Cocos2d-X 游戏之碰撞检测的方法
- SpriteKit:开启物理逐像素(Per-Pixel)碰撞检测后发现的问题及解决
- 【cocos2d-x游戏开发】物体的碰撞检测
- cocos2d-xAABB碰撞检测
- Cocos2d-x 3.0中 物理碰撞检测中onContactBegin回调函数不响应问题
- [Cocos2d-x For WP8]矩形碰撞检测
- cocos2d-x3.x 像素碰撞
- (译)碰撞检测和收集物品:如何使用cocos2d制作基于tiled地图的游戏:第二部分
- 基于cocos2d-x的2D空间中的OBB(Orient Bounding Box)碰撞检测算法
- 超高效的不规则物体碰撞检测