How to use multiple tick providers (frame & fixed) with Ash?

80 views
Skip to first unread message

Phillip Knoll

unread,
May 10, 2013, 11:45:07 AM5/10/13
to ash-fr...@googlegroups.com
(I just posted this on stackoverflow, also.)

It's a common practice to make rendering calculations on frame update and physics calculations at a fixed interval of time. I don't understand how to do this in Ash. All examples of Game objects I've seen just use one `ITickProvider` (could be a FixedTickProvider or FrameTickProvider), which calls `engine.update()` on each tick. What if, for example, I want to update my rendering systems at 60 fps, but update game logic at a fixed interval of time, in case there's lag?

    tickProvider = new FrameTickProvider( container );
    tickProvider
.add( engine.update );
    tickProvider
.start();

Some ideas...

 - Can I update groups of systems separately?  
 - Should I use 2 engines?

Eric Lund

unread,
May 10, 2013, 3:58:40 PM5/10/13
to ash-fr...@googlegroups.com
This is a bit of a hack, but you can pass a negative constant to the update(time) method of your System to act as group triggers. You could subclass System into a GroupSystem class which accepts a negative value in the constructor as its group trigger. Then in GroupSystem.update() if the time passed in is negative and is equal to the stored trigger value, it runs groupUpdate(), which you implement in your GroupSystem subclass. To trigger any group of systems, you supply the negative trigger value of the group to engine.update().

Worst case, this gives you a lot of systems that do fast (but pointless) conditional checks. I agree that it would be nice if Ash provided a manner of grouping systems and running them independently. Another way of handling this that I've used before to implement a turn-based system is to check a shared object if the turn number has changed. But I think a GroupSystem class would be more elegant. You could also maintain your own array or SystemList of Systems, supplying them a copy of the engine object and manually running their update method as needed, but I tried that before and it felt like a bigger kludge.

I don't recommend using separate engines unless you have a clear separation of entities.

Sercan Altun

unread,
May 11, 2013, 2:15:09 AM5/11/13
to ash-fr...@googlegroups.com
Create an utility class that excepts delta time input and keeps track if it is time to do next tick. Then do what ever you do on the systm based on that. It may not be most elegant but it is easy to implement and understand.

// for one time per update
FixedClock.update( delta);


// in each fixed update code
if( FixedClock.hasTick() )
{
// do stuff
}

Billy Belfield

unread,
May 11, 2013, 4:06:32 PM5/11/13
to ash-fr...@googlegroups.com
I'm in a similar situation where I'm trying to switch my motion systems to use fixed time.  In a non-ash loop this may look like:

for (var i:int = 0; i < steps; ++i)
{
   updatePhysics(FIXED_TIMESTEP);
   checkCollisions();
}

interpolatePositions();
render();

So, I may end up not executing a physics update at all or multiple times in a frame.  In any case all collision systems need to be updated once for each physics tick.  My workaround was to force an update of all my physics and collision systems if they need to be called more than once...not very flexible though.   It seems that having the logic inside Engine.updateSystems might be better...it decides whether or not to update a system based on if a system is using frametime or fixed time.  In the latter case it could update it multiple times or not at all depending on its timestep.


On Friday, May 10, 2013 11:45:07 AM UTC-4, Phillip Knoll wrote:
Reply all
Reply to author
Forward
0 new messages