Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使javascript冲突模拟更有效?_Javascript_Performance_Collision Detection_Complexity Theory - Fatal编程技术网

如何使javascript冲突模拟更有效?

如何使javascript冲突模拟更有效?,javascript,performance,collision-detection,complexity-theory,Javascript,Performance,Collision Detection,Complexity Theory,我正在努力改进碰撞模拟的代码。 让我们以1000个粒子碰撞为例,目前所有粒子都具有相同的半径 此代码大约运行100万次: for (i = 0; i<particles.length; i++){ for (j = 0; j<particles.length; j++){ if (particles[i] === particles[j]) continue; if (distanceOf(particle[i], particle[j] < ra

我正在努力改进碰撞模拟的代码。 让我们以1000个粒子碰撞为例,目前所有粒子都具有相同的半径

此代码大约运行100万次:

for (i = 0; i<particles.length; i++){
   for (j = 0; j<particles.length; j++){
      if (particles[i] === particles[j]) continue;
      if (distanceOf(particle[i], particle[j] < radius*2)) {
         Collide(particles[i], particles[j]);
         }
    }
 }

用于(i=0;这是一个问题,这些扇区边缘上的粒子可能会相互碰撞?如果是,那么你不能像这样分开。我不是那种打字难的类型,但我认为你的标题是指javascript,而不是jacascript?是的,我知道我需要制作另一个256个单元格的网格,以偏移到第一个网格解决边界问题。但这不是我的问题。我只想知道这段代码中是否存在严重阻碍x125速度的复杂性问题。例如:在这个解决方案中,我需要一个256个数组,其中应该包含1000个粒子的索引,并且我不断检查和更新这对我的程序的效率和速度有什么影响?@Alafandy你将用更多的内存来换取那些节省的计算周期,你需要考虑分配粒子所花费的时间,但除此之外,它看起来还不错。你为什么不实施它并做一些基准测试呢?确切的fac改进的tor在很大程度上取决于实现、语言和平台细节。
for (i = 0; i<particles.length; i++){
   for (j = i + 1; j<particles.length; j++){
      if (particles[i] === particles[j]) continue;
      if (distanceOf(particle[i], particle[j] < radius*2)) {
         Collide(particles[i], particles[j]);
         }
    }
 }
for (i = 0; i<particles.length; i++){
   let cellNumber = particles[i].cell; //each particle updates a "cell" variable with it's own current cell number by calculating it from it's position.
   for (j = 0; j<sector[cellNumber].length; j++){ //sector[] is an array of 256 cells, each one is an array of the indexes of the particles in it.
      if (particles[i] === particles[sector[cellNumber][j]]) continue;
      if (distanceOf(particle[i], particles[sector[cellNumber][j]] < radius*2)) {
         Collide(particles[i], particles[sector[cellNumber][j]]);
         }
    }
 }