资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

cocos2d-x开发中有关粒子系统的碰撞检测及可能性应用分析-创新互联

游戏开发中,普通的碰撞检测就简单了,这主要是借助于精灵类的boundingBox矩形间是否相交来判定。但试想,如果在一个游戏中存在多种粒子武器,这两种武器互相朝对方开火,那么也应当存在一个粒子***相交(即碰撞)的问题吧。这时候如何检测呢?

创新互联-专业网站定制、快速模板网站建设、高性价比万安网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式万安网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖万安地区。费用合理售后完善,10年实体公司更值得信赖。

今天在整理COCOS2D-X粒子系统支持时发现了这样的问题,而且碰到一个函数updateQuadWithParticle。这个函数在基类CCParticleSystem中定义如下:

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

而在上述基类CCParticleSystem的子类CCParticleSystemQuad中有如下实现代码:

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;                
    }
}

上述函数咱就不深入分析了,因为涉及到许多的OpenGL ES概念。但是,从名义上可以看出,其作用是使用当前粒子相关信息来更新quad数据(这个quad与三维场景下基本图元有关,一般在渲染三维物体时使用三角形图元及四边形图元等表示方法)。而我们通过把自己的武器类继承自CCParticleSystemQuad类,并重载这个函数,就可以取得这个粒子数据(当然,对于一个粒子系统来说,不止是一枚粒子了)。然后,通过类似于引文中的方法,如下:

void TTMyParticleWeapon::updateQuadWithParticle( tCCParticle* particle, const CCPoint& newPosition )  
{  
    CCParticleSystemQuad::updateQuadWithParticle(particle, newPosition);  
    if (!this->isVisible()) return ;  
    CCPoint pos = this->convertToWorldSpace(particle->pos);  
    /// 碰撞检测 。。。。。  
}

来实现自己的粒子武器之间的碰撞检测了!

参考文章:

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:cocos2d-x开发中有关粒子系统的碰撞检测及可能性应用分析-创新互联
分享地址:http://cdkjz.cn/article/pspoi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220