粒子算法:烟花、喷泉、爆炸

参考:https://www.codeproject.com/articles/10003/a-basic-particles-system

如果需要实现一个喷泉粒子系统:

1)需要先定义每个粒子元素Particle类:它应包含有运动方向和速度,初始化位置(及运动一个生命值后的位置),生命值,粒子渲染的颜色,及一个生命值增加一个单位后对应的粒子属性变化。

Vector.cs--存储粒子位置信息

  1 /// 
  2     /// 向量
  3     /// 
  4     public class Vector
  5     {
  6         #region Private members
  7         /// 
  8         /// X Coorination of the vector
  9         /// 
 10         private float m_Xcoord;
 11         /// 
 12         /// Y Coorination of the vector
 13         /// 
 14         private float m_Ycoord;
 15         /// 
 16         /// Z Coorination of the vector
 17         /// 
 18         private float m_Zcoord;
 19         #endregion
 20 
 21         #region Constructors
 22         /// 
 23         /// Default constructor. Initiate vector at the (0,0,0) location
 24         /// 
 25         public Vector()
 26         { }
 27 
 28         /// 
 29         /// Initiate vector with given parameters
 30         /// 
 31         /// X coordination of vector
 32         /// Y coordination of vector
 33         /// Z coordination of vector
 34         public Vector(float inX, float inY, float inZ)
 35         {
 36             m_Xcoord = inX;
 37             m_Ycoord = inY;
 38             m_Zcoord = inZ;
 39         }
 40 
 41         /// 
 42         /// Initiate vector with given parameters
 43         /// 
 44         /// Vector's coordinations as an array
 45         public Vector(float[] coordination)
 46         {
 47             m_Xcoord = coordination[0];
 48             m_Ycoord = coordination[1];
 49             m_Zcoord = coordination[2];
 50         }
 51 
 52         /// 
 53         /// Initiate vector with same values as given Vector
 54         /// 
 55         /// Vector to copy coordinations
 56         public Vector(Vector vector)
 57         {
 58             m_Xcoord = vector.X;
 59             m_Ycoord = vector.Y;
 60             m_Zcoord = vector.Z;
 61         }
 62         #endregion
 63 
 64         #region Public properties
 65         /// 
 66         /// X Coordination of vector
 67         /// 
 68         public float X
 69         {
 70             get { return m_Xcoord; }
 71             set { m_Xcoord = value; }
 72         }
 73         /// 
 74         /// Y Coordination of vector
 75         /// 
 76         public float Y
 77         {
 78             get { return m_Ycoord; }
 79             set { m_Ycoord = value; }
 80         }
 81         /// 
 82         /// Z Coordination of vector
 83         /// 
 84         public float Z
 85         {
 86             get { return m_Zcoord; }
 87             set { m_Zcoord = value; }
 88         }
 89         #endregion
 90 
 91         #region Methods
 92 
 93         /// 
 94         /// Add 2 vectors and create a new one.
 95         /// 
 96         /// First vector
 97         /// Second vector
 98         /// New vector that is the sum of the 2 vectors
 99         public static Vector Add(Vector vector1, Vector vector2)
100         {
101             if (((Object)vector1 == null) || ((Object)vector2 == null))
102                 return null;
103             return new Vector(vector1.X + vector2.X, vector1.Y + vector2.Y, vector1.Z + vector2.Z);
104         }
105         /// 
106         /// Substract 2 vectors and create a new one.
107         /// 
108         /// First vector
109         /// Second vector
110         /// New vector that is the difference of the 2 vectors
111         public static Vector Subtract(Vector vector1, Vector vector2)
112         {
113             if (((Object)vector1 == null) || ((Object)vector2 == null))
114                 return null;
115             return new Vector(vector1.X - vector2.X, vector1.Y - vector2.Y, vector1.Z - vector2.Z);
116         }
117         /// 
118         /// Return a new vector with negative values.
119         /// 
120         /// Original vector
121         /// New vector that is the inversion of the original vector
122         public static Vector Neg(Vector vector)
123         {
124             if ((Object)vector == null)
125                 return null;
126             return new Vector(-vector.X, -vector.Y, -vector.Z);
127         }
128         /// 
129         /// Multiply a vector with a scalar
130         /// 
131         /// Vector to be multiplied
132         /// Scalar to multiply vector
133         /// New vector that is the multiplication of the vector with the scalar
134         public static Vector Multiply(Vector vector, float val)
135         {
136             if ((Object)vector == null)
137                 return null;
138             return new Vector(vector.X * val, vector.Y * val, vector.Z * val);
139         }
140         #endregion
141 
142         #region Operators
143 
144         /// 
145         /// Check equality of two vectors
146         /// 
147         /// First vector
148         /// Second vector
149         /// True - if he 2 vectors are equal.
150         /// False - otherwise
151         public static bool operator ==(Vector vector1, Vector vector2)
152         {
153             if (((Object)vector1 == null) || ((Object)vector2 == null))
154                 return false;
155             return ((vector1.X.Equals(vector2.X))
156                 && (vector1.Y.Equals(vector2.Y))
157                 && (vector1.Z.Equals(vector2.Z)));
158         }
159 
160         /// 
161         /// Check inequality of two vectors
162         /// 
163         /// First vector
164         /// Second vector
165         /// True - if he 2 vectors are not equal.
166         /// False - otherwise
167         public static bool operator !=(Vector vector1, Vector vector2)
168         {
169             if (((Object)vector1 == null) || ((Object)vector2 == null))
170                 return false;
171             return ((!vector1.X.Equals(vector2.X))
172                 && (!vector1.Y.Equals(vector2.Y))
173                 && (!vector1.Z.Equals(vector2.Z)));
174         }
175 
176         /// 
177         /// Calculate the sum of 2 vectors.
178         /// 
179         /// First vector
180         /// Second vector
181         /// New vector that is the sum of the 2 vectors
182         public static Vector operator +(Vector vector1, Vector vector2)
183         {
184             if (((Object)vector1 == null) || ((Object)vector2 == null))
185                 return null;
186             return Vector.Add(vector1, vector2);
187         }
188         /// 
189         /// Calculate the substraction of 2 vectors
190         /// 
191         /// First vector
192         /// Second vector
193         /// New vector that is the difference of the 2 vectors
194         public static Vector operator -(Vector vector1, Vector vector2)
195         {
196             if (((Object)vector1 == null) || ((Object)vector2 == null))
197                 return null;
198             return Vector.Subtract(vector1, vector2);
199         }
200         /// 
201         /// Calculate the negative (inverted) vector
202         /// 
203         /// Original vector
204         /// New vector that is the invertion of the original vector
205         public static Vector operator -(Vector vector)
206         {
207             if ((Object)vector == null)
208                 return null;
209             return Vector.Neg(vector);
210         }
211         /// 
212         /// Calculate the multiplication of a vector with a scalar
213         /// 
214         /// Vector to be multiplied
215         /// Scalar to multiply vector
216         /// New vector that is the multiplication of the vector and the scalar
217         public static Vector operator *(Vector vector, float val)
218         {
219             if ((Object)vector == null)
220                 return null;
221             return Vector.Multiply(vector, val);
222         }
223         /// 
224         /// Calculate the multiplication of a vector with a scalar
225         /// 
226         /// Scalar to multiply vecto
227         /// Vector to be multiplied
228         /// New vector that is the multiplication of the vector and the scalar
229         public static Vector operator *(float val, Vector vector)
230         {
231             if ((Object)vector == null)
232                 return null;
233             return Vector.Multiply(vector, val);
234         }
235 
236         #endregion
237 
238         #region Constants
239         /// 
240         /// Standard (0,0,0) vector
241         /// 
242         public static Vector Zero
243         {
244             get { return new Vector(0.0f, 0.0f, 0.0f); }
245         }
246         /// 
247         /// Standard (1,0,0) vector
248         /// 
249         public static Vector XAxis
250         {
251             get { return new Vector(1.0f, 0.0f, 0.0f); }
252         }
253         /// 
254         /// Standard (0,1,0) vector
255         /// 
256         public static Vector YAxis
257         {
258             get { return new Vector(0.0f, 1.0f, 0.0f); }
259         }
260         /// 
261         /// Standard (0,0,1) vector
262         /// 
263         public static Vector ZAxis
264         {
265             get { return new Vector(0.0f, 0.0f, 1.0f); }
266         }
267         #endregion
268 
269         #region Overides
270         public override bool Equals(object obj)
271         {
272             Vector vector = obj as Vector;
273             if ((Object)vector != null)
274                 return (m_Xcoord.Equals(vector.X))
275                     && (m_Ycoord.Equals(vector.Y))
276                     && (m_Zcoord.Equals(vector.Z));
277             return false;
278         }
279 
280         public override string ToString()
281         {
282             return string.Format(CultureInfo.InvariantCulture, "({0}, {1}, {2})", m_Xcoord, m_Ycoord, m_Zcoord);
283         }
284         public override int GetHashCode()
285         {
286             return m_Xcoord.GetHashCode() ^ m_Ycoord.GetHashCode() ^ m_Zcoord.GetHashCode();
287         }
288         #endregion
289     }
View Code

