Wrecked Games

Please login or register.

Login with username, password and session length
Advanced search  

News:

We're just that awesome.

Author Topic: Joystick and Ogre3d Example  (Read 1384 times)

mashimaro

  • Newbie
  • *
  • Karma: +0/-0
  • Posts: 1
    • View Profile
Joystick and Ogre3d Example
« on: March 05, 2010, 04:16:24 PM »

Hi guys,

I'm trying to implement joystick support in my program, and I try, but fail...

I'm trying to implement it through the code from this tutorial:
http://www.ogre3d.org/wiki/index.php/Basic_Tutorial_4

Here's what I have so far, hopefully this is the right place to post this, let me know if it isn't.
So far, I want it to move when I press button 0 or 2, which doesn't work ><.



Quote
#include "ExampleApplication.h"

class TutorialFrameListener : public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener, public OIS::JoyStickListener
{
public:
    TutorialFrameListener(RenderWindow* win, Camera* cam, SceneManager *sceneMgr)
        : ExampleFrameListener(win, cam, true, true)
    {
        // Populate the camera and scene manager containers
        mCamNode = cam->getParentSceneNode();
        mSceneMgr = sceneMgr;

        // set the rotation and move speed
        mRotate = 0.13;
        mMove = 250;

        // continue rendering
        mContinue = true;

        mMouse->setEventCallback(this);
        mKeyboard->setEventCallback(this);
      mJoy->setEventCallback(this);
        mDirection = Vector3::ZERO;
    }

    bool frameStarted(const FrameEvent &evt)
    {
        if(mMouse)
            mMouse->capture();
        if(mKeyboard)
            mKeyboard->capture();
// capture joystick
      if(mJoy)
         mJoy->capture();
//Check to see which device is not buffered, and handle it
      if( !mJoy->buffered() )
         if( processUnbufferedJoyInput(evt) == false )
            return false;
        mCamNode->translate(mDirection * evt.timeSinceLastFrame, Node::TS_LOCAL);
        return mContinue;
    }

    // MouseListener
    bool mouseMoved(const OIS::MouseEvent &e)
    {
        if (e.state.buttonDown(OIS::MB_Right))
        {
            mCamNode->yaw(Degree(-mRotate * e.state.X.rel), Node::TS_WORLD);
            mCamNode->pitch(Degree(-mRotate * e.state.Y.rel), Node::TS_LOCAL);
        }
        return true;
    }
   bool povMoved( const OIS::JoyStickEvent &e, int pov ) { return true;}
    bool axisMoved( const OIS::JoyStickEvent &e, int axis ){ return true;}
    bool sliderMoved( const OIS::JoyStickEvent &e, int sliderID ) { return true;}
   bool buttonPressed( const OIS::JoyStickEvent &e, int button ) {
      if (e.state.buttonDown(0) ||  button == 0 || e.state.buttonDown(button)) {
         mDirection.z -= mMove;
      }
            if (e.state.buttonDown(2) || button == 2) {
         mDirection.z += mMove;
      }
      
      
      return true;

   }
    bool buttonReleased( const OIS::JoyStickEvent &e, int button ) { return true;}
    bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)  {
        Light *light = mSceneMgr->getLight("Light1");
        switch (id)
        {
        case OIS::MB_Left:
            light->setVisible(! light->isVisible());
            break;
        }
        return true;
    }

    bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true;}

    // KeyListener
    bool keyPressed(const OIS::KeyEvent &e)
    {
        switch (e.key)
        {
        case OIS::KC_ESCAPE:
            mContinue = false;
            break;

        case OIS::KC_1:
            mCamera->getParentSceneNode()->detachObject(mCamera);
            mCamNode = mSceneMgr->getSceneNode("CamNode1");
            mCamNode->attachObject(mCamera);
            break;

        case OIS::KC_2:
            mCamera->getParentSceneNode()->detachObject(mCamera);
            mCamNode = mSceneMgr->getSceneNode("CamNode2");
            mCamNode->attachObject(mCamera);
            break;

        case OIS::KC_UP:
        case OIS::KC_W:
            mDirection.z -= mMove;
            break;

        case OIS::KC_DOWN:
        case OIS::KC_S:
            mDirection.z += mMove;
            break;

        case OIS::KC_LEFT:
        case OIS::KC_A:
            mDirection.x -= mMove;
            break;

        case OIS::KC_RIGHT:
        case OIS::KC_D:
            mDirection.x += mMove;
            break;

        case OIS::KC_PGDOWN:
        case OIS::KC_E:
            mDirection.y -= mMove;
            break;

        case OIS::KC_PGUP:
        case OIS::KC_Q:
            mDirection.y += mMove;
            break;
        }
        return true;
    }

    bool keyReleased(const OIS::KeyEvent &e)
    {
        switch (e.key)
        {
        case OIS::KC_UP:
        case OIS::KC_W:
            mDirection.z += mMove;
            break;

        case OIS::KC_DOWN:
        case OIS::KC_S:
            mDirection.z -= mMove;
            break;

        case OIS::KC_LEFT:
        case OIS::KC_A:
            mDirection.x += mMove;
            break;

        case OIS::KC_RIGHT:
        case OIS::KC_D:
            mDirection.x -= mMove;
            break;

        case OIS::KC_PGDOWN:
        case OIS::KC_E:
            mDirection.y += mMove;
            break;

        case OIS::KC_PGUP:
        case OIS::KC_Q:
            mDirection.y -= mMove;
            break;
        } // switch
        return true;
    }
