DirectX Input With PS3 Controller?

Started by
14 comments, last by CDTMD 14 years, 4 months ago
Well to play some of my computer games I plug in my PS3 controller then use some six-axis program so my computer can recognize it. Since it works it means I should be able to do it too. So my question is: How do I get input from the controller?
Advertisement
You could try using IDirectInput8::EnumDevices to see if DirectInput can see the device, although I don't really know if that'll work. Give it a go I guess...
Thanks for you help Evil Steve. It can recognize it. I did some more researching and I was able to get something working.

I can get it to check correctly if a button is pressed.

But I am having problems with the sticks.

js.lX is supposed to be the X direction of the stick. However when I check the results it shows that it is some big number.

Shouldn't it be like 0 until I move it?

What am I doing wrong?
In DirectInput during EnumerateObjects you get a struct with object infos (LPCDIDEVICEOBJECTINSTANCE).

Check the type for an axis (either DIDFT_ABSAXIS or DIDFT_RELAXIS). If it has one of those bits set you can check the ranges the device will report:

DIPROPRANGE     diRange;diRange.diph.dwSize = sizeof( diRange );diRange.diph.dwHeaderSize = sizeof( DIPROPHEADER );diRange.diph.dwHow = DIPH_BYID;diRange.diph.dwObj = lpddoi->dwType;HRESULT hRes = pDevice->GetProperty( DIPROP_RANGE, (LPDIPROPHEADER)&diRange );if ( SUCCEEDED( hRes ) ){  NewCtrl.m_iMin = diRange.lMin;  NewCtrl.m_iMax = diRange.lMax;}


Compare the polled value to your min/max ranges (use some threshold value). Done.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Sorry for the bump but I'm back after a lot of homework and being sick. I never got it to work though.

This is the code I'm using.

I made a class for it. These are the only two calls I make. I get input then I write the result to a file. (ignore the getX(). Its just a temp name for it)

ps3i->getInput();
out << ps3i->getX() << endl;

The getInput() function

void PS3Input::getInput()
{
PS3Device->Poll();
PS3Device->Acquire();
PS3Device->GetDeviceState(sizeof(DIJOYSTATE2), &js);
}

getX()

int PS3Input::getX()
{
return js.lX;
}

This is the enumObjectsCallback function

BOOL CALLBACK PS3Input::enumObjectsCallback( const DIDEVICEOBJECTINSTANCE* instance, VOID* context )
{
HWND hDlg = ( HWND )context;

static int nSliderCount = 0; // Number of returned slider controls
static int nPOVCount = 0; // Number of returned POV controls

// For axes that are returned, set the DIPROP_RANGE property for the
// enumerated axis in order to scale min/max values.
if( instance->dwType & DIDFT_AXIS )
{
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof( DIPROPRANGE );
diprg.diph.dwHeaderSize = sizeof( DIPROPHEADER );
diprg.diph.dwHow = DIPH_BYID;
diprg.diph.dwObj = instance->dwType; // Specify the enumerated axis
diprg.lMin = -1000;
diprg.lMax = +1000;

// Set the range for the axis
if( FAILED( PS3Device->SetProperty( DIPROP_RANGE, &diprg.diph ) ) )
return DIENUM_STOP;
}
}


Wasn't I supposed to get values from -1000 to 1000?

These are the ones I get.

[Numbers too long, it pissed me off, deleted.]

[Edited by - CDTMD on December 4, 2009 11:48:24 PM]
Looks right to me.
Subtract MAXWORD/2 (eg, the maximum value of a 16 bit unsigned integer, divided on two).

Being unsigned, it cannot represent negative numbers :)
What did you do when the value was 32768? was it perhaps idle? (hint hint)
When I change it to: return (js.lX-(MAXWORD/2));
Now the value I get for no movement is -512

If it can't represent - numbers why set the range from -1000->1000?

Yes it was idle. Now instead of 32768 I get -512





From what I can understand aren't I supposed to just set a range

-5 to 5

Then for example if I wanted to move a char with it then I just get the value (ex: 5) and use that as the speed, correct?

So what's with all these large and strange numbers?
Quote:Original post by CDTMD
When I change it to: return (js.lX-(MAXWORD/2));
Now the value I get for no movement is -512
MAXWORD = 0xffff = 65535.
MAXWORD/2 = 32767
32768 - 32767 = 1, which is 0.1% above 0.

Where are you getting -512 from?
I have no idea but it seems to random.....

These numbers are from starting it then holding the left stick up. I waited a few seconds before moving the stick.

[Numbers too long, it pissed me off, deleted.]

These numbers are from when I tried it but did not touch the controller at all...

[Numbers too long, it pissed me off, deleted.]

And these from when I changed it back to: return js.lX;

[Numbers too long, it pissed me off, deleted.]

And these from when I waited then moved the left stick to the left.

[Numbers too long, it pissed me off, deleted.]

Whats with all the random numbers? Shouldn't idle be a constant?

[Edited by - CDTMD on December 5, 2009 2:55:56 PM]
The PS3 analog sticks are very sensitive. This is why most input managers implement a "dead zone" which is defined as the range in which physical input values are mapped as zero motion.

As Endurion said, you get the minimum and maximum points of the range from DI. The center of a given axis is the average of these values, not the constant zero as you seem to assume. The average is trivially found as (min+max)/2.

When the PS3 controller is connected, it performs an automatic calibration based on the current position of the analog controls (which usually are at center position during this time). DI adjusts the range automatically to take this into account. Therefore, you shouldn't assume that the range is exactly the same everytime you connect the controller.

Niko Suni

This topic is closed to new replies.

Advertisement