Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

A Simple Class for XNA Force Feedback
Date 4/14/2009    Tags XNA    (0)

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);
kick it on GameDevKicks.com
/tds/go


This article has been view 4817 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)