7

我正在制作类似于 Polyworld 的东西,这意味着我将模拟虚拟世界,小爬行者在其中四处奔跑、进食和进化。我正在用 Node.js 做这个,我计划使用物理和神经网络,但我不确定更新世界的最佳方式是什么,更具体地说,更新函数是否应该接收增量时间作为参数,还是每次都做同样的事情,与他们最后一次打电话的时间无关?两种方式有什么好处?

编辑: 我反对持续更新的一点是我想实现某种间隔,例如,每 20 模拟秒产生一个食物块。如果 dt 与 1 不同(或 1 的一小部分),这将永远无法精确工作。

再说一次,如果我进行离散更新,更新不关心已经过去了多少时间,我将无法“减慢时间”。当我让它在一个强大的服务器上工作并在浏览器中渲染时,我认为更新会经常发生,我需要一种在不影响模拟的情况下减慢时间的方法,这样我就可以看到发生了什么。

4

6 回答 6

2

每次调用更新函数时,您都可以计算自动画开始以来经过了多少时间。然后你将这个时间传递给更新函数并拥有它,即使帧可能不会在第 20 秒精确更新,你可以根据实际时间进行所有计算。

Example: Car starts its movement at 20th second with speed 3units/s. Assume that update function is triggered at following times: ..., 19.35s, 20.67s, ... When updating at 19.35s you know what it should not yet be moved so nothing happens, but when update function is triggered with time value 20.67s then you know that car has already been moving for 0.67s, therefore you calculate its position (time*speed) 0.67*3 = 2.01 and do all other calculations/draw as if it was already 2.01 units moved. This way you don't need to worry about not precise time measurements, lags, updating function taking more time than usually etc.

于 2012-08-29T06:48:25.380 回答
2

If your don't have multiple agents (each with its own thread) that have to collaborate and that you dont have to deal with synchronization/events of process flow problems I recomend you to use continuous simulation. Using fixed time step and change the state of your world in each step. Each world piece change its state using a funcion like:

newState = f(oldState, deltaSteps)

About the speed problem you mention, do not directly map your iterations to time. Define a intermediate time unit (step), and them map this unit time to ms, iterations or what you prefer. So if you what to increase or reduce your simulation speed, just change the factor used to conver from step to time/iterations. If you need to change speed just change your constant.

Check this page with some insight about simulation techniques.

于 2012-08-29T18:15:17.870 回答
1

我认为您不会超过某个频率(例如 50 Hz)。这会在不必要的精度上浪费 CPU 时间。

如果用户的设备无法提供该更新速率,您可以选择

  1. 保持相同的物理频率并减慢挂钟速度
  2. 使用较高的 delta T 降低物理频率

如果频率仍然高于 20Hz,我会选择 2。如果它变得更低,您可能应该切换到策略 1 以保持精度。

因此,您可能需要基于 deltaT 的解决方案,以便调整更新频率。

于 2012-08-22T22:39:29.147 回答
1

Your update functions should do the same thing every time step.

There are drawbacks to both approaches, but passing a delta that represents the time elapsed since the last update becomes difficult to manage when simulating many-to-many interactions within a population. This is because it is time-consuming to predict the points in time (the deltas) at which interactions will happen. If these points in time are missed, the simulation is not accurate.

A drawback to the approach that updates all the elements at every time-step is that it will do unneeded work. However, the cost of this unneeded work will probably be less than the amount of work it would take to accurately predict which points in time need to be evaluated, especially given a complex interacting environment.

于 2012-08-31T20:54:59.037 回答
0

I think you are going to want your animation to be continuous and based on some elapsed time or clock (that you might be able to speed up or slow down). So some update functions you would want to be based on a delta.

But that doesn't mean that you can't for example use a setInterval to spawn food blocks. It also doesn't mean that everything else needs or should be based on that delta.

For example, you could check after your position update for creatures that are near each other or whatever condition is required for procreation, and then generate the offspring as a discrete step that isn't dependent on the current clock. You would want to record what the clock was when that happened though.

于 2012-08-29T08:59:00.457 回答
-1

I would do this with constant timestep. It's by far more easy to code. Each update function does one step according to environement. You don't have to update the browser each step. You can compute 10 or 100 steps and then send results to browser.

It will be much more accurate this way. Lot of small simple steps are much easier to code than delta time depending functions (a nightmare).

If you are using variable time steps. When time step is big, you could have scenario where an ant is on point A at t0 and point B at t+delta. You update the ant first, it ends in point B. The you update a food respawn point between A and B that should have respawn at t0+ 1/3 delta. The ant passed by without seening the food. The simulation is wrong.

Other things you'll probably need :

  • To be really accurate you must check collision between segments [previous position - new position] rather than points. Otherwise ants may cross themselve without colliding. Check physics engines.
  • Avoid to send all objects to your browser. Use Octrees or quadtrees to quickly determine wich subset of your data correspond to the area displayed by browsers.

Strange choice to do this in Node.js, I would use a Java or a real oop language.

You'll find lot of useful help on game developer forums.

于 2012-08-30T21:32:24.630 回答