Particle.cs---粒子属性类

 1     /// 
 2     /// 粒子
 3     /// 
 4     public class Particle
 5     {
 6         /// 
 7         /// 当前粒子所在的位置信息
 8         /// 
 9         private Vector position;
10         /// 
11         /// 当前粒子运行的方向和速度
12         /// 
13         private Vector velocity;
14         /// 
15         /// 当前离子的生命值
16         /// 
17         private int life;
18         /// 
19         /// 当前离子渲染的颜色
20         /// 
21         private Color color;
22 
23         public Particle() : this(Vector.Zero, Vector.Zero, Color.Black, 0) { }
24 
25         public Particle(Vector position, Vector velocity, Color color, int life)
26         {
27             this.position = position;
28             this.velocity = velocity;
29             this.color = color;
30             if (life > 0)
31             {
32                 this.life = life;
33             }
34         }
35 
36         public virtual void Update()
37         {
38             // Update particle's movement according to environment
39             this.velocity = this.velocity - NativeEnvironment.GetInstance().Gravity
40                                     + NativeEnvironment.GetInstance().Wind;
41             // Update particle's position according to movement
42             this.position = this.position + this.velocity;
43             // Update particle's age
44             this.life++;
45         }
46 
47         /// 
48         /// 获取 当前粒子的位置信息
49         /// 
50         public Vector Position
51         {
52             get { return position; }
53         }
54 
55         /// 
56         /// 获取 当前粒子的运行方向和速度
57         /// 
58         public Vector Velocity
59         {
60             get { return velocity; }
61         }
62 
63         /// 
64         /// 获取 当前粒子的生命值
65         /// 
66         public int Life
67         {
68             get { return life; }
69         }
70 
71         /// 
72         /// 获取 当前离子渲染的颜色
73         /// 
74         public Color Color
75         {
76             get { return color; }
77         }
78     }

