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.

4 comments:

Matt Worden said...

Nice work on these tutorials ... and thanks for helping to bridge the VB.Net/XNA gaps!

Your matrix-based approach to sprites adds another layer of power for 3D-doing-2D beyond what I've considered before (the superfiscial alpha/rotation/scaling advantages).

I will definitely be keeping an eye on these!

emachine74 said...

Thanks Matt. I've been pleased with the results I can get with the matrix approach so far. I hope to learn a little more about them so I can implement a shearing matrix too.

Hope you find useful things here.

Matt Worden said...

I have a question for you on the matrix cascade piece with a practical game example ... probably something you would get to later, but I'll ask it now to put it in your head ... and I can be patient if it's coming up a few tutorials down the line ...

This system would make it very easy to have a space ship with independently-operating mounted gun turrets. The ship would be the parent sprite, and each gun turret would be a child of the ship. This would mean the guns would move right along with the ship as it travelled, without any special math or rotation/position adjustment being done elsewhere.

However, the guns would want to rotate to track (and fire at) enemy ships moving around in the world space. How would the matrix math work to have the children sprites rotating to point at something in the world, while still being married off to the parent? (I realize "it's just math" ... but I'm having problems wrapping my head around the approach to it.)

emachine74 said...

Good question! In fact I'll do a little write up about it later tonight, it sounds like a fun short topic and the tutorial engine is now adequately equipped to do it.