protected:
    Real mRotate;          // The rotate constant
    Real mMove;            // The movement constant

    SceneManager *mSceneMgr;   // The current SceneManager
    SceneNode *mCamNode;   // The SceneNode the camera is currently attached to

    bool mContinue;        // Whether to continue rendering or not
    Vector3 mDirection;     // Value to move in the correct direction
};

class TutorialApplication : public ExampleApplication
{
public:
    void createCamera(void)
    {
        // create camera, but leave at default position
        mCamera = mSceneMgr->createCamera("PlayerCam");
        mCamera->setNearClipDistance(5);
    }

    void createScene(void)
    {
        mSceneMgr->setAmbientLight(ColourValue(0.25, 0.25, 0.25));

        // add the ninja
        Entity *ent = mSceneMgr->createEntity("Ninja", "ninja.mesh");
        SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("NinjaNode");
        node->attachObject(ent);

        // create the light
        Light *light = mSceneMgr->createLight("Light1");
        light->setType(Light::LT_POINT);
        light->setPosition(Vector3(250, 150, 250));
        light->setDiffuseColour(ColourValue::White);
        light->setSpecularColour(ColourValue::White);

        // Create the scene node
        node = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode1", Vector3(-400, 200, 400));
        node->yaw(Degree(-45));
        node->attachObject(mCamera);

        // create the second camera node/pitch node
        node = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode2", Vector3(0, 200, 400));
    }

    void createFrameListener(void)
    {
        // Create the FrameListener
        mFrameListener = new TutorialFrameListener(mWindow, mCamera, mSceneMgr);
        mRoot->addFrameListener(mFrameListener);

        // Show the frame stats overlay
        mFrameListener->showDebugOverlay(true);
    }
};

#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"

INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
    // Create application object
    TutorialApplication app;

    try {
        app.go();
    } catch(Exception& e) {
#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
         MessageBoxA( NULL, e.what(), "An exception has occured!", MB_OK |   MB_ICONERROR  | MB_TASKMODAL);

#else
        fprintf(stderr, "An exception has occurred: %s\n",
            e.getFullDescription().c_str());
#endif
    }

    return 0;
}

Thanks!
« Last Edit: March 05, 2010, 05:15:37 PM by mashimaro »
Logged

pjcast

  • Administrator
  • Veteran
  • *****
  • Karma: +0/-0
  • Posts: 2653
    • View Profile
    • http://www.wreckedgames.com
Re: Joystick and Ogre3d Example
« Reply #1 on: March 06, 2010, 09:34:43 AM »

I don't see where you are creting mJoy device. By default, Ogre demos do not really create a joystick device. You'll need to do that, and make sure you turn buffered input on (recommended) for that device, and in the callbacks for JoyStickListener, simply react to the incoming values.
Logged