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

简介:

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


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

1
2
3
4
5
6
void  CCParticleSystem::updateQuadWithParticle(tCCParticle* particle,  const  CCPoint& newPosition)
{
     CC_UNUSED_PARAM(particle);
     CC_UNUSED_PARAM(newPosition);
     // should be overridden
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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类,并重载这个函数,就可以取得这个粒子数据(当然,对于一个粒子系统来说,不止是一枚粒子了)。然后,通过类似于引文中的方法,如下:

1
2
3
4
5
6
7
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






















本文转自朱先忠老师51CTO博客,原文链接:http://blog.51cto.com/zhuxianzhong/1551635 ,如需转载请自行联系原作者


相关文章
|
6月前
|
图形学
Unity射线检测的避坑指南
Unity射线检测的避坑指南
|
11月前
|
Java
手把手一步一步教你使用Java开发一个大型街机动作闯关类游戏05图像仿射变换(平移和缩放操作)
手把手一步一步教你使用Java开发一个大型街机动作闯关类游戏05图像仿射变换(平移和缩放操作)
111 0
|
11月前
|
Python
Python实现超级玛丽游戏系列教程04背景滚动及摄像机(Camera)原理
Python实现超级玛丽游戏系列教程04背景滚动及摄像机(Camera)原理
82 0
|
图形学 索引
unity 模拟抛物线
具体的步骤: 了解使用水平方向的速度是:v1=v0×cosθ竖直方向的速度是:v2=v0×sinθ-gty=v0×t-(gt^2)/2 克隆线段,坐标信息,线段的长度,线段的间距。检测碰撞的点、控制线段的显示和隐藏、绘制弧克隆子弹,计算子弹的发射速度,子弹的朝向点击空格,初始化子弹数据,子弹发射 在plyer下创建一个空物体为Arc;在代码中用创建一个空物体再添加组件LineRenderer,同时把这些空物体存到list中。
2122 0
|
图形学
Unity动画综合练习
实例:使用动画融合树实现原地不动,4个方向的走和跑的动画融合。通过代码控制角色控制器移动以及动画控制器播放动画 (1)首先创建一个Plane,一个Cube,给Plane用黄色的材质球,给Cube用红色的材质球,然后拖入一个模型到场景中,分别修改他们的Transform (2)创建一个动画控制器 (...
1378 0