Sonix2001

Hi,

I've just starting playing around with XNA for xbox 360 and have a noob question

I want to vibrate controller 1 for a single second. I know to set vibration on I can use the code:

GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);

and to turn it off:

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

But how do you set the length of time for it to vibrate

I have experience in programming with C++ but have not yet had the chance to fully immerse myself into C#.

Thanks



Re: XNA Game Studio Express Vibrate the controller for 1 second?

JLarkin

Maybe take note of the gameTime before you start the rumble, and then keep rumbling until your gameTime has been updated to initial+1 second. This would indicate that the game has progressed a full second since the last update, and it's now time to turn the rumble motors off.



Re: XNA Game Studio Express Vibrate the controller for 1 second?

Ecrofirt

You could also try something like setting the value in a static DateTime object when your start the motor, and then use a TimeSpan to find the number of seconds that have passed since the rumble initialized.

Something like this, and excuse my lack of proper button check and rumble initialization code. I'm not near a PC with XNA on it:

WhateverFunction()
{
    static DateTime startRumble;

    if (you pressed the button that causes rumble)
    {
        GamePad.SetVibration(PlayerIndex.One,1f,1f);
        startRumble = DateTime.Now;
    }

....

    TimeSpan timePassed = DateTime.Now - startRumble;

    if (timePassed.totalSeconds >=1.0)
    {
        GamePad.SetVibration(PlayerIndex.One,0f, 0f);
    }

}

Optimize it obviously so that you aren't doing that timespan check every time, or you'll end up constantly turning off the rumble when you don't need to.




Re: XNA Game Studio Express Vibrate the controller for 1 second?

Sonix2001

Thanks Ecrofirt
That works fine




Re: XNA Game Studio Express Vibrate the controller for 1 second?

Ecrofirt

Sonix2001 wrote:
Thanks Ecrofirt
That works fine


I don't know that what I'm doing is the preferred way to do these things, it's just the method I use because I know it works. Someone might post later that my method is trash, and offer a much better solution.




Re: XNA Game Studio Express Vibrate the controller for 1 second?

Jim Perry

Doing it that way measure actual time though, not game elapsed time which you might want to do. Using the gameTime parameter in the Update method allows you to measure actual elapsed game time.




Re: XNA Game Studio Express Vibrate the controller for 1 second?

waheyluggage

I tend to measure things in seconds, like the following...
 

static float rumbleTimer;

void CheckForRumble(GameTime gameTime) {

     if (you pressed the button that causes rumble)  {

          GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);

          rumbleTimer = 1.0f; // this is measured in seconds so two and a half seconds would be 2.5

     }

     rumbleTimer -= gameTime.ElapsedGameTime.TotalSeconds; // take off the amount of time passed since the last frame

     if (rumbleTimer <= 0.0f)  {

          GamePad.SetVibration(PlayerIndex.One, 0.0f, 0.0f);

     }

}

 





Re: XNA Game Studio Express Vibrate the controller for 1 second?

SouLDragooN

And yet another example =).

I have a GlobalObjects class that handles items such as this. That way any of my entities can override the shaking and get a more centralized control since there is only one gammepad in the players hand. You could also speed it up by taking out that divide and inverse mult of course... just hav enot done it.. =)..hope it helps..

public class GamePadRumble
{
float m_Time;
float m_Ratio;
float m_CurrTime;
float m_LeftMotor;
float m_RightMotor;

public GamePadRumble()
{

}

public void Dispose()
{
GamePad.SetVibration(0, 0, 0);
}

public void Update(ref float TimeElapsed)
{
if (GlobalObjects.m_Settings.m_RumbleEnabled)
{
m_CurrTime -= TimeElapsed;
m_Ratio = m_CurrTime / m_Time;
if (m_CurrTime <= 0)
{
GamePad.SetVibration(0, 0, 0);
}
else
{
GamePad.SetVibration(0, m_LeftMotor * m_Ratio, m_RightMotor * m_Ratio);
}
}
}

public void ShakePad(float Time,float LeftMotor,float RightMotor)
{
m_LeftMotor = LeftMotor;
m_RightMotor = RightMotor;
m_CurrTime = m_Time = Time;
}

}