Cocos2dx 像素判断碰撞检测

本文详细介绍了如何在Cocos2d-x游戏引擎中进行像素级别的碰撞检测,通过实例代码解析了关键步骤,包括获取精灵的像素数据、比较颜色差异以及判断碰撞发生,确保精确的碰撞响应。
摘要由CSDN通过智能技术生成

最近研究了一下透明图片的点击事件即png图片中透明区域的点击。当时用j2me的时候,记得设置个值就可以处理是像素点击还是图片点击,不对,是碰撞检测。看了看jdk,它处理了像素,处理如下:
  1. public void handlesinglepixel(int x, int y, int pixel) {
  2. int alpha = (pixel >> 24) & 0xff;
  3. int red = (pixel >> 16) & 0xff;
  4. int green = (pixel >> 8) & 0xff;
  5. int blue = (pixel ) & 0xff;
  6. // Deal with the pixel as necessary...
  7. }
复制代码



这时候使用的像素格式是ARGB。
  1. bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int width, unsigned int height)

  2. {

  3. unsigned char* tempData = image->getData();

  4. this->setFDImageData(tempData);

  5. unsigned int* inPixel32 = NULL;

  6. unsigned char* inPixel8 = NULL;

  7. unsigned short* outPixel16 = NULL;

  8. bool hasAlpha = image->hasAlpha();

  9. CCSize imageSize = CCSizeMake((float)(image->getWidth()), (float)(image->getHeight()));

  10. CCTexture2DPixelFormat pixelFormat;

  11. size_t bpp = image->getBitsPerComponent();

  12. // compute pixel format

  13. if(hasAlpha)

  14. {

  15. pixelFormat = g_defaultAlphaPixelFormat;

  16. }

  17. else

  18. {

  19. if (bpp >= 8)

  20. {

  21. pixelFormat = kCCTexture2DPixelFormat_RGB888;

  22. }

  23. else

  24. {

  25. pixelFormat = kCCTexture2DPixelFormat_RGB565;

  26. }

  27. }

  28. // Repack the pixel data into the right format

  29. unsigned int length = width * height;

  30. if (pixelFormat == kCCTexture2DPixelFormat_RGB565)

  31. {

  32. if (hasAlpha)

  33. {

  34. // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"

  35. tempData = new unsigned char[width * height * 2];

  36. outPixel16 = (unsigned short*)tempData;

  37. inPixel32 = (unsigned int*)image->getData();

  38. for(unsigned int i = 0; i < length; ++i, ++inPixel32) { *outPixel16++ = ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R ((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) | // G ((((*inPixel32 >> 16) & 0xFF) >> 3) << 0); // B } } else { // Convert "RRRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB" tempData = new unsigned char[width * height * 2]; outPixel16 = (unsigned short*)tempData; inPixel8 = (unsigned char*)image->getData();

  39. for(unsigned int i = 0; i < length; ++i) { *outPixel16++ = (((*inPixel8++ & 0xFF) >> 3) << 11) | // R (((*inPixel8++ & 0xFF) >> 2) << 5) | // G (((*inPixel8++ & 0xFF) >> 3) << 0); // B } } } else if (pixelFormat == kCCTexture2DPixelFormat_RGBA4444) { // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA" inPixel32 = (unsigned int*)image->getData();

  40. tempData = new unsigned char[width * height * 2];

  41. outPixel16 = (unsigned short*)tempData;

  42. for(unsigned int i = 0; i < length; ++i, ++inPixel32) { *outPixel16++ = ((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) | // R ((((*inPixel32 >> 8) & 0xFF) >> 4) << 8) | // G ((((*inPixel32 >> 16) & 0xFF) >> 4) << 4) | // B ((((*inPixel32 >> 24) & 0xFF) >> 4) << 0); // A } } else if (pixelFormat == kCCTexture2DPixelFormat_RGB5A1) { // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGBBBBBA" inPixel32 = (unsigned int*)image->getData();

  43. tempData = new unsigned char[width * height * 2];

  44. outPixel16 = (unsigned short*)tempData;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值