A Simple Class for XNA Force Feedback
Adding force feed back (rumble) support to your game is easier than you may think. Many of the examples I found online where much more complex than what I have created, allowing for customizable XML rumble settings and several other features that aren't needed for a less complex game. In my game, I only needed a couple different rumble effects so I created a Rumble class with far less overhead.
public class Rumble
{
private float rumbleTimer;
bool enableRumble;
public Rumble()
{
enableRumble = false;
}
public void Update(float elapsed, PlayerIndex playerIndex)
{
if (enableRumble)
{
if (rumbleTimer > 0f)
{
rumbleTimer -= elapsed;
}
else
{
enableRumble = false;
rumbleTimer = 0f;
GamePad.SetVibration(playerIndex, 0, 0);
}
}
}
// Rumble effect for gunshots
public void Rumble_GunShot(PlayerIndex playerIndex)
{
rumbleTimer = .15f;
enableRumble = true;
GamePad.SetVibration(playerIndex, 1, 1);
}
// Rumble effect when the player is killed
public void Rumble_Killed(PlayerIndex playerIndex)
{
rumbleTimer = .75f;
enableRumble = true;
GamePad.SetVibration(playerIndex, .75f, .75f);
}
}
To use this, simply add the Rumble class to your project and create an instance of the class. Wire the Update method to run with your update loop. You could take this a step further and make it into a GameComponent too.
I created two example rumble methods (Rumble_GunShot and Rumble_Killed). To use these, whenever you hit an event that you want the controller to rumble for, just call that method along with the PlayerIndex of which controller you want to rumble. It's not that slick or easily customizable, but it works great for a small game or if the number of rumble instances you need are relatively small (I'm only using two so far and likely won't exceed four).
Update:
Per a reader request, I've been asked for how to impliment this class. So here's a couple lines (and descriptive comments) that show you how:
// Create the rumble object as a global variable
private Rumble rumbleObject = new Rumble();
// Run this code when you want to start your rumble effect
rumbleObject.Rumble_GunShot(PlayerIndex.One);
// Put this code in your update loop so that it updates the rumble effect
rumbleObject.Update(elapsed, PlayerIndex.One);
/tds/go
This article has been view 4817 times.
|