需要注意的是,粒子系统和前面开发的物理小球之间有类似的地方,其都是通过数学方法和物理公式模拟客观世界中物体的运动轨迹。不同的是物理小球更强调个体运动,而焰火粒子系统更注重整体感觉,这点区别在代码中也能体现。
1、开发粒子对象Particle类:
1、开发粒子对象Particle类:
package xiao.fuyan.particle;
/**
* Created by xiao on 2017/2/10.
*/
public class Particle {
//粒子半径
int r;
//粒子颜色
int color;
//垂直速度
double v_v;
//水平速度
double h_v;
//初始坐标
int startX;
int startY;
//实时坐标
int x, y;
//起始时间
double startTime;
//构造器
public Particle(int color, int r, double v_v, double h_v, int x, int y, double startTime){
//初始化各个变量
this.color = color;
this.r = r;
this.v_v = v_v;
this.h_v = h_v;
this.startX = x;
this.startY = y;
this.x = x;
this.y = y;
this.startTime = startTime;
}
}
2、开发粒子集合ParticleSet类:
package xiao.fuyan.particle;
import android.graphics.Color;
import java.util.ArrayList;
/**
* Created by xiao on 2017/2/10.
*/
public class ParticleSet {
//用于存放Particle对象的集合
ArrayList<Particle> particleSet;
//构造器,
public ParticleSet(){
particleSet = new ArrayList<Particle>();
}
//向粒子集合中添加指定个数的粒子对象
public void add(int count, double startTime){
for(int i =0; i < count; i++){
//该段代码是开发焰火粒子
//获取粒子对象的颜色
int tempColor = this.getColor(i);
//设置粒子半径
int tempR = 3;
//随机产生粒子竖直方向上的速度
double tempv_v = -30 + 10 * (Math.random());
//同上获取水平方向上的速度
double temph_v = 10 - 20 * (Math.random());
//粒子的X坐标是固定的
int tempX = 160;
//随机产生Y坐标,90 - 100之间
int tempY = (int) (100 - 10 * (Math.random()));
Particle particle = new Particle(tempColor, tempR, tempv_v, temph_v, tempX, tempY, startTime);
//将创建好的Particle对象添加到列表中
particleSet.add(particle);
//下面代码是将焰火粒子变为瀑布粒子
//