genaray/Arch

Documentation examples missing deltaTime usage

dvdking opened this issue · 2 comments

While exploring the library I found myself wondering what a cannonical way would be to pass delta time to system updates without allocations.

There are a variety of ways to do this:

  • Use clojure to pass params (allocates garbage running release code in unity, but easy to write)
      var dt = t.DeltaTime;
      World.Query(in queryDesc,
        (ref Position pos, ref Velocity vel) =>
        {
          pos.Value.X += vel.Value.X * dt;
          pos.Value.Y += vel.Value.Y * dt;
        });
garbage

 

  • Write a custom inline query with parameters, harder to write but no allocations
   private struct VelocityUpdate : IForEach<Position, Velocity>
    {
      public float DeltaTime;
      
      [MethodImpl(MethodImplOptions.AggressiveInlining)]
      public void Update(ref Position pos, ref Velocity vel)
      {
        pos.Value.X += vel.Value.X * DeltaTime;
        pos.Value.Y += vel.Value.Y * DeltaTime;
      }
    }
  • Write custom loop, way too much code for this, but no allocations too
      foreach (ref var chunk in query)
      {
        var positions = chunk.GetSpan<Position>();
        var velocities = chunk.GetSpan<Velocity>();
        
        for (var i = 0; i < chunk.Size; i += 1)
        {
          ref var pos = ref positions[i];
          ref var vel = ref velocities[i];
          
          pos.Value.X += vel.Value.X * t.DeltaTime;
          pos.Value.Y += vel.Value.Y * t.DeltaTime;
        }
      }

So questions regarding this:

Should such examples be added to docs?
Is there a better way to pass uniform params to query updates I'm missing?

Ideally for simple case it could be done like:

  World.Query(in queryDesc, in uniformData,
    (in uniformData, ref Position pos, ref Velocity vel) =>
    {
      pos.Value.X += vel.Value.X * uniformData.DeltaTime;
      pos.Value.Y += vel.Value.Y * uniformData.DeltaTime;
    });

Thanks! And yes this should :)
Im currently working on an open source documentation for arch. Its still WIP but already in this repo:
https://github.com/genaray/Arch.Docs
https://arch-ecs.gitbook.io/arch

You might wanna add a page yourself... e.g. under documentation/optimisations. Would be great if you could contribute (also to test if it works).

Looking great!
I may actually try to add it during the next weekend