6/29/2004

6-29-04 - 2

6-29-04

One thing that lots of people fuck up is not having hysteresis in their game triggers. For example, let's say you have some volume trigger. When the player is inside that volume he's considered "hidden". If you just do a normal volume check, he can flicker in and out every other frame. If you add hysteresis, you prevent these sudden changes. The easy way to do it is by having a slightly larger volume for the "exit volume" and smaller one for the "entry volume". That way if you are considered "out", you don't switch to "in" unless you qualify the smaller entry volume. Then you stay "in" until you are rejected by the larger "exit volume". This gives you a little transition fuzz region where your previous state is held. Of course if you can use an analog key instead of a bool, that makes it a smooth transition. More generally, if you can key the transition off an analog, you can have hysteresis with biased transition points. Let's say you have some simple distance trigger, you generate a float in [0,1] based on the distance to some point. You want to generate a boolean for whether you are "near" or not. If you did it trivially, you'd just say :

near = f > 0.5

But with hysteresis, you make the previous value "sticky" like this :

if ( ! was near )
        near = ( f > 0.6 );
else // was near
        near = ( f > 0.4 );

here we've encoded an 0.2 "hysteresis" region, where the previous state is preserved, whatever it was. Another way to encode this is :

if ( f > 0.4 && f < 0.6 )
        // no change, leave previous state
else
        near = f > 0.5


or


if ( f < 0.4 )
        not near
else if ( f > 0.6 )
        near
else
        // no change, leave previous state

No comments:

old rants