XBOX 360 E User Guide
Have a look at the manual XBOX 360 E User Guide online for free. It’s possible to download the document as PDF or print. UserManuals.tech offer 11 XBOX manuals and user’s guides for free. Share the user manual or guide on Facebook, Twitter or Google+.
GOING BEYOND—MAKING YOUR MODEL MOVE USING INPUT Step 1: Connect Your Xbox 360™Controller The first step in this tutorial is making sure you can provide some input to your game. We’ll be using the Xbox 360 Controller throughout this tutorial. Designed for use with both a Windows computer and an Xbox 360, the controller features many analog and digital inputs, as well as vibration motors to give the user feedback.There are other ways to take input—the XNA Framework has support for keyboard and mouse devices. Mouse devices are only supported on Windows. To follow along with us, connect your Xbox 360 Controller and get ready to code! Step 2: Create Variables to Turn and Move the Model We want our ship to move around on the screen, so we need to create some variables to track our model’s position and orientation in the world.Fortunately, from our last tutorial, we have two variables to do just that: modelPosition, which is a 3-D vector; and modelRotation, which is a floating-point value. Currently, this system allows three degrees of translation (changing position in the world) but only one degree of rotation (changing orientation). For this demonstration, we use that limitation to simplify our input. In many 3-D games, there are three degrees of translation and three degrees of rotation, but this is a good start. To make input a little more interesting, what we can do right now is add another vector for velocity. By updating the position with the velocity of each frame, our 3-D model can accelerate and decelerate smoothly. Let’s try it: > Make sure your project from the “Going Beyond—3-D Models” section is open. If it isn’t, open it by clicking Open Project on the File menu, and then browsing to your project. > View the code by double-clicking Game1.cs in Solution Explorer. > In the code, find the Update method. Modify it to look like the sample at the top of the next page: 176 Xbox 360™Han dbook effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(modelPosition); effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); } //Draw the mesh; will use the effects set above. mesh.Draw(); } } } C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
The code you’ve just added to input runs every frame and does a few different things. First, it gets rid of the code that automatically rotates the ship; you’ll be controlling that with your controller. Next, it calls a method named UpdateInput, which you’ll have to create in the next step. Last, it adds our model’s velocity to its position, moving it in the world by its velocity, and decays the velocity so that eventually the model slows down. Step 3: Take Input from the User Now that the model is set up to move with velocity, you must provide some logic that changes the velocity based on controller input. A simple system that we can use is an orientation thrust method; in essence, you can point the front of your model in different directions using your controller’s stick, then apply thrust in the direction you point with your controller’s trigger. By building up thrust in a direction, the model begins to move. This is similar to how the ships in the Spacewar Starter Kit move. We can map our controls to the game this way: > Pressing left or right on the left stick increases or decreases the value of the modelRotation variable. > Pulling the right trigger adds a vector in the direction of our modelRotation variable to our modelVelocity vector. > Pressing 1resets the position, velocity, and rotation values of the model to “warp” the ship back to the screen’s center. Let’s code it! 177 Welcome to XNA™ //Velocity of the model, applied each frame to the model’s position Vector3 modelVelocity = Vector3.Zero; protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit(); //Get some input UpdateInput(); //add velocity to current position modelPosition += modelVelocity; //bleed off velocity over time modelVelocity *= 0.95f; base.Update(gameTime); } C# Code On the Xbox 360 Controller, both the triggers and the sticks are analog controls, meaning that they can report their movements in varying amounts, rather than just on or off; all other buttons are digital. NOTE Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
> Find some empty space in your code below the Update method. > Add a new method called protected void UpdateInput(). Modify the method to look like this: That method does a lot. Let’s take it piece by piece to investigate exactly what you’re doing with input and the model: //get the gamepad state GamePadState currentState = GamePad.GetState(PlayerIndex.One); 178 Xbox 360™Han dbook protected void UpdateInput() { //get the gamepad state GamePadState currentState = GamePad.GetState(PlayerIndex.One); if (currentState.IsConnected) {//rotate the model using the left stick; scale it down modelRotation -= currentState.ThumbSticks.Left.X * 0.10f; //create some velocity if the right trigger is down Vector3 modelVelocityAdd = Vector3.Zero; //find out what direction we should be thrusting, using rotation modelVelocityAdd.X = -(float)Math.Sin(modelRotation); modelVelocityAdd.Z = -(float)Math.Cos(modelRotation); //now scale our direction by how hard the trigger is held down modelVelocityAdd *= currentState.Triggers.Right; //finally, add this vector to our velocity modelVelocity += modelVelocityAdd; GamePad.SetVibration(PlayerIndex.One, currentState.Triggers.Right,currentState.Triggers.Right); //in case you get lost, press [[A]] to warp back to the center if (currentState.Buttons.A == ButtonState.Pressed) { modelPosition = Vector3.Zero; modelVelocity = Vector3.Zero; modelRotation = 0.0f; } } } C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
This call to GetState retrieves a GamePadState object, which contains the information we need about the controller—in this case, stick and trigger positions. //rotate the model using the left stick; scale it down modelRotation -= currentState.ThumbSticks.Left.X * 0.10f; Retrieving the X-axis value of the left stick (left and right movement) returns a value that is added to the modelRotation variable. The value is scaled down so that the rotation isn’t too fast. //create some velocity if the right trigger is down Vector3 modelVelocityAdd = Vector3.Zero; //find out what direction we should be thrusting, using rotation modelVelocityAdd.X = -(float)Math.Sin(modelRotation); modelVelocityAdd.Z = -(float)Math.Cos(modelRotation); //now scale our direction by how hard the trigger is held down modelVelocityAdd *= currentState.Triggers.Right; A little math here helps translate the rotation of the ship into a vector. Taking the sine value of the rotation gives us the proper amount of X(left and right) movement, and the cosine gives us the Z(forward and back) movement. Then, we take the vector and lengthen it by how hard the player pulls the right trigger. //finally, add this vector to our velocity modelVelocity += modelVelocityAdd; Finally, the created vector is added to the current velocity vector to create the final velocity vector applied to move the model around. GamePad.SetVibration(PlayerIndex.One, currentState.Triggers.Right, currentState.Triggers.Right); We’re using the right trigger values to give some feedback to the player with the Xbox 360 Controller vibration motors, using SetVibration. The Xbox 360 Controller has two motors that run at different speeds, so experiment to find the best combination for the action that’s happening in the game. //in case you get lost, press 1to warp back to the center if (currentState.Buttons.A == ButtonState.Pressed) { modelPosition = Vector3.Zero; modelVelocity = Vector3.Zero; modelRotation = 0.0f; } This little extra moves the model back to its original position and orientation in case it leaves the screen. 179 Welcome to XNA™ Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
Congratulations! At this point, your ship moves and gives you feedback through your Xbox 360 Controller. The player is in control of the action.When you’re ready, let’s add the final element—audio—to get you on your way. Once you can control the action and can see and hear the results of your actions, you’re well on your way to creating a game. Ideas to Expand Want to play around some more with input? Try these ideas: > Change the game to view your model from the top, as in a top-down arcade game. (Hint: Play with the camera position vector. Note that you can’t set it exactly up and down because the camera vector cannot be the same as the “up” vector.) > Scale the vibration to occur more powerfully as the ship approaches the viewer. (Hint: Use the distance between modelPosition and cameraPosition.) > Try using a keyboard to control the ship. (Hint: You can plug a USB keyboard into your Xbox 360.) The Complete Example 180 Xbox 360™Han dbook #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; #endregion public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; public Game1() {graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void Initialize() { base.Initialize(); C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
181 Welcome to XNA™ } //3d model to draw Model myModel; protected override void LoadGraphicsContent(bool loadAllContent) {if (loadAllContent) {myModel = content.Load(“Content\\Models\\p1_wedge”); } } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent == true) {content.Unload(); } } //Velocity of the model, applied each frame to the model’s position Vector3 modelVelocity = Vector3.Zero; protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit(); //Get some input UpdateInput(); //add velocity to current position modelPosition += modelVelocity; //bleed off velocity over time modelVelocity *= 0.95f; base.Update(gameTime); } protected void UpdateInput() { //get the gamepad state GamePadState currentState = GamePad.GetState(PlayerIndex.One); C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
182 Xbox 360™Han dbook if (currentState.IsConnected) {//rotate the model using the left stick; scale it down modelRotation -= currentState.ThumbSticks.Left.X * 0.10f; //create some velocity if the right trigger is down Vector3 modelVelocityAdd = Vector3.Zero; //find out what direction we should be thrusting, using rotation modelVelocityAdd.X = -(float)Math.Sin(modelRotation); modelVelocityAdd.Z = -(float)Math.Cos(modelRotation); //now scale our direction by how hard the trigger is down modelVelocityAdd *= currentState.Triggers.Right; //finally, add this vector to our velocity modelVelocity += modelVelocityAdd; GamePad.SetVibration(PlayerIndex.One, currentState.Triggers.Right,currentState.Triggers.Right); //in case you get lost, press Ato warp back to the center if (currentState.Buttons.A == ButtonState.Pressed) { modelPosition = Vector3.Zero; modelVelocity = Vector3.Zero; modelRotation = 0.0f; } } } //Position of the model in world space, and rotation Vector3 modelPosition = Vector3.Zero; float modelRotation = 0.0f; //Position of the camera in world space, for our view matrix Vector3 cameraPosition = new Vector3(0.0f, 50.0f, -5000.0f); //Aspect ratio to use for the projection matrix float aspectRatio = 640.0f / 480.0f; protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); //Copy any parent transforms C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
GOING BEYOND—AUDIO Step 1: Get Some Wave Files Audio in XNA Game Studio Express is created using the Microsoft Cross-Platform Audio Creation Tool (XACT). Wave files (.wav) are assembled into an XACT project and built into wave banks that are loaded into your game.The first thing to do is get some wave files. This is where the Spacewar project you created in “Going Beyond—3-D Models” comes in handy. Let’s go get the wave files from that project: > Make sure your project from “Going Beyond—Making Your Model Move Using Input” is open. If it isn’t, select Open Project from the File menu, and then browse to your project. > In the Solution Explorer, right-click on the Content folder, select Add, and then select New Folder. Name this folder “Audio.” > Right-click on the Audio folder you just created, select Add, and then select New Folder. Name this folder “Waves.” > Open Windows Explorer. Browse to the Spacewar Starter Kit project folder, into its Content\Audio\Waves folder. Inside that folder are several subcategories of folders, containing many sounds for you to use. > Drag and drop these two files from the Spacewar Waves folder to the Content\Audio\Waves folder in Windows Explorer: Ships\engine_2.wav, and Weapons\hyperspace_activate.wav. 183 Welcome to XNA™ Matrix[] transforms = new Matrix[myModel.Bones.Count]; myModel.CopyAbsoluteBoneTransformsTo(transforms); //Draw the model; a model can have multiple meshes, so loop foreach (ModelMesh mesh in myModel.Meshes) { //This is where the mesh orientation is set, as well as our camera and projection foreach (BasicEffect effect in mesh.Effects) {effect.EnableDefaultLighting(); effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation)* Matrix.CreateTranslation(modelPosition); effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); } //Draw the mesh; will use the effects set above mesh.Draw(); } base.Draw(gameTime); } } C# Code Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
Step 2: Create an X ACT™Project with Wave Files If you’ve noticed that you don’t add wave files like other content files (i.e., via the Solution Explorer), you’re right—the Content Pipeline processes XACT projects, which are compilations of wave files and not just the raw wave files themselves. We must now create that compilation using the Microsoft Cross-Platform Audio Creation Tool (XACT).Let’s launch XACT: > From your Start menu, browse to All Programs, then Microsoft XNA Game Studio Express, and then Tools. > Click Microsoft Cross-Platform Audio Creation Tool (XACT). > XACT launches and immediately opens a new project. Once you see the XACT window, save this new project to your game project folder in the Content\Audio folder: > From the XACT window, click the File menu, and then click Save Project As. > From the dialog box that appears, browse to your project folder, and then into the Content\Audio folder. > Type a name for your project. Use “MyGameAudio.” > Click OK. The project is saved in your game project folder under Contents\Audio. Later, we will load this XACT project through the Content Pipeline. First, let’s put our wave files in the project so that when the XACT project loads in our game, there are sounds to play: > The screen contains an empty project. In the empty project, you must create a wave bank by right-clicking Wave Banks, and then clicking New Wave Bank. A new wave bank appears in the tree view under Wave Banks with the default name Wave Bank. > Create a new sound bank. To do this, right-click Sound Banks, and then New Sound Bank. A new sound bank appears in the tree view under Sound Banks with the default name Sound Bank. > At this point, two new windows have appeared: one for the wave bank and one for the sound bank. To arrange these windows for easier viewing, click Window, and then click Tile Horizontally. The win- dows should now look similar to the following: 184 Xbox 360™Han dbook Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.
> Add both of your wave files to the wave bankwindow. Click on the wave bank window to make sure it is active, then right-click Wave Banks, and then click Insert Wave File(s). Browse to your game project folder, and into the Content\Audio\ Waves folder. Select both wave files. If you suc- cessfully added them, they appear in the wave bank window, which looks similar to the screen directly to the right: > For each wave listed in the wave bank window, drag the wave from the wave bank window to the sound bank window, and drop it on top of the Cue Name panel. XACT automatically creates a new cue that is linked to a new sound that plays this wave file. It should look similar to the screen to the right: > If the bottom-left corner of the sound bank window does not have any entries, you must add entries by dragging each sound from the sound bank’s upper-left panel to its lower-left panel. This action creates cues that correspond to the sounds. > Before saving, make sure our engine_2 sound loops when we play it; we don’t want it to just play once and stop. Click on engine_2 in the sound bank’s top-left panel. When you do this, a tree structure appears in the sound bank’s top-right panel. > In the sound bank’s top-right panel, click the Play Wave item. Notice that the property pane in the far lower-left panel of your XACT window changes to list a set of properties. > In this property panel, find the LoopEvent property. Set it to Infinite. It should look something like the screen to the right: > Save the XACT project by selecting the File menu, and then selecting Save Project. 185 Welcome to XNA™ Protected by copyright. Unauthorized or unlawful copying or downloading \ expressly prohibited.