Page 1 of 1

irrXbox360Controller

Posted: Tue Dec 26, 2006 6:33 pm
by lug
Image

I've spent like a day on this. The picture above is from the simple test program based on the DirectX December SDK example of the same name. Of course, it's running inside irrlicht. :)

Here's what the example code looks like:

Code: Select all

#include <iostream>
#include "CIrrXbox360Controller.h"

// hide the console window
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

int main()
{
    // create xbox 360 controller class
    irr::xb360controller::CIrrXbox360Controller xb360Controller;

    // create device
    IrrlichtDevice* device = createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(800, 600));

    if (device == 0)
        return 1; // could not create selected driver.

    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    gui::IGUIEnvironment* env = device->getGUIEnvironment();

    driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);

    // add irrlicht logo
    env->addImage(driver->getTexture("media/irrlichtlogoalpha.tga"),
        core::position2d<s32>(10,10));

    // add explanation text
    gui::IGUIStaticText* textExplanation = env->addStaticText(
        L"This sample displays the state of all 4 XInput controllers\nPress 'D' to toggle dead zone clamping.",
        core::rect<s32>(130,10,500,70), false, true, 0, -1, false);

    // add test value text
    gui::IGUIStaticText* textTestValues = env->addStaticText(
        L"Buttons: ",
        core::rect<s32>(130,90,550,800), false, true, 0, -1, false);

    video::SColor textColor(255,255,255,255);
    textExplanation->setOverrideColor(textColor);
    textTestValues->setOverrideColor(textColor);

    // disable mouse cursor
    device->getCursorControl()->setVisible(false);

    int lastFPS = -1;
    u32 dwResult = 0;
    wchar_t sz[1024];

    video::SColor background(0,85,153,240);

    while(device->run())
        if (device->isWindowActive())
        {
            driver->beginScene(true, true, background );

            smgr->drawAll();
            env->drawAll();

            // Simply get the state of the controller from XInput.
            dwResult = xb360Controller.getXInputState(0);

            if(dwResult == ERROR_SUCCESS)
                xb360Controller.setConnected(true);
            else
                xb360Controller.setConnected(false);

            if(xb360Controller.isConnected())
            {
                WORD wButtons = xb360Controller.getButtons();

                swprintf(sz,1024,
                    L"Controller %d: Connected\n"
                    L"  Buttons: %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n" 
                    L"  Left Trigger: %d\n"
                    L"  Right Trigger: %d\n"
                    L"  Left Thumbstick: %d/%d\n"
                    L"  Right Thumbstick: %d/%d", 0, 
                    (wButtons & XINPUT_GAMEPAD_DPAD_UP) ? L"DPAD_UP " : L"",
                    (wButtons & XINPUT_GAMEPAD_DPAD_DOWN) ? L"DPAD_DOWN " : L"",
                    (wButtons & XINPUT_GAMEPAD_DPAD_LEFT) ? L"DPAD_LEFT " : L"",
                    (wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) ? L"DPAD_RIGHT " : L"",
                    (wButtons & XINPUT_GAMEPAD_START) ? L"START " : L"",
                    (wButtons & XINPUT_GAMEPAD_BACK) ? L"BACK " : L"",
                    (wButtons & XINPUT_GAMEPAD_LEFT_THUMB) ? L"LEFT_THUMB " : L"",
                    (wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) ? L"RIGHT_THUMB " : L"",
                    (wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? L"LEFT_SHOULDER " : L"",
                    (wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? L"RIGHT_SHOULDER " : L"",
                    (wButtons & XINPUT_GAMEPAD_A) ? L"A " : L"",
                    (wButtons & XINPUT_GAMEPAD_B) ? L"B " : L"",
                    (wButtons & XINPUT_GAMEPAD_X) ? L"X " : L"",
                    (wButtons & XINPUT_GAMEPAD_Y) ? L"Y " : L"",
                    xb360Controller.getLeftTrigger(),
                    xb360Controller.getRightTrigger(),
                    xb360Controller.getThumbLX(),
                    xb360Controller.getThumbLY(),
                    xb360Controller.getThumbRX(),
                    xb360Controller.getThumbRY() );

                textTestValues->setText(sz);
            }
            else
            {
                swprintf(sz, 1024,                 
                    L"Controller %d: Not connected", 0 );

                textTestValues->setText(sz);
            }

            driver->endScene();

            // display frames per second in window title
            int fps = driver->getFPS();
            if (lastFPS != fps)
            {
                core::stringw str = L"XBOX 360 Controller: SimpleController Test [";
                str += driver->getName();
                str += "] FPS:";
                str += fps;

                device->setWindowCaption(str.c_str());
                lastFPS = fps;
            }
        }

        device->drop();

        return 0;
}

The goal is to be able to support the XInput API (XBOX 360 Controller) from the DirectX SDK inside irrlicht (yes, I know about SDL and OSI). Right now, it doesn't do much (detect button presses and analog stick movements). :)

TODO:

- rumble support (shouldn't be that hard to add)
- camera that uses the analog sticks for mouse movement
- bind the buttons to irrlicht keys without changing the irrlicht inner code

:D

Update:

Image

Rumble support is in! :)

Here's a win32 binary to try it out:

http://www.megaupload.com/?d=YDNXCUA0

Update 2:

Image

Analog controlled camera and character rotation working! Camera code based on the "Dead Easy 3rd Person Camera" from here:

http://irrlicht.sourceforge.net/phpBB2/ ... php?t=7977

The left thumb stick controls the character rotation. The right thumb stick controls the camera rotation. Right now, everything is too fast. I need to fine tune the math a bit.

Anyway, give it a whirl:

http://www.megaupload.com/?d=IBP2UZI0

Update 3:

Yay! :D Now I have mapped the different animation of the model to the various xbox 360 buttons:

"A" button for attack:
Image

Left thumb button for crouch:
Image

Left shoulder button for flip:
Image

"B" button for jump:
Image

Right shoulder button for run:
Image

Update 4:

Start button to show window/Back button to close window:
Image

Download:

http://www.megaupload.com/?d=XOCEF9PI

Update 5:

Image

The thumb stick behaves a little bit now (try holding down in one direction for a smooth spin). Tapping the thumb stick makes it rotate like crazy.

Anyway, I've added control mappings to the instruction and some debug information. I've made the DPAD change the window color. :)

