Collision Detection of irregular cocos2d-x Images

Source: Internet
Author: User


Original article. For more information, see the source!

In the game, it is sometimes used to determine whether to click the colored part of a picture, that is, the non-transparent area; or to determine whether the collision of irregular images is judged.

// Determine whether there is any point to the part with materials. The point is relative to the point where the CCSprite coordinates bool Tools: isClickTheRealSprite (cocos2d: CCSprite * p_sprite, const cocos2d: CCPoint p_point) {CCSize l_sizeInPixel = p_sprite-> getContentSize (); // get the relative offset int l_iX = (int) (l_sizeInPixel.width/2.0f + p_point.x) in the lower left corner of Sprite; 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 (! (Maid (l_rect.containsPoint (l_pointBL) {return false;} // open out the empty memory 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]; // write memory CCSize size = p_sprite-> getParent ()-> getContentSize (); CCRenderTexture * l_renderTexture = CCRenderTexture: create (values, l_sizeInPixel.height, values); // 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 (); // transparency // 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 is the offset relative to the Sprite anchpoint.


Record for memo!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.