2)需要考虑自然环境因素NativeEnvironment,并设定以下两个属性:重力、风速。

NativeEnvironment.cs--自然环境因素

 1     /// 
 2     /// 自然环境条件信息
 3     /// 
 4     public class NativeEnvironment
 5     {
 6         /// 
 7         /// 重力
 8         /// 
 9         private Vector gravity = Vector.Zero;
10         /// 
11         /// 风速
12         /// 
13         private Vector wind = Vector.Zero;
14 
15         /// 
16         /// 单例实体对象
17         /// 
18         private static NativeEnvironment instance = null;
19 
20         private NativeEnvironment() { }
21 
22         /// 
23         /// 获取 自然条件信息对象实例
24         /// 
25         public static NativeEnvironment GetInstance()
26         {
27             if (instance == null)
28             {
29                 instance = new NativeEnvironment();
30             }
31             return instance;
32         }
33 
34         /// 
35         /// 获取或设置 重力
36         /// 
37         public Vector Gravity
38         {
39             get { return gravity; }
40             set { gravity = value; }
41         }
42 
43         /// 
44         /// 获取或设置 风速
45         /// 
46         public Vector Wind
47         {
48             get { return wind; }
49             set { wind = value; }
50         }
51     }

3)定义一个喷泉粒子系统.类,它应该包含以下属性:存储当前粒子系统中所有粒子的集合容器、初始化系统时所有粒子的起始位置、最大生命值、粒子渲染的颜色、最大粒子个数,还应该包含以下几个方法:新增粒子,一个生命值单位变化后批量修改所有粒子的方法。

