Video Transcript
Welcome to Visual Studio for gaming, how to get started developing 3D games. The purpose of this session is to provide an overview and introduction to developing games that are 3D using XNA Game Studio. To do that I’ve broken the session into three distinct components: The graphics, where we’ll talk about models and meshes, textures and shaders; The math, where we’ll try to wrap our heads around 3D space and the 3D pipeline; and finally The game itself, and that is the object model of the XNA Framework and what it looks like to manipulate the graphics and the math in the game that you create using XNA Game Studio.
The Graphics
Now models are the fundamental building blocks of 3D games that you build using XNA Game Studio. A model is simply an object that can be manipulated in 3D space and it can represent just about anything. It can be your character, it can represent the enemy character, it can represent animals, weapons, objects, dinosaurs, who knows. Everything can be represented as a 3D model. Models are actually simply collections of things called meshes. A mesh is a set of connected 3D points. A mesh consists of these points called vertices and the connections between those points called edges and faces, which is what gets created any time you have 3 or more edges that connect three or more vertices. So if you look on the right hand side, you can see for the same section of this dolphin model, we have vertices, edges and faces, what they look like conceptually.
Texture mapping is mechanism by which you can take an image and apply it to a model so that the model looks more realistic, more real world. Now the model itself sometimes may and sometimes may not contain texture data. Even if it doesn’t, XNA provides full support for texture mapping. Now, I’m not gonna get into the details of the bump maps, height maps and mitt maps. Rest assured that XNA provides full support for all of that. The thing that you need to understand sort of at the beginning is textures are simply images that get applied to models so that they look more realistic.
Whereas textures simply define sort of the basic look and feel of an object, the shaders are responsible for determining the actual color value of each vertex or pixel at render time. And if you think about a game in which you, as a player, move between indoors and outdoors, or move between scenes that are happening in the day time and at night time, you can easily imagine objects that if they looked exactly the same color inside and outside it would look unrealistic, right? An object that’s sitting out in bright sunlight will have a distinctly different look than one that’s in a dark cave. So, that’s what shaders do. Now, there’s shader functionality that’s built in to XNA so that when you’re just getting started you don’t really have to worry about shaders. However, once you get more advanced, you should know that XNA supports multiple types of shaders and it supports programmable shaders, using the high level shader language or HLSL.
Ultimately you need to create these models and you create models with 3D computer graphic software and XNA game studio doesn’t provide that functionality. It’s not designed to do that, it’s designed to write the code for the game, not to define the models for the game. Now there are many, many packages, I’ve listed a few here of the more major or well known ones. Obviously Max and Mia you’ve probably heard of are high end commercial suites for 3D computer graphics. Blender is notable simply because it’s open source and freely available and it has a lot of the same features as some of the high end packages.
Also, Soft Image XSI. And one thing to call out there is Soft Image XSI had mod tool software that is available for free download and you can use it to create non-commercial games. There’s also a special offer right now in which you can, if you’re a member of the XNA creator’s club and you have a premium membership, you can download Soft Image XSI mod tool and you can use it even to create 3D models for use in your commercial games that you export to Xbox Live marketplace.
Graphics Demo
So, let’s take a look using Soft Image XSI mod tool and how we could create and texture a very, very simple model. This is XSI 6 mod tool, and the purpose of this session really isn’t to talk too much about model creation. Just to kind of give you an idea of what this is, this is a tool that allows us to create 3D content and build models and create meshes. So for instance I can create a sphere, I can transform that, I can scale it in multiple directions. I can add additional content. Maybe in this case we’ll create a cone. We can change the shape of the cone, we can rotate.
So, there’s quite a bit we can do and of course that’s quite a bit beyond the scope of this particular exercise to understand what, or how these modeling tools work. And again, there’s multiple modeling tools each with their own functionality. What I’m gonna do is I’m gonna start a new scene and I’m going to simply create a grid or a plane that is going to sit on the, just flat on the main grid there. And then I’m going to add a texture and then we’ll grab an image. And we’ll go ahead and create a texture projection that applies to this plane. I can kind of get a preview of what it’s going to look like just to make sure that it is what I think it is and that is just planks of wood that are projected or textured onto this surface.
And once I’m done with that, I can go ahead and export this. So, what I’ll do is I’ll use the crosswalk functionality provided by mod tool and I’ll export to an FBX, which is one of the file formats that’s supported by XNA. So, we’ll create a model file called wood.fbx. And we’ll go ahead and scale this up and bit and click okay. And that’s it. So, we have created a very simple model, which we’ll use in the next demo.
The Math
Now once we have our graphic assets and that means textures and mitt maps and models and so forth, then we bring them in and import them into our Visual Studio XNA game project. And then we need to render them. Before we can actually get to the code, we need to understand the process of rendering 3D objects and it’s really about a series of transformations between various spaces. And often times you’ll see this process referred to as the 3D pipeline. And the 3D pipeline just to be sort of clear is handled slightly differently depending on the environment, the tool set, the renderer, the product, the package. There’s lots of variable that go into it. And that’s just sort of as a result of there being different algorithms being used, different processes. It’s really about how do we get, for the purposes we want, how do we get the 3D objects rendered as efficiently as possible in the way that makes the most sense?
And so you’ll see for instance in CAD applications where you have a high degree of accuracy is necessary, CAD applications will sacrifice frame rate in order to get accuracy because that’s the most important thing, is making sure that everything just as accurate as possible. Gains on the other hand, you’re willing to sacrifice accuracy where necessary in order to keep up your frame rate because a poor frame rate will hurt your game more than lack of accuracy or lack of detail. So, there are tricks, especially when it comes to game engines, there are lots of tricks to try to improve the speed at which the 3D pipeline can render content. But we’ll walk through the 3D pipeline as its sort of practiced or as it’s implemented by XNA. And again, this is still fairly high level in terms of an understanding of what everything is involved with the 3D pipeline.
So, I’m not gonna talk about this diagram that you’re looking at yet. Let’s first—we’ll come back to it after we sort of understand what all the different spaces and transformations mean. So, we talked about models and meshes all ready and I told you that a model contains meshes and the meshes are defined by vertices and then those vertices are connected via edges and the three edges make up a face, etc. But really the key factor in all of that are those single points, those vertices that make up the model and the mesh, the mesh is in the model.
Just remember first off that each point is going to have three values right? X, Y and Z coordinate. Now in XNA 3D world, X is going to go from left to right, the values of X are going to increase as you move to the right. Y is up and down, values of Y will increase as you go up. And then Z is forward and backward, the third dimension and the Z values will increase as it gets closer to you. And Z values will decrease as they get farther away.
So, the first space that we’re going to talk about is object space and the idea behind object space is that each model, no matter how simple or complex it is, is made up of a set of these points and these points are defined relative to the model’s origin. So, we start with the model’s origin and in many cases, although it doesn’t have to be, in many cases the origin is going to be the front left corner. So, let’s say, for the sake of argument, that in our cube is defined as having an origin at the lower left corner. So, that’s 0, 0, 0. If we move to the right, then our X value increases, but unless we move forward or backward, our Z doesn’t change and if we don’t move up and down our Y doesn’t change. So, moving over to the lower right hand corner, we have a point that’s defined relative to the model’s origin as one X value and zeros for Y and Z.
We can further define the remaining six points with these same coordinate values. And you can see that each time we’re just changing one of the values as we’re moving around the cube. And when we move backwards, it’s our Z value that changes. When we move up, it’s our Y value that changes. And so this cube has a set of points all defined relative to its origin, to itself. So, that’s object space. And when you hear the conversation around object space, it’s just relative to that single object itself.
Now, you want models to be able to move about in 3D and they can’t all be sitting at the same origin or they’d all be rendered at the same location, they’d all be on top of each other. You need them to be coming and going, interacting with each other, moving away from each other and so forth. And so they have to have a position that’s relative to some fixed location that we can understand. So, we’re gonna say that that fixed position is at 0, 0, 0 and we’ll imagine it being sort of in the center of this box. Now, this is just for illustration purposes. There is no finite limit in terms of the world space values, right? So, it’s not like at some point you’re going to fall off the map, etc. We’re just—for the purposes of defining some area, we’ll use this cube. But understand this cube doesn’t impose any sort of limits.
The idea is that we can place objects into this world space and as we do so, not only are we concerned about position, we’re also concerned about perhaps rotating those objects or even scaling those objects. So, we’ll want to be aware of that and you take the position, the rotation and the scaling, you put them altogether and you get something called a world transform matrix for each object. And an object that has had this transformation applied has now said to be in world space, ‘cause no longer is it sitting at 0, 0, 0. Now, it’s got some value to indicate its relative position in world space.
So, world space, another way to think about world space is that it represents the sort of 3D reality and the position of all of the different objects that you’re keeping track of in your game. Now world space represents that 3D reality, but in reality you can’t see everything. You can’t see objects that are behind you, you can’t see objects that are hidden from view by other objects. So, imagine a camera that’s located somewhere in world space. It’s got a position in world space and its facing some direction. And what we have to think about or imagine is what is it looking at, what does it see?
This camera is known as a view port. Now, what the 3D pipeline does is take that view port and it applies it to world space and what that does is defines a view matrix. And that orients all of the 3D points relative to the camera and at that point, the objects are said to be in view space. So, if you’re looking at the diagram, you can see that we’ve got this camera facing in a position facing a direction, it’s facing down to where we can see the blue cylinder in the red box. And if we imagine that we’re now looking through the camera, we now see this 3D space that is very much like our world space, only now everything has been reoriented relative to the camera. And so we can see that, as far as the camera is concerned, it’s the blue cylinder that’s closer, the red box is farther away and that orange, the orange rectangle is not even visible according to, from that point in the camera. And so when the objects are positioned relative to the camera, we now say that they are in view space.
But the problem with view space is it’s still 3D, points are still defined by three coordinates. And the reason that’s a problem is because our displays are not 3D. Our displays are two dimensional monitors, screens, and they only support X and Y coordinates. You can’t send three coordinate pixel to your display and expect it to actually render. You need to convert those 3D coordinates to 2D coordinates for rendering and that is handled by a project matrix. And projection matrix is influenced by perspective and field of view aspect ratio. And those things are applied to the view matrix and the result is a conversion of the 3D coordinates to 2D coordinates and we can say that those points are now in screen space. And once those points are in screen space, they’re 2D, which means they can be rendered out to the display as pixels.
And so you can see where it’s a whole set of transformations in order to determine where things are. And the goal of a good 3D pipeline is to do that as quickly as possible because you’re trying to keep the frame rate up and you’re having to perform these calculations, you know, 24 or 30 times a second in order to determine where everything is. So, at every chance, the pipeline will try to take shortcuts. And the goal of a good 3D pipeline is to take shortcuts without getting caught, so to speak. If you take a shortcut and you get caught, that’s when you have triangles or sections of the model are missing. You get clipping that occurs and we’ve all seen games where that happens and sometimes it happens just as part of the engine trying to keep the frame rate up right? A good 3D pipeline will try to avoid those types of things by making sure that everything it does is not going back to the visuals. So for instance it’s not going to render points that are not visible to the camera. And as soon as it can figure out that a point is not going to be visible on the screen, because it’s not visible to the camera, it’ll drop that point from processing and it’s one of the shortcuts that it takes.
So, the framework is going to do as much as it can to get your 30 points from object space all the way out to screen space. But it doesn’t know what you want to do. In other words, it doesn’t know where you want your objects or where you want your camera facing or do you want your camera to move or do you want that spaceship to be rotated or do you want it scaled up or down? And so, that’s your job as the game developer to tell the XNA Framework what you need it to do and that’s how you participate in this pipeline.
Now, the framework classes, there are many, but the ones that you should begin to understand to get started developing 3D games, the first one I mentioned before how model was the fundamental building block where there’s a model class in XNA that represents a 3D model. And there’s a mesh is property off of instances of model that represents the individual meshes and you’ll see shortly how those are used to draw objects.
If you’re familiar with 2D game development in XNA, then you’ve probably come across vector 2 quite a bit. And vector 2 represents a X, Y coordinate. Well, vector 3 represents an X, Y, Z coordinate, represents a vertex in 3D space. Vector 3s can also be used to store directionality ‘cause you can store the positive or negative direction in the X space and in the Y space and so forth.
Finally, a matrix. Now matrix is simply a vector that is used to transform points. And if you recall, it’s all about transforming points from one space to another, from object space in world space, from world space to view space, etc. So, matrices are extremely important. So important that it’s worthwhile spending time just talking about matrices.
Ultimately, every point that’s in your 3D space is ultimately going to be determined, in terms of its position by a world matrix, a project matrix and a view matrix. And if you recall the diagram before, those were the three matrices along the way that influenced the transformations from one space to another. So, ultimately after all of these things get applied, you eventually end up with the position of a point in screen space, and that’s what we’re really after because at the end of the day, no matter what else we do, we gotta put a pixel on the screen. Matrices are pretty nice because if you have multiple matrices, you can actually combine their effects simply by multiplying them and we’ll see an example of that in the sample.
So, dry models with XNA and the XNA Framework. There’s no one single way that you have to do it. And in fact the best approach is probably gonna depend on the style of game that you’re doing. In some cases, you’re gonna have a game with a fixed camera or a camera who’s position is fixed but may rotate. Other games are going to use a first person approach where the camera is moving all the time or a third person where you want the camera to move, but it’s sort of following a character, the player character around. So, depending on what type of game you have, different methods or styles of models are going to be suitable.
But the most basic is to simply use a nested loop in which the inner loop applies transformations to each effect in a mesh and the outer loop draws each mesh once the transformations are complete. And this is what the code looks like. So, we have an outer loop where we just iterate through each mesh in the model and inside that we look at each effect because there could be multiple effects that are applied to each mesh, for each effect we set the world matrix, the projection matrix and the view matrix. And within that outer loop, we wanna make sure that each mesh is drawn to the screen. That mesh.draw method, that’s a critical point. It looks very simple but at that point, that portion of the model, that mesh, is handed off to the game engine and it’s pretty much you don’t touch it again at that point because now it’s off to the 3D pipeline and from there on XNA takes over and ultimately puts it on the screen.
The Game
So, let’s take a look at what it looks like to create a simple game or 3D scene using XNA and some of these concepts that we’ve talked about. So, I’ve already started Visual C# 2008 Express edition. And I’m gonna say file, new project and I’ll select from XNA Game Studio 3.0 a Windows game and we’ll just call this demo 3D and click okay. Now, the first thing I’m gonna do is to actually import that model that I created. So I’ll right click on the content node in solution explorer, select add existing item and I’ll go out to the demo directory and find the model as well as the supporting texture.
I’m gonna add those and I’m gonna make a couple changes. First off, it’s gonna complain if I try to build at this point because the texture and the model both have the same name in terms of the asset name. So, for the textures since that’s gonna be sort of compiled into the model, it doesn’t need to be compiled at all. We’ll set the build action there to none. And the other thing that I’m gonna do is I’m going to modify for the content processor, I’m gonna modify the scale ‘cause I know that it’s gonna need to be scaled up a little bit. So, we’ll just make it 5 times bigger as it gets imported. This content processor, these properties allow you to make changes to your models on import, which is nice in case, especially if you’re getting models that you purchase or download or somebody else makes for you and they use a coordinate system that differs and you need to rotate it, or reverse it or something before you bring it into XNA.
Now I’m going to add some member variables to t0he game that is going to allow me to keep track of that particular model. So, right up here in the class, but outside of any method, I’ll define two member variables, a model called floor, our floor model, as it were, and a vector 3 that’s gonna represent the position of the floor and we’ll initialize that to be equal to vector 3.0. In other words, our floor is not going to move at this point. We’re just going to say that it’s located right in the center of the world, which would be the equivalent of saying vector 0, 0, 0.
Now before I can actually do any drawing, there’s still some more things I need to do and one of them is that I need to be aware of where my camera is and what direction it’s facing. So, I’m gonna create four member variables here that will store all my camera related data. One is a vector 3 that’s going to represent the camera position. And so we’ll create a new vector 3 to store that information and I’m gonna put it so that it’s directly in line with the center of the X-axis. But I’m going to put it about 30 units up and about 40 units closer so it’s going to be closer to the viewer than the center position. So, that’s our camera position.
And then I’m going to define the target, what do we want it to look at? Well, the camera target we can say that the camera target is also going to be vector 3.0. That just keeps things in our head sort of straight because the camera’s going to be sort of off to the side, but it’s going to be looking straight at the center of the world. Now, we’ll define two matrices, a matrix that is going to store the camera projection matrix, and a matrix that’s going to store the camera view matrix.
Now, we’ve got our member variables defined. We need to go initialize those matrices and also load the floor model. So, that’s all gonna happen in load content. So, we’ll just replace this comment with a few lines of code. First thing we’ll do is we’ll define the camera view matrix. And we’ll say that the camera view matrix is going to be defined by using the helper method. And by the way, the matrix class has a lot of static helper methods that define all sorts of ways to create new matrices so they’re very helpful. And in this case, we want the create look at method. And you can see that it takes a camera position, which we have, a camera target, which we have, and finally the direction that is up from the camera’s point of view. Well, luckily they have a little convenience vector for us, we just say vector 3.up and now we’ve got our camera view matrix.
Next up we’ll say that our camera projection matrix is going to be created using call to create perspective field of view. So another helper method. In this case, it takes a bunch of float values. The first float value being the field of view and so we can think of that as an angle. Most of the methods in XNA that deal with angles require radians as opposed to degrees. So, we’re probably more used to thinking in degrees. So, one of the things that comes in very handy is the two radians method from math helper and we just simply say that we want whatever value in radians is represented by 45 degrees in degrees.
So, it’s 45 degrees. Then we need the aspect radio. Well, we could take the width of the display or the view port and divide it by its height or we could just use our graphics device, get the view port and instead of dividing width by height, there’s a little convenience property called aspect ratio. And then finally we need to define the near plane and the far plane. And this is part of especially with respect to the far plane, this is one of those things where we’re sort of cheating by saying if it’s outside these bounds, if it’s too close or too far, then we don’t wanna display it, we don’t need to see it, and that will allow the 3D pipeline to save processing time by being able to ignore points that are outside these two values. So, we’re gonna say that the near plane distance is going to be one unit, and the far unit is going to be 10,000, which is pretty far away, but we should—we’re doing something simple enough to where that should be fine.
Finally, we need to load the content of that model, so we’re gonna call 4 = content.load. We’re loading an object of type model and it’s called would because that’s the asset name over in solution explorer that shows up in the property sheet. Okay, so we’ve done a lot of set up, but we haven’t actually gotten to the point where we’re drawing anything yet. And the simplest way to do that is to create a method that you can reuse, right? I could add the code directly into the draw method, but then I’d have to do that for every single model and so in an effort to be more reusable, we’ll create a method called draw model that’s gonna take the model that we wanna draw as well as a vector 3 that represents the position of that model.
So, in this method, we’re going to—this is where we write the code that we saw in the slides and that is the nested loop where we go through each model mesh object in model.meshes and then inside that loop, we loop through and we grab each basic effect in mesh.effects. And then for each effect, we need to set the world projection and view matrices. So, effect.world is equal to matrix and here’s another helper class. Create translation and we simply supply the model position, which is being passed in as an argument. And then we set the projection matrix, camera projection matrix, which we determine back in the load content method. And then finally effect.view, which also was determined or set up.
Now, in an effort to keep things simple, one of the things I’m doing here is I’m never going to move the camera, right? And in fact throughout this whole demo, the camera will remain in the same place. And it’s probably a good way to get started simply because if you have the camera moving and objects moving, it’s really hard to keep track of where everything is, just from an understanding standpoint in your 3D. So, a good way to get started is to think of a game where the camera doesn’t have to move and you can get a sense of just controlling the models and their position because, you know, especially the first time out, you know that’s plenty to keep track of.
Okay, so our camera and it’s view, the projection and view matrix associated with the camera will never, ever change. The only thing that’s gonna be modified is the position of the model and so at this point the model isn’t moving yet. So, we expect to see no motion, but we do have to draw the model. So, from the draw method, which just to refresh your memory it works the same way as in 2D games, once the game get started, it’s just an endless cycle of calls to update, calls to draw, call to update, call to draw and so forth. So, during the draw method, we’ll replace this line with a call to draw model and we’ll pass in the name of the model, which is floor and its position which is floor position.
One more thing we need to do, we need to actually draw the mesh. So, outside of the inner loop, we call mesh.draw and now let’s debug. So, there is the game, so to speak and at this point it’s just the texture that we created, or the model that we created with the texture that we supplied being shown and it’s the center of that model there is at 0, 0. So, we’re looking straight towards the center from our camera. Okay, it probably would be worthwhile making it look a little bit more interesting. So, I’m going to load another model, only this time, we’re gonna load a model that already exists rather than take time to make it. And so that is this little duck model. I’m gonna do the same trick as I did before to make sure that the action on the graphic file is none. And also, this happens to be a particular tiny model, so we need to scale this way up to about 4,000 in order for it to appear.
Now, I’m going to just copy what we’ve already done because we need a model for the duck and we need its position. We need to load it. So, we copy that code as well. And then finally, we call draw model and we pass in the duck and the duck’s position. So, nothing new here, other than we have now a model that’s being placed in the same location. And now we have the duck sitting in the center of our wood floor. Not too exciting. One thing though that you might notice is the lighting on the duck is pretty uniform and boring. And we can actually fix that with a very, very simple line of code. In fact just one line of code that we’ll do here, we’ll say that we want to enable default lighting and we’ll try it again, and we should see that immediately we get some shadows on the underside of the duck and it looks just a little bit more interesting. And of course if you want to use non-default lighting and create your own lighting effects, then that’s something that would be more advanced, but it’s certainly within, entirely within the realm of what you can do with XNA.
So now we have a duck and a floor, it’s time to make them move knowing and understanding what we know about 3D now and manipulating objects in 3D space. So, we’re going to make the duck move just up and down and we’ll do that by adding another vector 3 called duck motion and we’ll say that the duck motion is going to be straight up and down. So, just to recap, X coordinate, it’s not going left to right, Y coordinate it is going in a positive direction, which means it’s going to be floating upwards and it’s not going anywhere in the Z direction which means it’s not coming towards us and it’s not going away from us. So, that’s the motion.
I’m also going to define as a very simple way of handling gravity another vector. And we’ll say that gravity also goes—does not go towards us or from us, and it doesn’t go left to right, but it does go up and down. So, this is going to be 0.01 units. And now we have a simple value that represents gravity. And now what we can do is in the update method down here, we can replace this comment with a call to modify the duck’s position. So, we’ll say duck position is going to be modified by duck motion. And that’s the nice thing, you can just add vectors and have the results applied to the original vector.
And then we will check to make sure that the duck is not falling off of—below the floor. So, we’ll say if duck’s Y position, if it’s less than 0, then we’re gonna wanna make sure that it doesn’t go below that, so we’ll reset it to 0. And we also want to reverse the motion. So, if the duck is coming down at a certain velocity shall we say, then we will invert that. It’s likely to be—we’d expect it to be a negative number, but getting the absolute value, that’ll make sure that it’s positive number so the duck is going back forward already.
We want to apply gravity to give us sort of some smoothness to this. So, we’ll say duck motion will add gravity to that. And now with those changes, we should see that in our game the duck is going to bounce up and down a little bit. So now the camera’s in the same place, it’s facing the same direction, the only thing we’re manipulating is the model position and the model position is being affected by these vectors.
Now let’s talk about rotation ‘cause that’s one other thing that we can do without getting too deep into the math is to simply rotate a model. Well, we can say, for instance, and let’s just create a copy in fact, let’s create a copy of draw model here. In this draw model, this copy is going to have—take a third parameter that’s gonna be a matrix that represents the rotation. Now, here’s the beauty of the matrices is if I want to change the world transformation matrix of our model and I want it to rotate, all I need to do is to take a matrix that contains or defines the rotation and multiply that by the matrix that already represents where in world’s space the model is. So, simply put, I just add a multiplication here of the existing matrix, multiplied by the rotation and that gives me the new matrix.
And I’m actually going to do this on the floor, instead of rotating the duck. We’re already moving the duck, let’s rotate the floor. So, I’m gonna go back up here to where I’ve defined my floor model and vector and I’ll add a few more variables. One will be a matrix that represents the floor rotation. And then I’ll have two floating point values that represents the rotation in the X direction and the rotation in the Z direction and those are going to be used. In the beginning they’re not gonna be rotating any at all, but you’ll see that I can modify these values, create a new matrix and the result will cause the model to rotate.
Okay, so I’m now going to override or change the draw model so that when we draw the floor, in addition to floor position we pass in the floor rotation. And I can run it at this point, but because I haven’t modified this rotation, nothing’s gonna happen. So, what I need to do is over in the update method, I’m going to handle the floor rotation part. And just so we’re keeping things straight, this is move the duck, okay. So, handling the floor rotation, we’re going to actually use the controller for this. So, I’ll do first modify floor rotation X by modifying its value, taking its value and then subtracting the value of game pad from player 1 and we’re going to get the left thumb sticks left value and we’re going to divide it by 2, right?
And then I’m going to do some sort of sanity checks, ‘cause I don’t wanna be able to rotate the floor too much. So, I’m going to check to see if floor rotation is less than negative 5, then we’ll set it to negative 5, so that’s the limit. And if it’s greater than 5, that’s the upper limit. And then we’ll do the same thing for floor rotation Z. Only this we’re going to use the X axes of the left thumb stick. And we’re dividing by 2, so it doesn’t go as fast. And then this is gonna affect floor rotation Z. Aside from that, all the logic is the same.
And then finally, we have to create our matrix. So, floor rotation, we’re going to call matrix.create rotation X and we’ll do that using our math helper again, where we call two radians and we pass in floor rotation X. And again any time we need to combine matrices, we just multiply them. So, we’ll multiply this by matrix.create rotation Z, again using math helper for floor rotation Z. Now what we’re doing is, based on the movement of the left thumb stick, we’re modifying the rotation up to a point.
And one final thing that we need to do is we’d like the duck to react to where the floor is rotating. So, I’m just going to add modifications here to the duck motion X and Z values. So, we’re going to say that based on floor rotation Z divided by 100, that’s how we’re gonna modify the X value and then floor rotation X will modify the Z value. And let’s go ahead and try it out. So, this is our finished product, so to speak. We should now have our bouncing duck and we can move our controller to the left or to the right or forward and backward and it’s going to keep the—rotate the floor model, which will influence based on some, you know, very, very simple, you know, simplistic calculations will influence the direction that the duck bounces, right?
So, you can see that this is nowhere near a finished game, but it’s sort of the starting point and hopefully you will have seen here an example of—enough to get you started in the world of 3D programming with XNA Game Studio. So, as we saw 3D games start with 3D graphics. So, you need to have an understanding of how your going to create and get your models, textures, etc. We saw a quick example, but obviously it wasn’t about the model creation so much, as understanding what they are. But you’ve seen a number of places or software packages that will let you create models.
I also wanna note that there are model warehouses available that you can purchase models and some places you can download models for free, especially ones to just get you started. XNA Game Studio and Visual Studio doesn’t have model creation facilities, but again there are plenty of programs that do. Once you’ve got your graphic assets, then it’s time to start building your game and the XNA Framework provides that 3D graphics pipeline that we talked about that’s required to create 3D games. You will need an understanding of 3D space and there’s a bit of a learning curve there, and of course there is—you need a basic understanding of the XNA object model, but hopefully what you’ve seen in this session is how it really is attainable to be able to start developing 3D games takes to XNA Game Studio 3.0.
[End of Audio]