您好,登錄后才能下訂單哦!
游戲開發中,普通的碰撞檢測就簡單了,這主要是借助于精靈類的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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。