游戏开发中,普通的碰撞检测就简单了,这主要是借助于精灵类的boundingBox矩形间是否相交来判定。但试想,如果在一个游戏中存在多种粒子武器,这两种武器互相朝对方开火,那么也应当存在一个粒子***相交(即碰撞)的问题吧。这时候如何检测呢?
今天在整理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
本文出自 “青峰” 博客,请务必保留此出处http://zhuxianzhong.blog.51cto.com/157061/1551635
cocos2d-x开发中有关粒子系统的碰撞检测及可能性应用分析
原文地址:http://zhuxianzhong.blog.51cto.com/157061/1551635