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);
	}

 

效果图:

在线运行

源码下载

posted on 2013-04-18 10:38  韩细  阅读(912)  评论(0编辑  收藏  举报