Download:

http://www.megaupload.com/?d=7JHKDL0A

Update 6:

Image

Thumb sticks "crazy fast" movement has been fixed! Also added a skydome as a reference point for orientation purposes.

Download (you know, megaupload is going to start charging me for uploading so frequently):

http://www.megaupload.com/?d=OGHXGQ6P

Update 7:

Image

Now the mesh can move using the left thumb stick (disabled rotation of mesh though). Also added a terrain, bitplane's grass node with klasker's wind generator, and bitplane's cloud node to help with orientation.

Download:

http://www.megaupload.com/?d=EJNR8Q91

Posted: Thu Jan 04, 2007 6:40 pm
by BlindSide
Wow this is amazing, great work!...now if only I had an XBOX 360... :lol:

Posted: Tue Jan 09, 2007 1:27 pm
by MolokoTheMole
Wow this the it! I even have a controller to test it but I can't download it :(. Can you ulpoad it somewhere else (maybe http://www.mediafire.com/)?
Also is the source available?

Posted: Wed Jan 31, 2007 5:09 am
by Zenix
Wow this the it! I even have a controller to test it but I can't download it Sad. Can you ulpoad it somewhere else (maybe http://www.mediafire.com/)?
Also is the source available?
Can I second that?

Posted: Thu Feb 01, 2007 1:38 am
by lug
NO! :)

Actually, I was going to update the camera to make the movement smoother. Also, I ran into some serious clipping/collision issue with the dead easy camera.

So, I plan to switch over to another camera system before posting another release.

We'll see. I'm busy with irrGUIEditor at the moment (and two other projects not announced yet on this board -- incidently, they're taking up a huge chunk of time). :(

I get too many ideas after starting a new project. Then before I know it, I'm coding/designing like 15 different things. :)

Coding is an addiction for me. :)

Sorry for the late reply MolokoTheMole and Zenix. :cry:

Posted: Thu Feb 01, 2007 6:21 am
by Rambus
lug - Project looks good,
if you need free and ad free, web hosting check out my offering. Two emails and you could be up and running...
http://www.g0dsoft.com/index.php?topic=17.0

P.S. Guess its time to buy that xbox pc controller!

Posted: Fri Feb 02, 2007 3:12 am
by lug
Rambus wrote:lug - Project looks good,
if you need free and ad free, web hosting check out my offering. Two emails and you could be up and running...
http://www.g0dsoft.com/index.php?topic=17.0

P.S. Guess its time to buy that xbox pc controller!
Thanks, Rambus for the webspace offer. But I'm going to stick with atspace.com for now. If they don't work out, I'll give you a holler. :)

Posted: Fri Feb 02, 2007 4:45 am
by BlindSide
Wow its another New Zealander I almost missed it. Howdy neighbour! :D

Posted: Wed Feb 14, 2007 9:02 pm
by fullforce
That looks great!
Is it vista only?

Posted: Sat Feb 17, 2007 12:57 am
by lug
fullforce wrote:That looks great!
Is it vista only?
Thanks. No, it's not vista only. It was developed on Vista x64 machine. The program runs on Windows XP/x64 (haven't tried Windows 2000 so can't say one way or another).

Posted: Fri Apr 11, 2008 5:03 pm
by Gianni
Uauuuu and for xbox 1 You can do this as well?

Posted: Wed Feb 18, 2009 11:31 am
by franklin
lug wrote:NO! :)

Actually, I was going to update the camera to make the movement smoother. Also, I ran into some serious clipping/collision issue with viagra the dead easy camera.

So, I plan to switch over to another camera system before posting another release.

We'll see. I'm busy with irrGUIEditor at the moment (and two other projects not announced yet on this board -- incidently, they're taking up a huge chunk of time). :(

I get too many ideas after starting a new project. Then before I know it, I'm coding/designing like 15 different things. :)

Coding is an addiction for me. :)

Sorry for the late reply MolokoTheMole and Zenix. :cry:
Wow, lug! It's beautiful work! Can I use it with linux or solaris?

Posted: Wed Feb 18, 2009 1:57 pm
by devsh
Well done, I did the same kind of thing with my controller, but have the keys and axis left to map, so that will be a bit of work.

I have started with rumble thou....

can I call mine irrXboxControllerLinux???