adding vibration effects

23
1 Adding Vibration Effects Session 3.2.2

Upload: neola

Post on 23-Jan-2016

54 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Adding Vibration Effects

11

Adding Vibration Effects

Session 3.2.2

Page 2: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 2

Kris Athi
Color Nerve game? is this new
Page 3: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 3

Page 4: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 4

Page 5: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 5

GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;

Page 6: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 6

GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;

Page 7: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 7

GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;

Page 8: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 8

GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;GamePad.SetVibration ( PlayerIndex.One, 1, 0 ) ;

Kris Athi
might want to mention The "f suffix" thats needed with floating points on one of these slides
Page 9: Adding Vibration Effects

Vibration Demonstration

Chapter 3.2.2: Adding Vibration Effects 9

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);}

Page 10: Adding Vibration Effects

1. Vibration Demonstration

Chapter 3.2.2: Adding Vibration Effects 10

This program implements the vibration behavior that we have just seen

However, it does have a problem…

Page 11: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 11

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);

Page 12: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 12

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);

Page 13: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 13

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);

Page 14: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 14

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);

Page 15: Adding Vibration Effects

Using Blocks to Improve Code Appearance

Creating blocks can make your code clearer

In this case it separates the code from the conditions

Chapter 3.2.2: Adding Vibration Effects 15

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);}

Page 16: Adding Vibration Effects

2. Color Nerve with Vibration

Chapter 3.2.2: Adding Vibration Effects 16

This version of the game provides vibration feedback when any of the color intensity values exceeds 220

Page 17: Adding Vibration Effects

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

Chapter 3.2.2: Adding Vibration Effects 17

Page 18: Adding Vibration Effects

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.

Chapter 3.2.2: Adding Vibration Effects 18

Page 19: Adding Vibration Effects

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.

Chapter 3.2.2: Adding Vibration Effects 19

Page 20: Adding Vibration Effects

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.

Chapter 3.2.2: Adding Vibration Effects 20

Page 21: Adding Vibration Effects

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.

Chapter 3.2.2: Adding Vibration Effects 21

Page 22: Adding Vibration Effects

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.

Chapter 3.2.2: Adding Vibration Effects 22

Page 23: Adding Vibration Effects

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.

Chapter 3.2.2: Adding Vibration Effects 23