How to: Activate Controller Vibration

This topic describes how to activate vibration for the Xbox One gamepad. It assumes that you are familiar with how to use the gamepad API as described in Getting Started with the Gamepad API.

To activate controller vibration:

  1. Use Gamepad.Gamepads to get all connected gamepads.

    C++

    auto gamepads = Gamepad::Gamepads;  
    
  2. Retrieve the desired gamepad from the returned gamepads.

    C++

    IGamepad^ gamepad0;
          
    // For simplicity, we assume there is only one connected Xbox One gamepad.
    // Otherwise, check if the gamepads vector size is > 0 first, by using this: if (gamepads->Size > 0)
    gamepad0 = gamepads->GetAt(0);  
    
  3. Use SetVibration to set the left motor speed and the right motor speed.
    The following code sets the speed of both motors to 0.75f.

    C++

    GamepadVibration vib;
                      
    vib.LeftMotorLevel    = 0.75f;   // Valid range is 0.0 - 1.0
    vib.RightMotorLevel   = 0.75f;  // Valid range is 0.0 - 1.0
    vib.LeftTriggerLevel  = 0.0f;  // Valid range is 0.0 - 1.0
    vib.RightTriggerLevel = 0.0f; // Valid range is 0.0 - 1.0
                      
    gamepad0->SetVibration(vib);  
    

To stop gamepad vibration:

  1. Set the speed of both motors to zero.

    C++

    GamepadVibration vib;
                      
    vib.LeftMotorLevel    = 0.0f;
    vib.RightMotorLevel   = 0.0f;
    vib.LeftTriggerLevel  = 0.0f;
    vib.RightTriggerLevel = 0.0f;
                      
    gamepad0->SetVibration(vib);  
    

See Also

Getting Started with the Gamepad API

How to: Activate the Gamepad Impulse Triggers

How to: Detect Whether a Gamepad Button Is Pressed

How to: Get the Pairing Between Bodies and Users by Using an Event-Driven Approach

Programming the Xbox One Gamepad Controller

Gamepad Vibration Overview