Collision Detection and possibility Application Analysis of Particle System in cocos2d-x Development

Source: Internet
Author: User

Collision Detection and Application of Particle System in cocos2d-x Development

In game development, normal collision detection is simple. This is mainly determined by the intersection of boundingbox rectangles of the genie class. But imagine that if there are multiple particle weapons in a game and the two weapons open fire at each other, there should also be a particle *** intersection (collision) problem. How can this problem be detected?


This problem was found today while sorting out support for the COCOS2D-X particle system, and encountered a function updatequadwithparticle. This function is defined in the base class ccparticlesystem as follows:

void CCParticleSystem::updateQuadWithParticle(tCCParticle* particle, const CCPoint& newPosition){    CC_UNUSED_PARAM(particle);    CC_UNUSED_PARAM(newPosition);    // should be overridden}

The following code is available in the ccparticlesystemquad subclass of the above base class ccparticlesystem:

void CCParticleSystemQuad::updateQuadWithParticle(tCCParticle* particle, const CCPoint& newPosition){    ccV3F_C4B_T2F_Quad *quad;    if (m_pBatchNode)    {        ccV3F_C4B_T2F_Quad *batchQuads = m_pBatchNode->getTextureAtlas()->getQuads();        quad = &(batchQuads[m_uAtlasIndex+particle->atlasIndex]);    }    else    {        quad = &(m_pQuads[m_uParticleIdx]);    }    ccColor4B color = (m_bOpacityModifyRGB)        ? ccc4( particle->color.r*particle->color.a*255, particle->color.g*particle->color.a*255, particle->color.b*particle->color.a*255, particle->color.a*255)        : ccc4( particle->color.r*255, particle->color.g*255, particle->color.b*255, particle->color.a*255);    quad->bl.colors = color;    quad->br.colors = color;    quad->tl.colors = color;    quad->tr.colors = color;    // vertices    GLfloat size_2 = particle->size/2;    if (particle->rotation)     {        GLfloat x1 = -size_2;        GLfloat y1 = -size_2;        GLfloat x2 = size_2;        GLfloat y2 = size_2;        GLfloat x = newPosition.x;        GLfloat y = newPosition.y;        GLfloat r = (GLfloat)-CC_DEGREES_TO_RADIANS(particle->rotation);        GLfloat cr = cosf(r);        GLfloat sr = sinf(r);        GLfloat ax = x1 * cr - y1 * sr + x;        GLfloat ay = x1 * sr + y1 * cr + y;        GLfloat bx = x2 * cr - y1 * sr + x;        GLfloat by = x2 * sr + y1 * cr + y;        GLfloat cx = x2 * cr - y2 * sr + x;        GLfloat cy = x2 * sr + y2 * cr + y;        GLfloat dx = x1 * cr - y2 * sr + x;        GLfloat dy = x1 * sr + y2 * cr + y;        // bottom-left        quad->bl.vertices.x = ax;        quad->bl.vertices.y = ay;        // bottom-right vertex:        quad->br.vertices.x = bx;        quad->br.vertices.y = by;        // top-left vertex:        quad->tl.vertices.x = dx;        quad->tl.vertices.y = dy;        // top-right vertex:        quad->tr.vertices.x = cx;        quad->tr.vertices.y = cy;    }     else     {        // bottom-left vertex:        quad->bl.vertices.x = newPosition.x - size_2;        quad->bl.vertices.y = newPosition.y - size_2;        // bottom-right vertex:        quad->br.vertices.x = newPosition.x + size_2;        quad->br.vertices.y = newPosition.y - size_2;        // top-left vertex:        quad->tl.vertices.x = newPosition.x - size_2;        quad->tl.vertices.y = newPosition.y + size_2;        // top-right vertex:        quad->tr.vertices.x = newPosition.x + size_2;        quad->tr.vertices.y = newPosition.y + size_2;                    }}

The above functions will not be further analyzed, because they involve many OpenGL ES concepts. However, it can be seen from the name that the function is to update quad data using the current particle information (This quad is related to the basic elements in 3D scenarios, generally, the triangle and quadrilateral elements are used to render a 3D object ). By inheriting our weapon class from the ccparticlesystemquad class and reloading this function, we can obtain the particle data (of course, for a particle system, more than one particle ). Then, use a method similar to that in the citation as follows:

Void ttmyparticipant leweapon: updatequadwithparticle (tccparticle * particle, const ccpoint & newposition) {ccparticlesystemquad: updatequadwithparticle (particle, newposition); If (! This-> isvisible () return; ccpoint Pos = This-> converttoworldspace (particle-> POS); // collision detection ..... }

To achieve collision detection between your own particle weapons!


References:

Http://blog.csdn.net/jebe7282/article/details/8486822

This article from the "Qingfeng" blog, please be sure to keep this source http://zhuxianzhong.blog.51cto.com/157061/1551635

Collision Detection and possibility Application Analysis of Particle System in cocos2d-x Development

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.