An anecdote on recursion
Dec. 30th, 2006 02:01 pmMany of you will remember the Winnie-the-Pooh flash game I wrote. Here is a puzzle for you.
The world is a grid of squares, containing (level 1) grass or pit or wall and (level 3) Chris or Pooh or Piglet. Continuous motion is represented by a series of discrete steps. So every 400ms, the "tick" function runs, which updates all positions at the same time, the map changing instantly from eg. "space, Pooh, Pooh, space, Chris, space" to "space, space, Pooh, Pooh, Chris, space" if too Poohs were running toward Chris.
The animation is chosen to show movement from one square to another, so although logically the second state of affairs takes over instantly, in fact the pictures are internally displaced 3/4 of the number of pixels in a square to the left, and then 1/2 and 1/4, before 300ms later being centred on the square they are actually "on" (in fact, moving towards).
But although the Poohs positions are all updated at the same time, in fact, that function must calculate where to put them in some order. In fact, there is a function representing Pooh's thoughts, which directs the direction he tries to run, and then a function representing movement which runs for every character, even CR, which actually updates.
A brief thought will show potential pitfalls -- the same that Dungeons and Dragons faces when it tries to sequentially represent concurrent events. For instance, if Pooh1 and Pooh2 are trying to move into the same square, which does? In fact, it's essentially arbitrary -- whichever move function happens first moves, and the other then tries to but finds the way blocked. This almost never makes a difference -- one will be in the square closest to you, and the other will follow next click, so there's only 4/10 of a second when the game could be in two states. In fact, it is deterministic, it depends on the initial position of the characters, from which they are read into an array of all moveable things at initialisation.
When CR interacts with Pooh it makes a difference. CR moves first, then other animals. Animals "think" before CR moves, (except that some, including Pooh, think both before and after). This means that if you pause the screen, when you continue, Pooh will move towards where you are now, not where you're moving to. Eg. if CR and Pooh are both moving into the same square, CR will move first, and then Pooh will be blocked. Except that then one or the other will die, but this will be shown with a slight jerk, as CR move-into-new-square animation is replaced by a kill or die animation in the new square
OK, that was long, it may be of interest to amateur programmers, the interesting bit will go in another post
The world is a grid of squares, containing (level 1) grass or pit or wall and (level 3) Chris or Pooh or Piglet. Continuous motion is represented by a series of discrete steps. So every 400ms, the "tick" function runs, which updates all positions at the same time, the map changing instantly from eg. "space, Pooh, Pooh, space, Chris, space" to "space, space, Pooh, Pooh, Chris, space" if too Poohs were running toward Chris.
The animation is chosen to show movement from one square to another, so although logically the second state of affairs takes over instantly, in fact the pictures are internally displaced 3/4 of the number of pixels in a square to the left, and then 1/2 and 1/4, before 300ms later being centred on the square they are actually "on" (in fact, moving towards).
But although the Poohs positions are all updated at the same time, in fact, that function must calculate where to put them in some order. In fact, there is a function representing Pooh's thoughts, which directs the direction he tries to run, and then a function representing movement which runs for every character, even CR, which actually updates.
A brief thought will show potential pitfalls -- the same that Dungeons and Dragons faces when it tries to sequentially represent concurrent events. For instance, if Pooh1 and Pooh2 are trying to move into the same square, which does? In fact, it's essentially arbitrary -- whichever move function happens first moves, and the other then tries to but finds the way blocked. This almost never makes a difference -- one will be in the square closest to you, and the other will follow next click, so there's only 4/10 of a second when the game could be in two states. In fact, it is deterministic, it depends on the initial position of the characters, from which they are read into an array of all moveable things at initialisation.
When CR interacts with Pooh it makes a difference. CR moves first, then other animals. Animals "think" before CR moves, (except that some, including Pooh, think both before and after). This means that if you pause the screen, when you continue, Pooh will move towards where you are now, not where you're moving to. Eg. if CR and Pooh are both moving into the same square, CR will move first, and then Pooh will be blocked. Except that then one or the other will die, but this will be shown with a slight jerk, as CR move-into-new-square animation is replaced by a kill or die animation in the new square
OK, that was long, it may be of interest to amateur programmers, the interesting bit will go in another post