Subscribe

RSS Feed (xml)

Powered By

Powered by Blogger

Google
 
xnahelp.blogspot.com

Kamis, 03 April 2008

Creating 3D Objects

In this chapter we examine 3D concepts and how the
XNA Framework exposes different types and objects that
allow us to easily create 3D worlds. We will create a couple
of 3D demos that explain the basics. We will also create 3D
objects directly inside of our code. Finally, we will move
these objects on the screen.
Vertices
Everything in a 3D game is represented by 3D points. There
are a couple of ways to get 3D objects on the screen. We
can plot the points ourselves or we can load them from a
3D file (which has all of the points stored already). Later, in
Chapter 6, “Loading and Texturing 3D Objects,” we will
learn how to load 3D files to use in our games. For now,
we are going to create the points ourselves.
We defined these points with an x, y, and z coordinate (x,
y, z). In XNA we represent a vertex with a vector, which
leads us to the next section.
Vectors
XNA provides three different vector structs for us—
Vector2, Vector3, and Vector4. Vector2 only has an x
and y component. We typically use this 2D vector in 2D
games and when using a texture. Vector3 adds in the z
component. Not only do we store vertices as a vector, but
we also store velocity as a vector. We discuss velocity in
Chapter 19, “Physics Basics.” The last vector struct that
XNA provides for us is a 4D struct appropriately called
Vector4. Later examples in this book will use this struct to
pass color information around as it has four components.
We can perform different math operations on vectors,
which prove to be very helpful. We do not discuss 3D math
in detail in this book as there are many texts out there that
60 CHAPTER 4 Creating 3D Objects
cover it. Fortunately, XNA allows us to use the built-in helper functions without having to
have a deep understanding of the inner workings of the code. With that said, it is
extremely beneficial to understand the math behind the different functions.
Matrices
In XNA a matrix is a 4x 4 table of data. It is a two-dimensional array. An identity matrix,
also referred to as a unit matrix, is similar to the number 1 in that if we multiply any
other number by 1 we always end up with the number we started out with (5 * 1 = 5 ).
Multiplying a matrix by an identity matrix will produce a matrix with the same value as
the original matrix. XNA provides a struct to hold matrix data—not surprisingly, it is
called Matrix.
Transformations
The data a matrix contains are called transformations. There are three common transformations:
translation, scaling, and rotating. These transformations do just that: They transform
our 3D objects.
Translation
Translating an object simply means we are moving an object. We translate an object from
one point to another point by moving each point inside of the object correctly.
Scaling
Scaling an object will make the object larger or smaller. This is done by actually moving
the points in the object closer together or further apart depending on if we are scaling
down or scaling up.
Rotation
Rotating an object will turn the object on one or more axes. By moving the points in 3D
space we can make our object spin.


Transformations Reloaded

tag: transformation,reloaded,xna

An object can have one transformation applied to it or it can have many transformations
applied to it. We might only want to translate (move) an object, so we can update the
object’s world matrix to move it around in the world. We might just want the object to
spin around, so we apply a rotation transformation to the object over and over so it will
rotate. We might need an object we created from a 3D editor to be smaller to fit better in
our world. In that case we can apply a scaling transformation to the object. Of course, we
might need to take this object we loaded in from the 3D editor and scale it down and
rotate it 30 degrees to the left so it will face some object and we need to move it closer to
the object it is facing. In this case we would actually do all three types of transformations
to get the desired results. We might even need to rotate it downward 5 degrees as well,
and that is perfectly acceptable.
We can have many different transformations applied to an object. However, there is a
catch—there is always a catch, right? The catch is that because we are doing these transformations
using matrix math we need to be aware of something very important. We are
multiplying our transformation matrices together to get the results we want. Unlike
multiplying normal integers, multiplying matrices is not commutative. This means that
Matrix A * Matrix B != Matrix B * Matrix A. So in our earlier example where we want to
scale our object and rotate it (two different times) and then move it, we will need to be
careful in which order we perform those operations. We will see how to do this a little
later in the chapter.

0 komentar: