在Node.js上运行几个物理世界,而不是从服务器中获取最多

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

我正在使用Node在AWS上托管我自己的多人游戏世界。游戏是基于物理的(使用p2.js),因此我的物理步长非常高,每秒200步。

每个游戏都有自己的世界,每个世界需要每5ms步。每个游戏中只有大约6-8个玩家,所以我一次只能在服务器上托管大约60个玩家。我想改进这一点,但我不确定如何。

现在,我正在使用nanotimer 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;
javascript node.js p2 cannon.js
1个回答
0
投票

为了从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,那么您可以通过一个很好的因素来提高性能。

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