参考:https://www.codeproject.com/articles/10003/a-basic-particles-system
如果需要实现一个喷泉粒子系统:
1)需要先定义每个粒子元素Particle类:它应包含有运动方向和速度,初始化位置(及运动一个生命值后的位置),生命值,粒子渲染的颜色,及一个生命值增加一个单位后对应的粒子属性变化。
Vector.cs--存储粒子位置信息
1 /// <summary> 2 /// 向量 3 /// </summary> 4 public class Vector 5 { 6 #region Private members 7 /// <summary> 8 /// X Coorination of the vector 9 /// </summary> 10 private float m_Xcoord; 11 /// <summary> 12 /// Y Coorination of the vector 13 /// </summary> 14 private float m_Ycoord; 15 /// <summary> 16 /// Z Coorination of the vector 17 /// </summary> 18 private float m_Zcoord; 19 #endregion 20 21 #region Constructors 22 /// <summary> 23 /// Default constructor. Initiate vector at the (0,0,0) location 24 /// </summary> 25 public Vector() 26 { } 27 28 /// <summary> 29 /// Initiate vector with given parameters 30 /// </summary> 31 /// <param name="inX">X coordination of vector</param> 32 /// <param name="inY">Y coordination of vector</param> 33 /// <param name="inZ">Z coordination of vector</param> 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 /// <summary> 42 /// Initiate vector with given parameters 43 /// </summary> 44 /// <param name="coordination">Vector's coordinations as an array</param> 45 public Vector(float[] coordination) 46 { 47 m_Xcoord = coordination[0]; 48 m_Ycoord = coordination[1]; 49 m_Zcoord = coordination[2]; 50 } 51 52 /// <summary> 53 /// Initiate vector with same values as given Vector 54 /// </summary> 55 /// <param name="v">Vector to copy coordinations</param> 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 /// <summary> 66 /// X Coordination of vector 67 /// </summary> 68 public float X 69 { 70 get { return m_Xcoord; } 71 set { m_Xcoord = value; } 72 } 73 /// <summary> 74 /// Y Coordination of vector 75 /// </summary> 76 public float Y 77 { 78 get { return m_Ycoord; } 79 set { m_Ycoord = value; } 80 } 81 /// <summary> 82 /// Z Coordination of vector 83 /// </summary> 84 public float Z 85 { 86 get { return m_Zcoord; } 87 set { m_Zcoord = value; } 88 } 89 #endregion 90 91 #region Methods 92 93 /// <summary> 94 /// Add 2 vectors and create a new one. 95 /// </summary> 96 /// <param name="vector1">First vector</param> 97 /// <param name="vector2">Second vector</param> 98 /// <returns>New vector that is the sum of the 2 vectors</returns> 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 /// <summary> 106 /// Substract 2 vectors and create a new one. 107 /// </summary> 108 /// <param name="vector1">First vector</param> 109 /// <param name="vector2">Second vector</param> 110 /// <returns>New vector that is the difference of the 2 vectors</returns> 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 /// <summary> 118 /// Return a new vector with negative values. 119 /// </summary> 120 /// <param name="v">Original vector</param> 121 /// <returns>New vector that is the inversion of the original vector</returns> 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 /// <summary> 129 /// Multiply a vector with a scalar 130 /// </summary> 131 /// <param name="vector">Vector to be multiplied</param> 132 /// <param name="val">Scalar to multiply vector</param> 133 /// <returns>New vector that is the multiplication of the vector with the scalar</returns> 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 /// <summary> 145 /// Check equality of two vectors 146 /// </summary> 147 /// <param name="vector1">First vector</param> 148 /// <param name="vector2">Second vector</param> 149 /// <returns>True - if he 2 vectors are equal. 150 /// False - otherwise</returns> 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 /// <summary> 161 /// Check inequality of two vectors 162 /// </summary> 163 /// <param name="vector1">First vector</param> 164 /// <param name="vector2">Second vector</param> 165 /// <returns>True - if he 2 vectors are not equal. 166 /// False - otherwise</returns> 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 /// <summary> 177 /// Calculate the sum of 2 vectors. 178 /// </summary> 179 /// <param name="vector1">First vector</param> 180 /// <param name="vector2">Second vector</param> 181 /// <returns>New vector that is the sum of the 2 vectors</returns> 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 /// <summary> 189 /// Calculate the substraction of 2 vectors 190 /// </summary> 191 /// <param name="vector1">First vector</param> 192 /// <param name="vector2">Second vector</param> 193 /// <returns>New vector that is the difference of the 2 vectors</returns> 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 /// <summary> 201 /// Calculate the negative (inverted) vector 202 /// </summary> 203 /// <param name="v">Original vector</param> 204 /// <returns>New vector that is the invertion of the original vector</returns> 205 public static Vector operator -(Vector vector) 206 { 207 if ((Object)vector == null) 208 return null; 209 return Vector.Neg(vector); 210 } 211 /// <summary> 212 /// Calculate the multiplication of a vector with a scalar 213 /// </summary> 214 /// <param name="vector">Vector to be multiplied</param> 215 /// <param name="val">Scalar to multiply vector</param> 216 /// <returns>New vector that is the multiplication of the vector and the scalar</returns> 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 /// <summary> 224 /// Calculate the multiplication of a vector with a scalar 225 /// </summary> 226 /// <param name="val">Scalar to multiply vecto</param> 227 /// <param name="vector">Vector to be multiplied</param> 228 /// <returns>New vector that is the multiplication of the vector and the scalar</returns> 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 /// <summary> 240 /// Standard (0,0,0) vector 241 /// </summary> 242 public static Vector Zero 243 { 244 get { return new Vector(0.0f, 0.0f, 0.0f); } 245 } 246 /// <summary> 247 /// Standard (1,0,0) vector 248 /// </summary> 249 public static Vector XAxis 250 { 251 get { return new Vector(1.0f, 0.0f, 0.0f); } 252 } 253 /// <summary> 254 /// Standard (0,1,0) vector 255 /// </summary> 256 public static Vector YAxis 257 { 258 get { return new Vector(0.0f, 1.0f, 0.0f); } 259 } 260 /// <summary> 261 /// Standard (0,0,1) vector 262 /// </summary> 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 }