javafx粒子系统之烟雾模拟
javafx粒子系统之烟雾模拟
功能说明: 用图片纹理模拟烟雾生成,烟雾可以随风飘散。
程序讲解:
1、粒子对象定义: 定义Particle对象继承Parent。 定义以下属性:
//烟雾图片 Image image; //x坐标 DoubleProperty x = new SimpleDoubleProperty(); //y坐标 DoubleProperty y = new SimpleDoubleProperty(); //粒子半径 double raidus; //水平速度 double vx; //垂直速度 double vy; //加速度 DoubleProperty acc = new SimpleDoubleProperty(); //粒子存在时长 DoubleProperty timer = new SimpleDoubleProperty();
2、粒子创建 用图片纹理创建烟雾
private void create() {
ImageView view = new ImageView(image);
view.xProperty().bind(this.x);
view.yProperty().bind(this.y);
view.opacityProperty().bind(this.timer.divide(100));
getChildren().add(view);
}
3、粒子创死亡判断 timer属性的值判断粒子是否死亡。
public boolean isDead() {
return timer.get() < 0;
}
4、粒子更新 粒子运动方向根据acc的大小来改变。
private void update() {
Particle p = new Particle(image, 84, 164,
0.3 * random.nextGaussian(),
0.3 * random.nextGaussian() - 1, 100);
p.acc.bind(acc);
particles.add(p);
Iterator it = particles.iterator();
while (it.hasNext()) {
Particle tempParticle = it.next();
tempParticle.update();
if (tempParticle.isDead()) {
it.remove();
}
}
float fps = com.sun.javafx.perf.PerformanceTracker.getSceneTracker(
stage.getScene()).getInstantFPS();
fpsLable.setText("FPS:" + fps);
}
效果图:

浙公网安备 33010602011771号