Particles.cs --- 粒子系统抽象类

 1     /// 
 2     /// 粒子系统
 3     /// 
 4     public abstract class Particles
 5     {
 6         /// 
 7         /// 当前粒子系统内所有的粒子元素存储集合
 8         /// 
 9         protected List particles = new List();
10         /// 
11         /// 粒子系统的中心位置
12         /// 
13         protected Vector position;
14         /// 
15         /// 默认粒子的最大生命值
16         /// 
17         protected int maxLife = 150;
18         /// 
19         /// 默认粒子的渲染颜色
20         /// 
21         protected Color color;
22 
23         /// 
24         /// 生成(新增)粒子到粒子系统中
25         /// 
26         /// 
27         protected abstract Particle Create();
28 
29         /// 
30         /// 修改粒子系统中所有粒子的状态位置等信息
31         /// 
32         /// 
33         public abstract bool Update();
34 
35         public virtual void Draw(Graphics g)
36         {
37             Pen pen;
38             int intense;
39             Particle part;
40 
41             for (int i = 0; i < this.particles.Count; i++)
42             {
43                 part = this[i];
44                 // Calculate particle intensity
45                 intense = (int)((float)part.Life / this.MaxLife);
46                 // Generate pen for the particle
47                 pen = new Pen(Color.FromArgb(intense * this.color.R,
48                                              intense * this.color.G,
49                                              intense * this.color.B));
50                 // Draw particle
51                 g.DrawEllipse(pen, part.Position.X, part.Position.Y,
52                     Math.Max(1, 4 * part.Life / this.MaxLife),
53                     Math.Max(1, 4 * part.Life / this.MaxLife));
54                 pen.Dispose();
55             }
56         }
57 
58         /// 
59         /// 根据index访问系统中某个粒子对象
60         /// 
61         /// 
62         /// 
63         public Particle this[int index]
64         {
65             get { return (Particle)this.particles[index]; }
66         }
67 
68         /// 
69         /// 返回系统中所有粒子个数
70         /// 
71         public int Count
72         {
73             get { return this.particles.Count; }
74         }
75 
76         /// 
77         /// 返回系统中粒子最大生命值
78         /// 
79         public int MaxLife
80         {
81             get { return this.maxLife; }
82         }
83 
84     }

