Asynchronous Animation

Hello,

I have always wondered how any kind of asynchronous animation would work. I can do animations but all of them are always timed (as an example, a sphere loop-orbiting around something since and for ever) made with something like “angle=(angle+x)%360” inside a never ending timer function. I apologize if this is not a speciffically OpenGL related one but I guess this is the best place to post here.

By asynchronous I would mean any rendering action that would happen at an unknown or random moment, and with limited duration. As examples, a character that would stop and walk after random timeslices or something that moves only when a button is pressed and eventually stops.

I have thought on some ‘status’ scheme (haven’t tried implementing it so far). Using the character example and being every character an object: if status Walk then walk or if status Stop then stop. Status changes by chances over time. However this would require a check in every clock. Something like:


for every clock do
  generate random number 0-99
  if number < 5 then //for a 5% chance of changing status
    swap status
  endif
  if status=walk then
    increments position
  endif
endfor

Also, I wonder if I would have to keep two timers for this. Like one for frame rendering and other one for this kind of event handling?
The same clock for frame rendering would be just weird for me. I also wonder if there’s no problem to run more than one timer at once, and what would be their frequency (probably the event timer faster than the rendering timer…).

That is, i can only think on a solution where there would be a timer and chances for things to start happening and finish happening. Is it an okay thought?

Sorry if this was a somewhat noob example and/or question, it’s just that I cant really think on how to Google for that, and would like some guidance.

Thanks in advance, any input will be appreciated! :slight_smile: (and sorry for any bad english, Brazil here.)

You just have to clearly separate the simulation logics (one that changes your world) and the rendering logics (one that presents your world). A nice abstraction would be, for example, a multithreaded application, where one thread updates the world (what you call “status”) and another one renders this world continuously.

I agree with Zengar. Using a multithreaded application you can separate all the other stuff from rendering (except for some synchronization stuff). This will lead you to a much clear design.

Hi,

thanks for the quick inputs.

I see the point on multithreading that solution, though it’s not the issue I was willing to solve.

My doubts are more around the solution for the asynchronous/random events problem.

So is that what I created a reasonable solution? Like, is that how apps make things randomly happen? With an event timer and checking for object statuses and new events at each “clock cycle”?

Any related article I could read to clarify that out, if my questions seem too confuse?

Thanks in advance, any input will be appreciated :slight_smile:

Yes, your implementation is one possible way.
However it is often needed that right after switching state, you want to prevent a new switch for a definite amount of time (to prevent unnatural flicker).

Having definite min/max bounds is a plus, so I would probably go with something like that :


int minsteps=10;
int maxsteps=100;
generate random number between minsteps;maxsteps
for every clock do
  number--
  if number = 0
    swap status
    generate random number between minsteps;maxsteps
  endif
  if status=walk then
    increments position
  endif
endfor

Then you can tweak the ‘number’ generation to have a gaussian look, or whatever you feel interesting such as a log-normal distribution. Or even use different distributions for each state.