2
\$\begingroup\$

How would I work a spawning algorithm into a sim engine with variable time steps?

If each engine step is, say, 1/30 of a second and I have some event occur with a 0.001 probability each step, how do I translate this to an engine with a variable time step? At the end of 1 second I could have nothing spawned or (in a very unlikely event) I could have as many as 30 spawned.

Now if the engine hasn't done an update for a full second, how can my spawner simulate the same behaviour and probabilites? What if my steps are much smaller, say 1/100 or 1/1000 of a second?

What algorithms can I use?

\$\endgroup\$
4
\$\begingroup\$

I'd go with something like this (pseudo-code):

spawnProbability     = 0.001 // Your original probability at fixed timestep
spawnAtThisFrequency = 1/30  // The original fixed timestep dt
dtAcc                = 0     // An accumulator that will accumulate the variable dt

update( dt )
{
  dtAcc += dt; // Accumulate

  while( dtAcc >= spawnAtThisFrequency ) // If enough time has accumulated...
  {
    dtAcc -= spawnAtThisFrequency // remove from the accumulator
    tryToSpawnWithProbability( spawnProbability ) // and try to spawn
  }
}

With this, you take into account that the dt can be smaller than what it was with a fixed timestep, and you can also handle larger dt.

\$\endgroup\$
0
\$\begingroup\$

The number of spawns will be distributed as a binomial, the sum of the Bernoulli RVs of your spawn windows.

E.g., say for a window w of some number of ticks per spawn opportunity, and an elapsed ticks e since last check, with Bernoulli probability of p per spawn window, the number of spawns will be distributed as

B(e/w,p)

So, at each spawn check (end of period e), take one sample from the resulting distribution, and you have the total number to spawn.

Obviously, e/w is not always an integer, so round/floor/ceiling as desired.

\$\endgroup\$
1
  • \$\begingroup\$ I think in this case translating the math into pseudo-code would be helpful for the OP and future visitors :) \$\endgroup\$
    – Vaillancourt
    Sep 23 '17 at 2:17
-1
\$\begingroup\$

I just used Random.Range for my spawn times. Could you use the same?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ This does not answer the question, perhaps you could edit the answer to show how you'd use that method to solve the issue at hand? \$\endgroup\$
    – Vaillancourt
    Sep 22 '17 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.