FountainParticlesSystem.cs---喷泉粒子系统定义

  1     public class FountainParticlesSystem : Particles
  2     {
  3         private static readonly int DEFAULT_NUM_PARTICLES = 500;
  4 
  5         // Random numbers generator
  6         private Random m_rand = new Random();
  7 
  8         /// 
  9         /// Default constructor
 10         /// 
 11         public FountainParticlesSystem()
 12             : this(Vector.Zero, Color.Black)
 13         { }
 14 
 15         /// 
 16         /// Constructor
 17         /// 
 18         /// Starting position of system
 19         public FountainParticlesSystem(Vector pos)
 20             : this(pos, Color.Black)
 21         { }
 22 
 23         /// 
 24         /// Constructor
 25         /// 
 26         /// Starting position of system
 27         /// Color of the particles in the system
 28         public FountainParticlesSystem(Vector pos, Color col)
 29         {
 30             // Set system's position at given position
 31             this.position = pos;
 32             // Set system color to given color
 33             this.color = col;
 34             // Create ONLY 5 particles
 35             for (int i = 0; i < 5; i++)
 36             {
 37                 // Create particle, and add it to the list of particles
 38                 this.particles.Add(Create());
 39             }
 40         }
 41 
 42         /// 
 43         /// Generate a single particle in the system.
 44         /// This function is used when particles are first created, and when they are regenerated
 45         /// 
 46         /// New particle
 47         protected override Particle Create()
 48         {
 49             // 生成随机方向和速度的新粒子
 50             // 在一个喷泉,粒子移动几乎直
 51             float rndX = 2.0f * ((float)m_rand.NextDouble() - 0.4f);
 52             float rndY = -1.0f - 1 * (float)m_rand.NextDouble();
 53             float rndZ = 1.5f * ((float)m_rand.NextDouble() - 0.4f);
 54 
 55             // Create new particle at system's starting position
 56             Particle part = new Particle(this.position,
 57                 // With generated direction and speed
 58                 new Vector(rndX, rndY, rndZ), this.color,
 59                 // And a random starting life
 60                 m_rand.Next(50));
 61 
 62             // Return newly created particle
 63             return part;
 64         }
 65 
 66         /// 
 67         /// Update all the particles in the system
 68         /// 
 69         /// False - if there are no more particles in system
 70         /// True - otherwise
 71         public override bool Update()
 72         {
 73             Particle part;
 74             int count = this.Count;
 75 
 76             // For each particle
 77             for (int i = 0; i < count; i++)
 78             {
 79                 // Get particle from list
 80                 part = (Particle)this.particles[i];
 81                 // Update particle and check age
 82                 part.Update();
 83 
 84                 /*part.Life > this.maxLife && */
 85                 if (part.Position.Y >= this.position.Y)
 86                 {
 87                     // Remove old particles
 88                     this.particles.RemoveAt(i);
 89                     // Update counter and index
 90                     i--;
 91                     count = this.particles.Count;
 92                 }
 93             }
 94 
 95             // If there aren't enough particles
 96             if (this.particles.Count < DEFAULT_NUM_PARTICLES)
 97             {
 98                 // Add another particles
 99                 this.particles.Add(Create());
100             }
101 
102             // Always return true, since system is regenerating
103             return true;
104         }
105     }

4)设定粒子一个生命值增长需要耗费的时间、一个用来渲染粒子系统当前生命时刻的画布。

 1     public partial class Main : Form
 2     {
 3         private static readonly Vector MIDDLE_OF_VIEW = new Vector(250, 250, 250);
 4         private Particles ps = null;
 5 
 6         public Main()
 7         {
 8             InitializeComponent();
 9 
10             this.timer.Interval = 20;//ms
11 
12             NativeEnvironment.GetInstance().Gravity = new Vector(0.0f, -0.02f, 0.0f);
13             NativeEnvironment.GetInstance().Wind = new Vector(0.0f, 0.0f, 0.0f);
14 
15             if (NativeEnvironment.GetInstance().Gravity == NativeEnvironment.GetInstance().Wind)
16                 return;
17         }
18 
19         private void btnStart_Click(object sender, EventArgs e)
20         {
21             ps = new FountainParticlesSystem(MIDDLE_OF_VIEW, Color.FromArgb(0, 0, 255));
22             this.timer.Enabled = true;
23         }
24 
25         private void timer_Tick(object sender, System.EventArgs e)
26         {
27             if (!ps.Update())
28             {
29                 picDisplay.Refresh();
30                 ps = null;
31                 this.timer.Enabled = false;
32             }
33             else
34             {
35                 picDisplay.Refresh();
36             }
37         }
38 
39         private void picDisplay_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
40         {
41             if (ps == null)
42                 return;
43             ps.Draw(e.Graphics);
44         }
45     }

效果:

粒子算法:烟花、喷泉、爆炸_第1张图片

代码下载位置:http://pan.baidu.com/s/1gfaODF1

 

你可能感兴趣的:(粒子算法:烟花、喷泉、爆炸)