My Ps3 Joypad
Hello all
Yesterday I managed to connect my Ps3 Controller (DualShock3, Sixaxis) with my computer. It didn't work well, but I found this. It's great! All buttons work perfectly.
Then it was time to add ps3 pads support to Esenthel. I made test application based on Tutorials/Advanced/Misc/Joypad Vibrations. Here it's source code of my Draw() function from that tutorial:
PHP Code:
D.clear(WHITE); if( Joypads()<=0 )D.text(0, -0.4, "No Joypads Detected");else if(!Joypad[0].supportsVibrations())D.text(0, -0.4, "Joypad #0 doesn't support Vibrations");else { D.text(0, -0.9f, "Press Keyboard Arrow Keys to Change Applied Force"); D.text(0, -0.6f , S+"Applied Force: "+force); } FREP(32) { if(Joypad->b(i))D.text(0,0.9,S+"Button "+i); } D.text(0, 0.8, S+"Left Axis "+Joypad->dir_a[0]); D.text(0, 0.7, S+"Right Axis "+Joypad->dir_a[1]); D.text(0, 0.6, S+"DInput rx "+Joypad->dis.rx); D.text(0, 0.5, S+"DInput ry "+Joypad->dis.ry); D.text(0, 0.4, S+"DInput rz "+Joypad->dis.rz); D.text(0, 0.3, S+"DInput slider0 "+Joypad->dis.slider[0]); D.text(0, 0.2, S+"DInput slider1 "+Joypad->dis.slider[1]); D.text(0, 0.1, S+"DInput x "+Joypad->dis.x); D.text(0, 0.0, S+"DInput y "+Joypad->dis.y); D.text(0, -0.1, S+"DInput z "+Joypad->dis.z); D.text(0, -0.2, S+"Dir "+Joypad->dir);
Now I'm working at custom Ps3 Controller support class. Current source code (not finished yet, variable and function names can be bad):
PHP Code:
//.h file enum PS3PAD_AXIS { AXIS_X_PLUS = 20, AXIS_X_MINUS, AXIS_Y_PLUS, AXIS_Y_MINUS, AXIS_NUM };
class Ps3Controller { JoypadClass &joypad; public:
Ps3Controller() : joypad(Joypad[0]) {}
//left and right joystick bool Axis(PS3PAD_AXIS Axis, bool LeftAxis = true, Flt deadZone = 0.1f); //buttons bool Btn(int BtnNumber, bool Long = true); //L2 and R2 buttons bool LR2(bool Left, Flt deadZone = 0.2f);
} extern Ps3;
//.cpp file Ps3Controller Ps3;
bool Ps3Controller::Axis( PS3PAD_AXIS Axis, bool LeftAxis /*= true*/, Flt deadZone) { Vec2 axis = (LeftAxis ? joypad.dir_a[0] : joypad.dir_a[1]);
//in right joystick X and Y axis are replaced (X is Y, Y is X) //this code repairs it if(!LeftAxis)axis = Vec2(axis.y, axis.x);
switch(Axis) { case AXIS_X_PLUS: if(axis.x > deadZone)return true; break; case AXIS_X_MINUS: if(axis.x < -deadZone)return true; break; case AXIS_Y_PLUS: if(axis.y > deadZone)return true; break; case AXIS_Y_MINUS: if(axis.y < -deadZone)return true; break; } return false; }
bool Ps3Controller::Btn( int BtnNumber, bool Long /*= true*/ ) { bool pressed = (Long ? joypad.b(BtnNumber) : joypad.bp(BtnNumber)); return pressed; }
bool Ps3Controller::LR2( bool Left, Flt deadZone /*= 0.2f*/ ) { bool pressed; if(Left) { if(joypad.dis.rx < -deadZone)pressed = true; else pressed = false; } else if(!Left) { if(joypad.dis.rx > deadZone)pressed = true; else pressed = false; } return pressed; }
How to use:
For tests I changed some Player moving code
PHP Code:
// turn & move //input.turn.x=Kb.b(KB_Q)-Kb.b(KB_E); //input.turn.y=Kb.b(KB_T)-Kb.b(KB_G); input.turn.x=(Ps3.Axis(AXIS_X_PLUS, false, 0.3f)-Ps3.Axis(AXIS_X_MINUS, false, 0.3f)); input.turn.y=(Ps3.Axis(AXIS_Y_MINUS, false, 0.5f)-Ps3.Axis(AXIS_Y_PLUS, false, 0.5f)); //input.move.x=Kb.b(KB_D)-Kb.b(KB_A); //input.move.z=Kb.b(KB_W)-Kb.b(KB_S); input.move.x=Ps3.Axis(AXIS_X_PLUS)-Ps3.Axis(AXIS_X_MINUS); input.move.z=Ps3.Axis(AXIS_Y_PLUS)-Ps3.Axis(AXIS_Y_MINUS); input.move.y=Kb.b(KB_SPACE)-Kb.b(KB_LSHIFT); // dodge, crouch, walk, jump input.dodge = Kb.bd(KB_D)-Kb.bd(KB_A); input.crouch= Kb.b (KB_LSHIFT); //input.walk = Kb.b (KB_LCTRL ); input.walk = Ps3.Btn(0); input.jump =(Kb.bp(KB_SPACE ) ? 3.5f : 0);
If you'd like see some images, I can add some.
Problems:
- I don't know, how to enable moving detector (sixaxis, g-sensor). SLOVED!
- Esenthel doesn't support it's vibrations (Joypad.supportVibrations() returns false)
Sorry 4 my English.
(This post was last modified: 12-28-2010 09:57 AM by Barthap.)
|