Download presentation
Presentation is loading. Please wait.
Published byBarnard Williams Modified over 9 years ago
1
11 Adding Vibration Effects Session 3.2.2
2
Session Overview Describe how the vibration feature of the gamepad works Show how an XNA program can control gamepad vibration Add vibration to the Color Nerve game Look at how we can change the behavior of a game by modifications to the code Describe how the vibration feature of the gamepad works Show how an XNA program can control gamepad vibration Add vibration to the Color Nerve game Look at how we can change the behavior of a game by modifications to the code Chapter 3.2.2: Adding Vibration Effects2
3
The Xbox Gamepad Vibration The gamepad contains two motors which turn weighted flywheels The wheels turn at different speeds to vibrate the pad Games can control the power going into the motors to get different levels of vibration The gamepad contains two motors which turn weighted flywheels The wheels turn at different speeds to vibrate the pad Games can control the power going into the motors to get different levels of vibration Chapter 3.2.2: Adding Vibration Effects3
4
Vibration Speeds The motor on the left provides low-frequency rumble The motor on the right provides higher-frequency vibration XNA allows you to control the power going to either of these motors from a C# program The motor on the left provides low-frequency rumble The motor on the right provides higher-frequency vibration XNA allows you to control the power going to either of these motors from a C# program Chapter 3.2.2: Adding Vibration Effects4
5
Controlling Gamepad Vibration from XNA We have used the GamePad class before It provides the getState method that we used to read the gamepad state It also provides a method called setVibration which is used to control the vibration of a gamepad The method is given three parameters A parameter is a way of feeding information into a method We have used the GamePad class before It provides the getState method that we used to read the gamepad state It also provides a method called setVibration which is used to control the vibration of a gamepad The method is given three parameters A parameter is a way of feeding information into a method Chapter 3.2.2: Adding Vibration Effects5 GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;
6
Selecting the Gamepad to Be Controlled This parameter value identifies the gamepad to be controlled We used it to tell getState which gamepad to read You can try to control gamepads that aren’t there, but the method call won’t do anything This parameter value identifies the gamepad to be controlled We used it to tell getState which gamepad to read You can try to control gamepads that aren’t there, but the method call won’t do anything Chapter 3.2.2: Adding Vibration Effects6 GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;
7
Setting the Vibration Levels This parameter value gives the amount of vibration the left (low frequency) motor should produce It is given as a floating point value between 0 and 1 If we give the value 1 it will produce maximum vibration If we give the value 0 the vibration is turned off This parameter value gives the amount of vibration the left (low frequency) motor should produce It is given as a floating point value between 0 and 1 If we give the value 1 it will produce maximum vibration If we give the value 0 the vibration is turned off Chapter 3.2.2: Adding Vibration Effects7 GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;
8
Setting the Vibration Levels This parameter value gives the amount of vibration the right (high frequency) motor should produce It is given as a floating point value between 0 and 1 It works in the same way as the left-hand value, but the vibration effect is different Once you have set a vibration level the pad will continue to vibrate until you tell it something different This parameter value gives the amount of vibration the right (high frequency) motor should produce It is given as a floating point value between 0 and 1 It works in the same way as the left-hand value, but the vibration effect is different Once you have set a vibration level the pad will continue to vibrate until you tell it something different Chapter 3.2.2: Adding Vibration Effects8 GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;
9
Vibration Demonstration Chapter 3.2.2: Adding Vibration Effects9 protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.X == ButtonState.Pressed) GamePad.SetVibration(PlayerIndex.One, 1, 0); base.Update(gameTime); } protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.X == ButtonState.Pressed) GamePad.SetVibration(PlayerIndex.One, 1, 0); base.Update(gameTime); }
10
1. Vibration Demonstration Chapter 3.2.2: Adding Vibration Effects10 This program implements the vibration behavior that we have just seen However, it does have a problem… This program implements the vibration behavior that we have just seen However, it does have a problem…
11
Making the Vibration Stop The game program must tell the gamepad to stop vibrating when the button is not pressed The easiest way to achieve this is to add an else part to the conditional statement This version of the code will only make the gamepad vibrate when the Blue button is pressed The game program must tell the gamepad to stop vibrating when the button is not pressed The easiest way to achieve this is to add an else part to the conditional statement This version of the code will only make the gamepad vibrate when the Blue button is pressed Chapter 3.2.2: Adding Vibration Effects11 if (pad1.Buttons.X == ButtonState.Pressed) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0); if (pad1.Buttons.X == ButtonState.Pressed) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0);
12
Adding Vibration to Color Nerve The Color Nerve program could make the gamepad vibrate when the intensity values get close to wrapping round The condition must compare two values and trigger when one is greater than the other We can use the “greater than” operator to do this The Color Nerve program could make the gamepad vibrate when the intensity values get close to wrapping round The condition must compare two values and trigger when one is greater than the other We can use the “greater than” operator to do this Chapter 3.2.2: Adding Vibration Effects12 if (redIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0); if (redIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0);
13
The Greater Than Operator The Greater Than operator can be placed between two numeric values It returns true if the number on the left is greater than the number on the right In all other situations (including equals) it returns false The Greater Than operator can be placed between two numeric values It returns true if the number on the left is greater than the number on the right In all other situations (including equals) it returns false Chapter 3.2.2: Adding Vibration Effects13 if (redIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0); if (redIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0);
14
Combining Tests If we want the vibration to start when any of the color intensities exceeds 220 we have to test all of them We can combine the results using logical OR, so that the vibration starts if any of them exceeds the limit If we want the vibration to start when any of the color intensities exceeds 220 we have to test all of them We can combine the results using logical OR, so that the vibration starts if any of them exceeds the limit Chapter 3.2.2: Adding Vibration Effects14 if (redIntensity > 220 || greenIntensity > 220 || blueIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0); if (redIntensity > 220 || greenIntensity > 220 || blueIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0);
15
Using Blocks to Improve Code Appearance Creating blocks can make your code clearer In this case it separates the code from the conditions Creating blocks can make your code clearer In this case it separates the code from the conditions Chapter 3.2.2: Adding Vibration Effects15 if (redIntensity > 220 || greenIntensity > 220 || blueIntensity > 220) { GamePad.SetVibration(PlayerIndex.One, 1, 0); } else { GamePad.SetVibration(PlayerIndex.One, 0, 0); } if (redIntensity > 220 || greenIntensity > 220 || blueIntensity > 220) { GamePad.SetVibration(PlayerIndex.One, 1, 0); } else { GamePad.SetVibration(PlayerIndex.One, 0, 0); }
16
2. Color Nerve with Vibration Chapter 3.2.2: Adding Vibration Effects16 This version of the game provides vibration feedback when any of the color intensity values exceeds 220
17
Summary The Xbox gamepad has two vibration motors for different frequency vibration of the gamepad The GamePad class which is part of XNA provides a method called setVibration which controls the intensity of the vibration the motors produce Once a gamepad has been told to vibrate it will vibrate at that level until given another vibration command or the exit method is called to end an XNA game The Xbox gamepad has two vibration motors for different frequency vibration of the gamepad The GamePad class which is part of XNA provides a method called setVibration which controls the intensity of the vibration the motors produce Once a gamepad has been told to vibrate it will vibrate at that level until given another vibration command or the exit method is called to end an XNA game Chapter 3.2.2: Adding Vibration Effects17
18
True/False Revision Quiz The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. Chapter 3.2.2: Adding Vibration Effects18
19
True/False Revision Quiz The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. Chapter 3.2.2: Adding Vibration Effects19
20
True/False Revision Quiz The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. Chapter 3.2.2: Adding Vibration Effects20
21
True/False Revision Quiz The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. Chapter 3.2.2: Adding Vibration Effects21
22
True/False Revision Quiz The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. Chapter 3.2.2: Adding Vibration Effects22
23
True/False Revision Quiz The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. The Xbox 360 console contains vibration motors. An XNA program can make the keyboard vibrate. An XNA program can only control the vibration of one gamepad. The amount of gamepad vibration is set using a value in the range 0 to 1. The gamepad stops vibrating when it loses contact with the game. Chapter 3.2.2: Adding Vibration Effects23
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.