Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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 在Node.js上运行多个物理世界,但无法充分利用服务器_Javascript_Node.js_P2_Cannon.js - Fatal编程技术网

Javascript 在Node.js上运行多个物理世界,但无法充分利用服务器

Javascript 在Node.js上运行多个物理世界,但无法充分利用服务器,javascript,node.js,p2,cannon.js,Javascript,Node.js,P2,Cannon.js,我正在AWS上使用Node托管我自己的多人游戏世界。这个游戏是基于物理的(使用p2.js),所以我有一个相当高的物理步速率,每秒200步 每一场比赛都有自己的世界,每个世界都需要每5毫秒一步。每个游戏只有6-8名玩家,所以我一次只能在服务器上托管大约60名玩家。我想改进这个,但我不知道如何改进 现在,我正在使用setInterval并按顺序遍历每个物理世界 const stepsPerSecond = 200; // Number of games the server can manage.

我正在AWS上使用Node托管我自己的多人游戏世界。这个游戏是基于物理的(使用p2.js),所以我有一个相当高的物理步速率,每秒200步

每一场比赛都有自己的世界,每个世界都需要每5毫秒一步。每个游戏只有6-8名玩家,所以我一次只能在服务器上托管大约60名玩家。我想改进这个,但我不知道如何改进

现在,我正在使用
setInterval
并按顺序遍历每个物理世界

const stepsPerSecond = 200;
// Number of games the server can manage.
const numSlots = 10;
// Evaluates to 500 microseconds (.5ms)
const timePerStep = parseInt(1000 * (1000 / stepsPerSecond) / numSlots);
const timeLabel = `${timePerStep}u`;

this.timer.setInterval(() => {
  const slotIndex = this.currIndex++;
  // Go back to beginning of slots if end.
  if (this.currIndex == this.numSlots) {
    this.currIndex = 0;
  }
  const game = this.slots[slotIndex];
  game.physics.update();
}, '', timePerStep);
这实际上效果很好,因为物理学通常是非常顺利的,但问题是当它接近容量时,会有很多口吃,我认为线程中的计算太多了

世界上每一步平均需要0.2毫秒,每秒200步,或者每一步之间5毫秒,理论上有25场比赛的空间

有更好的方法吗?我觉得我没有充分利用服务器的潜力。也许是加速子进程?我试着在同一台机器上运行第二台服务器,但它们最终会互相碰撞,使整个世界变得极其落后

编辑:添加有关物理世界的更多详细信息: 每个世界大约有60个天体,其中大部分是静止的墙壁。通常有7个移动物体,带有两个用于检测“目标”的传感器

物理世界参数:

world.setGlobalStiffness(1e8);
// Default is 4, but the puck sometimes warps through the sticks
// with this any lower, even with CCD enabled.
world.setGlobalRelaxation(10);

maxSubSteps = 4;

要从p2.js中获得更多信息,您应该查看文档并关闭不需要的东西。例如:

world.defaultContactMaterial.friction=0; // if you don’t need contact friction on a ContactMaterial
world.narrowphase.enableFriction=false; // if you never need friction for anything in your World
world.applySpringForces=false; //if you don’t use springs
world.applyGravity=false; // if you don’t need gravity along x or y axis 
world.applyDamping=false; // not sure if you need this?
world.islandSplit=false; // since you have a top-down type of game with few contacts, this might save you a run of the UnionFind algorithm
world.emitImpactEvent=false; // unless you use it
world.broadphase.sortAxis=0; // 0 means x and 1 means y. Choose the axis on which your bodies are more spread out along.
world.solver.iterations=3; // this is default 10, you might want to decrease it. Just check if your collisions work OK after you change it.

<>如果你想从你的服务器中得到最大的好处,你也应该考虑你使用的技术栈。运行JavaScript物理引擎的Node.js将在垃圾收集上使用大量RAM和浪费CPU周期。例如,如果您在C/C++中切换到Box2d,并且可能直接使用libuv而不是Node.js,您可以通过一个很好的因素来提高性能。

要从p2.js中获得更多信息,您应该查看文档并关闭不需要的东西。例如:

world.defaultContactMaterial.friction=0; // if you don’t need contact friction on a ContactMaterial
world.narrowphase.enableFriction=false; // if you never need friction for anything in your World
world.applySpringForces=false; //if you don’t use springs
world.applyGravity=false; // if you don’t need gravity along x or y axis 
world.applyDamping=false; // not sure if you need this?
world.islandSplit=false; // since you have a top-down type of game with few contacts, this might save you a run of the UnionFind algorithm
world.emitImpactEvent=false; // unless you use it
world.broadphase.sortAxis=0; // 0 means x and 1 means y. Choose the axis on which your bodies are more spread out along.
world.solver.iterations=3; // this is default 10, you might want to decrease it. Just check if your collisions work OK after you change it.

<>如果你想从你的服务器中得到最大的好处,你也应该考虑你使用的技术栈。运行JavaScript物理引擎的Node.js将在垃圾收集上使用大量RAM和浪费CPU周期。例如,如果您在C/C++中切换到Box2d,并且可能直接使用libuv而不是使用Node.js,那么您可以通过一个很好的因素来提高性能。

Node.js在异步操作中的excel,。但物理本质上并不是异步的。我要做的是把你的物理计算放到另一个过程中。您可以尝试->实际上避免npm,暂时不更新。。我最近用过一个,效果非常好。。我会去找它的。我明白了,你是在建议每个世界都有自己的进程,然后从计时器发送一条消息进行计算吗?不知道我现在用的是什么软件包!!,我想可能是这一个,或者我甚至可能直接转到node.js工作线程。但在节点中,我不得不做一些繁重的计算,我做到了这一点。:)你可以用自己的方法处理每个世界是的,这样可以使事情变得美好和简单。。然后让你的main node.js进程做它擅长的事情。你能添加你在p2上做的关于时间步进、解算器、宽相位等性能相关的设置吗?有多少个实体/形状,什么类型?@schteppe-sure,刚刚在异步操作中添加了这些,。但物理本质上并不是异步的。我要做的是把你的物理计算放到另一个过程中。您可以尝试->实际上避免npm,暂时不更新。。我最近用过一个,效果非常好。。我会去找它的。我明白了,你是在建议每个世界都有自己的进程,然后从计时器发送一条消息进行计算吗?不知道我现在用的是什么软件包!!,我想可能是这一个,或者我甚至可能直接转到node.js工作线程。但在节点中,我不得不做一些繁重的计算,我做到了这一点。:)你可以用自己的方法处理每个世界是的,这样可以使事情变得美好和简单。。然后让你的main node.js进程做它擅长的事情。你能添加你在p2上做的关于时间步进、解算器、宽相位等性能相关的设置吗?还有多少身体/形状,什么类型?@schteppe当然,刚刚在上面加了这些。谢谢!今晚晚些时候我会试试这个。谢谢你的帮助,谢谢你提供了一个很棒的引擎!我想避免使用Box2d,但我想我们会看到的。谢谢!今晚晚些时候我会试试这个。谢谢你的帮助,谢谢你提供了一个很棒的引擎!我想避免使用Box2d,但我想我们会看到的。