Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Nasty: The Game   |   Abduction: The Game   |   Starcraft Live  |   AngryEvilRobot.com   |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

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

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


Share: Wellplayed.net
This article has been view 2876 times.

Advertisement:

Comments:


Joel Bennett

Avatar

4/22/2009 10:19:24 AM

That does seem like a pretty nice and clean way of doing things. Something else that would be simple to add to this would to have the SetVibration rumble values fade out over the time period. This would be simple enough to do, and could get subtly different effects by using different interpolations (eg: a nice linear falloff of vibration, or something that ends a bit more briefly by using MathHelper.SmoothStep (a cubic interpolation).
Kris

Avatar

5/16/2009 10:43:51 PM

Yes, that would definately be a good idea to include. Thanks for the suggestion, maybe some day I'll get around to doing an update (or part 2) of this that include such functionality. Shouldn't be too hard to impliment.
C# XNA Newb

Avatar

5/22/2009 8:53:24 AM

Could you show a sample of the code you use to implement the rumble feature. Sorry I'm completely new to programming. Thanks.
Kris

Avatar

5/23/2009 8:02:52 AM

Check the article again, I've added a piece at the end with details of how to impliment this.


Add Comments:


Name
Website
Comment
Insert Cancel
top
top


top
Tags
YouTube (1)  ASP.net (16)  Annoyances (7)   (2)  Sage-Like Advice (12)  Video Games (6)  NFL (3)  Writing (2)  Abduction Action (1)  Particle Effects (1)  Google (3)  Visual Studio (1)  Site News (2)  Advise (1)  Interview (1)  C# (14)  Education (2)  Politics (1)  MSSQL (1)  Certifications (1)  Conspiracy (1)  Abduction Action! (12)  Nasty (30)  Get Rich Quick (1)  Technology (1)  Economy (1)  Cool (3)  Sports (11)  Web Tools (3)  Abduction! (1)  Rant (33)  XBLIG (11)  Tutorial (7)  Web Hosting (1)  Books (1)  Realty (1)  Programming (4)  Tech Support (1)  Vista (2)  Development (11)  Design (3)  Absurd (1)  Links (1)  Legal (2)  Security (4)  JavaScript (7)  CSS (1)  Video (5)  Weird (1)  Computing (2)  Content (1)  TFS (1)  Xbox (1)  XNA (24)  Popularity (6)  Abudction Action! (1)  Domain Name (2)  Game Design (4)  Wii (3)  .Net (3)  AJAX (4)  Game Development (6)  
top

top
top


Twitter

    Follow me on Twitter



    Enjoy Nasty!
    I'm with Coco.