Collision

From Gamewiki

Revision as of 19:40, 14 January 2009 by 75.70.83.247 (Talk)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

In describing this pattern we’ll discuss some alternatives to the programming approach that we recommend, to illustrate some of the considerations that go into choosing a good approach.

An example of the collision pattern occurs in the gamelet Frogger, where it can happen that a moving truck hits a frog. The truck wants to continue driving to the right but the frog is in the way. The live frog needs to be replaced by a dead frog when this happens.

One way to program this effect is to make the truck agent check for a frog agent directly ahead, and, if it finds one, operate on it so as to replace the live frog with a dead one. Instead of using language or system specific representations to capture this behavior we employ a UML sequence diagram to illustrate the interaction between the truck and the frog. In this approach
the truck does everything: it detects the frog and determines what is done with it. If there are many different things the truck can hit, with different effects (for example if the truck hits a car, the car probably doesn’t turn into a dead frog), all of this has to be specified in the behavior for the truck. It seems more natural to make the frog deal with the consequences of being hit. A second approach
, that implements this idea, is for the frog to detect the approaching truck, and, if it detects one, to take the appropriate action, in this case turning itself into a dead frog. Now it’s the frog that’s doing everything: detecting the truck and taking action. If there are different things that can it the frog, for example cars and buses as well as trucks, the frog has to be programmed to detect all of them.

The recommended approach is a kind of blending of the truck-centered and frog-centered approaches. It looks a little more complicated at first, but deals more naturally with situations in which several kinds of things may collide with several kinds of things.

The idea is that collision behavior is divided between the truck and the frog. The truck detects the frog (or anything else it may collide with) and sends a message (say, “impact”) to the frog. The frog then responds to this message by changing itself to a dead frog. This diagram shows the sequence of events: trucks sees frog, truck sends “impact” message to frog. The frog reacts by changing its look to deal frog, plays sound, and erases itself.

Personal tools