Creating a simple 3d game with xna


















We're going to load a 3D model of a room with four corners and some fantastic abstract art hanging on the walls. FBX file format. Once you've got your. Simply right-click on your game project, Add Existing Item Now, we've got our 3D model inside our project, we can load it through code. Right after we create our camera, add these two lines:.

Now, we have some pretty things to look at. But, it's not exactly action-packed alien destruction, yet. Let's make it so the player can move around when you press keys on the keyboard. Let's build that now.

With XNA, you update your game state like player movement inside a special function called Update. Here's what it looks like:. Inside of that special function, let's check if the user pressed the W, A, S, or D keys and move the camera accordingly. Inside that function, just above the base. Update gameTime ; line, add the line ProcessKeyPresses ;. Pretty simple code: all we're doing here is just adjusting the camera's position based on what keys were pressed. End result?

Joystick Movement. By just reading the state of the pad, you can get button information and thumbstick positions with a few easy to use static methods.

In keeping with the keypad control, we decided to use the left thumbstick to move forward and backward and the right thumbstick to rotate clockwise and counterclockwise.

What is nice about the joystick classes in XNA is the code is almost self-explanatory. You can get the state of a particular button and see if it was pressed or released, or you can get the state of a thumbstick Left or Right and see its X and Y position vector. In listing 4 we round the left thumbstick's X vector positions in order to determine if we are moving forward or backward. A value of 1 indicates forward and a value of -1 indicates backward.

We use the same technique for rotation. We round the right thumbstick's X vector positions in order to determine if we are rotating clockwise or counterclockwise.

A value of 1 indicates clockwise and a value of -1 indicates counter clockwise. Listing 4 - Reading the Joystick State. GetState PlayerIndex. Update 0, 0,. Update 0, 0, -. Angle; Sound. Drawing Fonts. Fonts are handled much in the same way as textures. You add a SpriteFont to the content of your project. You then load the font using the ContentManager. Finally you draw the font in the Draw routine of your game object.

Start by right clicking on your project in solution explorer and choose, Add New Item. Pick the Sprite Font from the Content Dialog shown in figure 2: Figure 2 Adding a new content Item This will produce an xml file that defines your font.

Go into the content file and edit the file for the font parameters needed to customize the appearance of your font. There are also some lines of generated code which handle the game pad keys and a few other things - but once again for this simple example we can ignore them.

Building the cube. Buidling the faces. More faces. The effect. Projection and view transformations. Improve this question. Guapo Guapo 4 4 silver badges 10 10 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Ricket Ricket You must not have noticed it but this is indeed a tutorial!

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.



0コメント

  • 1000 / 1000