11/14/07

VB.NET XNA Tutorial 5.3: The Parent/Child Matrix Cascade

[GSE 1.0 Refresh]

While tutorial 5.3 encodes (...it really takes forever), I'll write a little preface. Things get a little "weird" in tutorial 5.3, so hopefully I'll be making sense.

In the previous tutorial we saw how a sprite can use a matrix to position itself on the screen. We drew kermit to the screen by supplying his matrix to the Begin method of the SpriteBatch, and then supplied his origin to the Draw method. These two separate pieces of data (the matrix and the origin) passed to those two methods worked together to put kermit where we wanted on the screen, and make him rotate how we wanted about his origin.

In tutorial 5.3, we're going merge origin information into the matrix, so that we no longer have our matrix and origin supplied as separate data pieces when we draw; we'll be able to pass a single matrix to Begin without supplying an origin value to Draw.

By incorporating the origin information into the matrix, the matrix becomes a more comprehensive/accurate/complete description of how to place a sprite on the screen. In turn, we can use that matrix to transform other things in addition to the sprite itself (such as the sprite's children).

So we're going to create a simple Parent/Child system, whereby each sprite has a reference to a parent sprite as well as a list of children sprites. This will give us a way to organize groups of sprites together into logical objects, and at the same time it will give us a vehicle for the "matrix cascade". Here's what a "matrix cascade" is designed to accomplish:

Dinner on a Plate

Dinner on a Plate demonstrates the net result of what our engine can do after tutorial 5.3. I thought it might be a good idea to get a clear picture of our goal, without which tutorial 5.3 can be, unfortunately, "a long trip through abstract city".

As you can see in Dinner on a Plate, it's starting to become pretty easy to build a new application that, if nothing else, demonstrates how a mildly complicated positioning exercise can be greatly simplified. In the video, a cookie and a piece of pizza are "put onto a dinner plate", and when the plate is rotated, the pizza and cookie behave as if they are intuitively attached to the plate. There are no laws of physics at work here, the explanation is simply that, as children of the dinner plate, the cookie and the pizza are subject to the dinner plates matrix cascade. This means that every time the dinner plate "changes" (location, rotation, origin or (eventually) scale), the cookie and pizza slice are automatically incorporating the new dinner plate matrix into their own matrices when they draw.

The "exciting" details can be found in Tutorial 5.3 - The Parent/Child Matrix Cascade.