原始粒子系统 - 使用JavaScript 2D粒子相互作用

问题描述 投票:0回答:1

这只是我的复活的尝试。阅读以下更多。

我张贴这只是作为一个努力维护张贴在这里的新成员上周,这为他后来自愿自己删除一个有趣的问题;即使问题收到了一些向上票和星星在几个小时的事张贴后。

链接到原来的/现在删除的问题:https://stackoverflow.com/questions/54478107/2d-particle-interactions

我不是这方面的专家,此刻有点兴趣,但是,我很聪明,保存所有的代码和更高离线链接;)

原来的问题 - 重建


问题

我想模拟和可视化均受粒子相互作用:[image source]

PPS Pseudo Code

在给定的设置:

PPS = <R = 5,α= 180°,B = 17°,V = 0.67>

有效地复制以下观察:[image source]

PPS Generations

如显示在下面的视频:

How life emerges from a simple particle motion law

但创作者没有提供他们的源代码,因为他们说:

“你能不能发布代码吗?”

“我们把需要到科学报告文件,该文件是开放获取的一切。没有更多的了。我们曾经压缩完全运行模式的代码放到一个鸣叫,这是在那些日子里,当微博有140个字符,模型是超级简单,超级短。”

参考包括在这个问题开头的伪代码。

重要提示:原作者的代码工作没有复制在这里

我怎样才能使这项工作,因为它显示?


javascript particles
1个回答
0
投票

目前已经有几种解决方案/工作代码示例中的JavaScript PPS系统:

  1. 用户nagualdesign @ github上

https://github.com/nagualdesign/Primordial-Particle-System

年长/原始版本@谷歌驱动器:

https://drive.google.com/file/d/1eX_cczNM4qfDue6j83f8T4gG4ecjSV-p

https://drive.google.com/file/d/1KoJf753p3HXPHwP4N2lW9cWLXgusTP72

我只会张贴在这里作为一个片段他的更老更简单的代码版本。请访问他的GitHub页面的最新版本。

// author: user "nagualdesign" @ github
// github repository: https://github.com/nagualdesign/Primordial-Particle-System
// For more information visit: https://www.youtube.com/watch?v=makaJpLvbow
// This video focuses primarily on specific values of alpha, beta, v and r
// It goes on to show the effects of altering the values of alpha and beta
// To replicate the video it is necessary to tune the density of particles
// Density depends on the screen size, as well as particle size and number
// You can also increase/decrease density by zooming in/out and refreshing

// Global variables:
var a=180; // Alpha in degrees
var b=17; // Beta in degrees
var v=0.67; // Speed of particles
var r=5.0; // Radius of neighbourhood

// Convert to radians!
a=(a/180)*Math.PI;
b=(b/180)*Math.PI;

var canvas, context; // HTML canvas
var t=40; // Time interval in milliseconds
var s=5; // Size/scale of particles
var n=1200; // Number of particles
var p=new Array(n); // Particles

function init() {
	// Set up canvas:
	canvas=document.getElementById("canvas");
	canvas.width=window.innerWidth;
	canvas.height=window.innerHeight;
	context=canvas.getContext("2d");
	for (i=0; i<n; i++) { // Randomize position and orientation of particles:
		p[i]=new Array(4); // Each particle has 4 variables
		p[i][0]=Math.random()*window.innerWidth; // Set random x coordinate
		p[i][1]=Math.random()*window.innerHeight; // Set random y coordinate
		p[i][2]=Math.random()*2*Math.PI; // Set random orientation
	}
}

function draw() {
	context.clearRect(0,0,canvas.width,canvas.height); // Clear canvas
	for (i=0; i<n; i++) { // For each particle:
		// Set fill colour based on number of neighbours:
		let fc='#00C200'; // Green
		if (p[i][3]>35) fc='#F8E302'; // Yellow
		else if (p[i][3]>16) fc='#0064FF'; // Blue
		else if (p[i][3]>15) fc='#FF0792'; // Magenta
		else if (p[i][3]>12) fc='#A4714B'; // Brown
		// Draw particle:
		context.beginPath();
		context.arc(p[i][0],p[i][1],s,0,2*Math.PI);
		context.fillStyle=fc;
		context.fill();
	}
}

function scope(ang) { // Ensure angles are between 0 and 2*pi radians!
	while (ang>(2*Math.PI)) ang=ang-(2*Math.PI);
	while (ang<0) ang=ang+(2*Math.PI);
	return ang;
}

function loop() {
	for (i=0; i<n; i++) { // For each particle:
	// Count neighbors within radius r:
	let nLeft=0, nRight=0, nTotal=0;
	for (j=0; j<n; j++) if (i!=j) { // Compare every other particle:
	let sX=p[j][0]-p[i][0]; // X axis separation
	let sY=p[j][1]-p[i][1]; // Y axis separation
	let sD=Math.sqrt((sX*sX)+(sY*sY)); // Separation distance
	if (sD<(r*s*2)) { // Distance is within radius r
	nTotal++; // Increase count
	let sA=scope(Math.atan2(sY,sX)); // Separation angle
	if (scope(sA-p[i][2])<Math.PI) nRight++; // Neighbour on right
	else nLeft++; // Neighbour on left
	}
	}
	p[i][3]=nTotal; // Used for colouring particles

	// delta_phi = alpha + beta × N × sign(R - L)
	let deltaPhi=a+(b*nTotal*Math.sign(nRight-nLeft));

	// turn right delta_phi
	p[i][2]+=deltaPhi;
	p[i][2]=scope(p[i][2]); // Keep angles within scope!

	// Move forward v
	p[i][0]+=(v*s*2*Math.cos(p[i][2])); // X coordinate
	p[i][1]+=(v*s*2*Math.sin(p[i][2])); // Y coordinate

	// Wrap screen edges, Pac-Man style!
	if (p[i][0]<(s*-1)) p[i][0]=(canvas.width+s);
	else if (p[i][0]>(canvas.width+s)) p[i][0]=(s*-1);
	if (p[i][1]<(s*-1)) p[i][1]=(canvas.height+s);
	else if (p[i][1]>(canvas.height+s)) p[i][1]=(s*-1);
	}
	draw(); // Update canvas
}

function run() {
	init();
	run=setInterval(loop,t);
}
<body style="margin:0; background:#000; overflow:hidden;" onLoad="run();">
<canvas id="canvas" onclick="window.clearTimeout(run)"></canvas>
</body>
  1. 用户elggem @ github上

https://github.com/elggem/js-primordialparticles demo


© www.soinside.com 2019 - 2024. All rights reserved.