20.Feb.07 Lecture 4
3D Graphics Primer and Torque Game Engine

Do you game?

If you look at your life, do you see it as a first person or a third person shooter. My guess is that most people behave as if they are in an FPS. It's pretty easy to just sit in front of the TV or your favorite game and just immerse yourself for hours. Right?

Try this exercise: Step back from yourself a little bit and just pretend for a minute that your body is just an avatar in the third person and you are the controller sitting behind a computer screen somewhere. What would you do with that life? What would be your goal? Would you have your avatar sitting in front of the TV for hours or playing some RPG? Would you have it working hard towards some life goal even if took years of tedious work to complete?

What if your control of your avatar wasn't complete and you could only give it hints as to what you wanted it to accomplish and it just sat in front of the TV anyways? Would that be frustrating? If you have faith, do you think it would be hard for God to give someone so much potential and see them sitting around doing nothing with it?

Think of your favorite game, whether it be halo, wow, or some other rpg. Do you think it would be fun to be the avatar? Do you really think it would be fun killing the Covenant over and over and over again, or hacking on the same Gnoll/spider/bird/orc over and over and over again? Or do you think it is more interesting to look at the big picture and understand that fixing bugs can be much less tedious and more goal oriented if you step back and look at the bigger picture. If you were the master chief, would you veg out in front of the tv, or fight the covenant?

What am I getting at? Live your life with a goal and work towards that goal. There are a million things that will distract you and mush your brain into simplicity. You have to fight to achieve a goal and half of the battle will be finding a way to make the tedium interesting.

Get to work! Only 77 days left.

3D programming overview

Read Chapter 3 - All in one book, but it doesn't go deep enough so here is more.

Positions and Vectors

Positions are a location in three dimensional space (x, y, z).

Vectors are a line going from 0,0 to a position (x,y,z). It can also be associated with a position in space and give a magnitudinal direction, but it's magnitude is still measured from a 0,0 origin.

Both positions and vectors are represented in the same way, but used differently. Positions define a location and vectors usually define a facing direction or movement. For instance if you are standing on the origin and looking directly north (in the positive z direction), your eyes might be located at the position (0,1.82, 0). This is if you are 6 feet tall or 1.82 meters. A vector is used to define the direction you are looking and in this case would be (0,0,1.0). Note, direction vector does not have anything to do with the actual location of the eyes, it is only used to represent direction.

Vector Length

The length of a vector is used for many calculations.

length = sqrt(x*x+y*y+z*z).

You should notice that this is simple the calculation for the hypotenuse of a right triangle. Simple math used in an elegant way. Now aren't you glad you finished the 8th grade and finally (almost 10 years later) have something useful you can use that math for?

Vector math

You can add (or subtract) two vectors. (x1,y1,z1) + (x2,y2,z2) = (x1+x2,y1+y2,z1+z2).

You can multiply (or divide) a vector by a scalar to make another vector:

a * (x1,y1,z1) = (a*x1, a*y1, a*z1)

This is a simple way to scale a vector.

Unit vectors

Unit vectors are used to simplify many calculations. Our eye direction above could easily have been represented as (0,0,2.45) and the direction would have been exactly the same, but some calculations require a unit vector. To create a unit vector simply divide the vector by it's length. In this case, the length = sqrt(0*0+0*0+2.45*2.45) = 2.45. u = (0,0,2.45)/2.45 = (0/2.45,0/2.45,2.45/2.45) = (0,0,1). Any vector can be turned into a unit vector in this way. Many texts represent unit vectors with vertical bars |u|.

Dot product

The dot product can be used to calculate the angle between two vectors.

v * v should read as (v dot v)

dot = (x1,y1,z1) * (x2,y2,z2) = x1*x2 + y1*y2 + z1*z2

Note that this is a scalar product (not a vector, a simple number).

Here is where unit vectors come into play. If both vectors are unit vectors, then the dot product is the cosine of the angle between the vectors.

theta = invcos(A dot B)

Remember: where A and B are unit vectors.

This angle is useful in a thousand different ways. You can tell if two items are looking at each other or headed in the same direction by looking at this angle. If the angle is< 90 then they are not facing each other, if it is less than 90, then they are facing. It is also used to calculate lighting properties between the eye and a light in texturing.

Matrices and Transformations

Positions (and vectors) are simple 1x3 matrices.

A common math operation is to multiply a 1x3 matrix by a 3x3 matrix.

Another common operation is to multiply two 3x3 matrices.

Why all this matrix stuff?

Multiplying a 1x3 matrix by a 3x3 matrix has certain properties. You can create matrices to translate, scale and rotate as well as combine these together into a single matrix.

To translate.

To scale

To rotate around the x axis

You could create a single matrix by multiplying two rotation matrices, then multiply by a translation matrix and any points multiplied by that combined matrix would be rotated twice and scaled with a single calculation. Be careful, the order you create (multiply) the transformation matrices by is the same order that the point will be changed. A rotate and a translation is different than a translation and a rotation.

