首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Node.js上运行多个物理世界,不能充分利用服务器

在Node.js上运行多个物理世界,不能充分利用服务器
EN

Stack Overflow用户
提问于 2019-03-28 03:39:04
回答 1查看 345关注 0票数 1

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

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

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

代码语言:javascript
复制
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);

这实际上工作得很好,因为物理通常是非常平滑的,但问题是当它达到容量时,会有很多卡顿,因为我认为线程中有太多的计算。

世界上的每一步平均需要大约.2ms,每秒200步,或者说每步之间5ms,理论上有25场比赛的空间。

有没有更好的方法来做这件事?我觉得我没有充分发挥我的服务器的潜力。也许旋转子进程?我试着在同一台机器上同时运行第二台服务器和这台服务器,但它们最终互相攻击,使所有的世界都变得非常迟钝。

编辑:添加一些关于物理世界的更多细节:每个世界大约有60个物体,其中大部分是静态墙。通常有7个移动物体,带有几个传感器来检测“目标”。

物理世界参数:

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

发布于 2019-04-02 14:36:57

为了更好地利用p2.js,您应该查看文档并关闭不需要的内容。例如:

代码语言:javascript
复制
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将使用大量内存并将CPU周期浪费在垃圾收集上。例如,如果您想在C/C++中切换到Box2d,并且可能直接使用libuv而不是Node.js,那么您可以通过一个好的因素提高性能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55385238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档