1 / 23

Adding Vibration Effects

Session 3.2.2. Adding Vibration Effects. 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. Session Overview.

neola
Download Presentation

Adding Vibration Effects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 3.2.2 Adding Vibration Effects

  2. 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 Effects Session Overview

  3. 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 Effects The Xbox Gamepad Vibration

  4. 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 Effects Vibration Speeds

  5. 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 Effects Controlling Gamepad Vibration from XNA GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;

  6. 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 Effects Selecting the Gamepad to Be Controlled GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;

  7. 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 Effects Setting the Vibration Levels GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;

  8. 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 Effects Setting the Vibration Levels GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;

  9. Chapter 3.2.2: Adding Vibration Effects Vibration Demonstration protected override void Update(GameTimegameTime) { // 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. Chapter 3.2.2: Adding Vibration Effects 1. Vibration Demonstration • This program implements the vibration behavior that we have just seen • However, it does have a problem…

  11. 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 Effects Making the Vibration Stop if (pad1.Buttons.X == ButtonState.Pressed) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0);

  12. 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 Effects Adding Vibration to Color Nerve if (redIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0);

  13. 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 Effects The Greater Than Operator if (redIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0);

  14. 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 Effects Combining Tests if (redIntensity > 220 || greenIntensity > 220 || blueIntensity > 220) GamePad.SetVibration(PlayerIndex.One, 1, 0); else GamePad.SetVibration(PlayerIndex.One, 0, 0);

  15. Creating blocks can make your code clearer In this case it separates the code from the conditions Chapter 3.2.2: Adding Vibration Effects Using Blocks to Improve Code Appearance if (redIntensity > 220 || greenIntensity > 220 || blueIntensity > 220) { GamePad.SetVibration(PlayerIndex.One, 1, 0); } else { GamePad.SetVibration(PlayerIndex.One, 0, 0); }

  16. Chapter 3.2.2: Adding Vibration Effects 2. Color Nerve with Vibration • This version of the game provides vibration feedback when any of the color intensity values exceeds 220

  17. 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 Effects Summary

  18. 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 Effects True/False Revision Quiz

  19. 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 Effects True/False Revision Quiz

  20. 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 Effects True/False Revision Quiz

  21. 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 Effects True/False Revision Quiz

  22. 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 Effects True/False Revision Quiz

  23. 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 Effects True/False Revision Quiz

More Related