For more infromation, there are thousands of references and books, but a good source is this wikipedia page. The same matrix multiplaction system is used to convert the 3D definition of an object into a 2D drawing on a flat screen. That page talks about this projection onto 2D space so may not fully describe the process of matrix math. In general, this discussion is beyond the scope of this class, but necessary for a better understanding of TGE.

Cross product

The cross product is another math trick that is used extensively in 3d graphics. Given two vectors, this is the cross product:

(x1,y1,z1) x (x2,y2,z2) = (x1*z2 - z1*y2, z1*x2 - x1*z2, x1*y2 - y1*x2)

Take a linear algebra class to learn more about determinants and matrix math. Linear Algebra is used extensively in 3D and imaging and should high on your list of courses to take.

The cross product is a perpendicular vector between two other vectors. Note, there are two different directions the perpendicular vector could take depending on the vector order you create the cross product with.

One use of cross products is to determine the direction a surface or polygon is facing. Given a three vertex polygon and traversing the vertexes in clockwise order, you can create two vectors and take the cross product of those vectors. The result of this cross product is called the surface normal (or just normal) and is used extensively in all of the rendering operations. Take a class in 3D graphics to learn more.

Two types of 3d objects

This is out of place, but I wasn't sure where else it would fit.

.dts files

Typically smaller objects, vehicles and player avatars. These are created with Milkshape, Blender or 3DSM and exported to dts format

.map and .dif files

These are actual levels and buildings. If you can walk through it, use a map/dif file. These are created with QuArk.

Textbooks and how to use them.

The yellow book The Game Programmers Guide to Torque is a great reference, but poor at following an educational path or teaching other tools and gaming concepts.

The black book 3D Game Programming All In One has a good flow and covers all the topic including other required tools, but doesn't go into enough depth. You will need to have both books handy when programming torque.

TGE Documentation

You should first login to Garage Games to make sure this documentation is accessible.

Official Torque Game Engine Documentation. This is a great overview resouce and you should spend some time reading these documents.

There is also a large amount of TGE documentation on the TDN site.

Torque Script

You should have read Chapter 4 in the yellow book while working on TGB. At least most of the Torque language syntax should already be familiar to you by now.

Datablocks

Datablocks will be confusing, there is no doubt. There is a good overview on Datablocks and more indepth discussion on the TDN site. To understand them you have to understand the reasoning behind them. They conserve memory and were mostly used for the networking and rendering side of the TGE. Consider GTA. There might be 30 Ford Falcons on the map at any given time. It makes sense to only require one copy of the static Ford Falcon data, then reference that blob of data with a single number in each of the car instances. Static data would be things like maximum speed, turn radius, acceleration rate, deceleration rate, etc. That block (Data block) would be shared across all the instances. The dynamic data, current speed, acceleration, position etc would be kept in each individual object instance.

So, when Torque creates a new object to send to the client, it first checks to see if the client has received the datablock. If not, it gives the data(block) a unique number and sends the number and the datablock to the client. If the datablock has already been transferred, it simply sends the unique number to the client skipping a large data transfer and saving bandwidth.

Most of the explanations of datablocks really try to muddy this up a lot. Datablocks are just another class and have inheritance just like other classes. Don't be confused, it's just a class that has some special functionality so it can be shared between object instances.

You should read about Datablocks in the yellow book, pages 127-140. Table 4.4 on page 128 has a great example of this.

Some of this datablock stuff will take time to get used to. It would be best if you kept coming back to it over and over until you feel that you understand it.

Online class documentation

The online documentation for classes has a poor interface, but the documentation is there. You have to find the object you want, then click on the section and find that object again. I made a couple of changes to it so think linking was easier to use.

Core Classes

Yellow book pages 143-156.

SimObject P#143

This is the root object for everything in Torque. Every object instance has a name and an id. Names can be duplicated so id's should be used to access object instances since the id is unique.

SimDataBlock P#148

This is the root class for all datablock classes. Datablocks are paired with almost all GameBase derived classes.

SceneObject P#151

All objects that appear in your missions will be children of SceneObjects. This is where the items in your scene receive position and transformations. Notice that the transformation matrix does not match what we would expect. It is reformatted to make it easier for non-programmers to understand.

GameBase and GameBaseData P#155

These are objects that you should study in depth.

Torque Game Engine

You should probably make your own copy of c:\torque\SDK\examples directory on your own thumb drive. You can use this to keep you game separate from the rest of the system.

In general terms, you will start by clicking on the torqueDemo.exe file in the examples directory. This will parse the main.cs file (which lists tutorial.base as it's default) and run the game listed in $defaultGame. You can change this variable to try the other samples. Copy one of these sample directories to your own directory and make all of your changes in that new directory by changing the main.cs file.

The best way to start using Torque is to run through the Getting Started Tutorial. It should be located on your local machine at C:\Torque\SDK\example\GettingStarted.pdf.

CPSC240 - Games Development
Chapman University
Instructor: W. Wood Harter
(c) copyright 2006-2007 - W. Wood Harter - All Rights Reserved
Screen shots on banner (c) copyright their resprective owners