Monday, November 24, 2008

Why Animating Cars Doesn't Always Work Right

I saw a post about this on X-Plane.org...authors sometimes try to make a vehicle (a car, truck, etc) modeled via an OBJ "drive around" using animation translate commands.  The problem is that sometimes the objects disappear.  Here's what is going on:

X-Plane uses a bounding sphere to decide whether to draw an object.  The bounding sphere is the smallest sphere X-Plane can fit around the entire object; if the sphere is on screen, the object is drawn (even if the object itself isn't on screen).  We do this because we can test whether the sphere is on screen very quickly.

But what if the object has animation?  X-Plane attempts to guess how animation might affect the sphere by looking at animation commands and making the sphere a bit bigger where animation might move the object outside the sphere.  This process works, well, rather poorly. In particular, X-Plane doesn't know exactly how your datarefs will change.  This results in two error cases:
  • If X-Plane assumes the animation is more drastic than it really is, we make the sphere too big.  The object will then be drawn even when it is not on screen (because the sphere is on screen because it is too big).  This case hurts fps but does not cause objects to disappear.
  • If X-Plane assumes the animation is less drastic than it really is, we do not make the sphere big enough, and sometimes the object "disappears" because the object is on screen but the (too small) sphere is not.
Now let's apply this to objects that are driving around.  Usually this is done via a translate animation command where datarefs feed the object's position.

X-Plane estimates the effects of a translate animation using the largest and smallest key frame values.  But the animation engine will extrapolate beyond these key frames.  So consider these three cases:
  • As your dataref goes from -1 to 1, you translate by +/- 1 meter.  In this case, the bounding sphere will be increased in radius by one meter.
  • As your dataref goes from -25 to 25, you translate by +/- 25 meters.  In this case, the bounding sphere is increased in radius by twenty five meters.
  • As your dataref goes from -1000 to 1000, you translate +/- 1 kilometer.  In this case, the bounding sphere is increased in radius by 1000 meters.
Note that in all three of these cases, the animation works exactly the same!  But by using different dataref and value extremes, X-Plane's estimate of the effects of the animation (and its change to the boundign sphere) can be quite different.

So...if you animate an object and it disappears, it is probably because the bounding sphere has not been increased, perhaps because a translation animation is being sent values outside its minimum and maximum values.

The problem is of course that to have an object "roam" over a large area, it must have a very large bounding sphere, which means it is being drawn a lot more than necessary.

No comments: