1

So I've started using DirectX C++ and have a base game with is a bunch of asteroids which you can shoot using the mouse. It also has a drawn crosshair sprite which follows the mouses location.

I have decided that I would like to change input to support Xbox 360 controllers. I have managed to program the shoot button to the A button on the gamepad however I am having some issues changing to movement of the crosshair from mouse to the analog sticks on the controller.

The following is the code which is used for determining the mouses location and getting its position for use:

    virtual void Update() 
        { 
        POINT mousePosition;
        D3DXVECTOR3 position, currentPosition;
        DirectInput::Get().ProcessMouse();
        mousePosition = DirectInput::Get().GetCurrentMousePosition();
        position.x = (float) mousePosition.x;
        position.y = (float) mousePosition.y;
        position.z = 0.0f;
        currentPosition = m_pCrossHairs->GetSpritePosition();
        position.x -= currentPosition.x;
        position.y -= currentPosition.y;
        position.x = position.x/2.0f;
        position.y = position.y/2.0f;
        m_pCrossHairs->SetTranslationMatrix(position);
        m_pCrossHairs->CheckBoundary();
        m_pCrossHairs->Update();
        }

And here is what I've changed it to for use with an Xbox Controller Instead:

    virtual void Update() 
        { 
        POINT crosshairPosition;
        D3DXVECTOR3 position, currentPosition;
        crosshairPosition.x = XBox360Controller::Get().RightJoyStickX();
        crosshairPosition.y =- XBox360Controller::Get().RightJoyStickY();
        position.x = crosshairPosition.x;
        position.y = crosshairPosition.y;
        position.z = 0.0f;
        currentPosition = m_pCrossHairs->GetSpritePosition();
        position.x -= currentPosition.x;
        position.y -= currentPosition.y;
        position.x = position.x/2.0f;
        position.y = position.y/2.0f;
        m_pCrossHairs->SetTranslationMatrix(position);
        m_pCrossHairs->CheckBoundary();
        m_pCrossHairs->Update();
        } 

On a positive note what I have done kind of works. The Crosshair does move when I move the analog stick, however it is stuck in the top right hand corner and can only move within a range of about 1 square inch on the screen. I'm a bit stuck as to why this is and therefore would appreciate any kind of input. Sorry if I'm lacking any important info that I may have missed, a bit of a late post however would really love to get it working!

On a side note - I'm getting an error on the following giving me a warning about conversion from long to float or something (should I be worried about this)?

        crosshairPosition.x = XBox360Controller::Get().RightJoyStickX();
        crosshairPosition.y =- XBox360Controller::Get().RightJoyStickY();

Once again thank you in advance. Billy

Edit:

Code for XBox360Controller::Get().

XBox360Controller& XBox360Controller::Get()
{
if (s_pXBox360Controller == NULL)
    s_pXBox360Controller = new XBox360Controller();
return *s_pXBox360Controller;
}

Code for Right analog stick X-Axis

float XBox360Controller::RightJoyStickX()
{
float joyStickX = GetState().Gamepad.sThumbRX;
// Make sure there has been movement, as joystick does return different values even if no movement.
if ( joyStickX< XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && 
    joyStickX > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
    return 0;
else
    return joyStickX/DynamicRange;
}

Code for Right analog stick Y-Axis

float XBox360Controller::RightJoyStickY()
{
float joyStickY = GetState().Gamepad.sThumbRY;
// Make sure there has been movement, as joystick does return different values even if no movement.
if ( joyStickY< XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && 
    joyStickY > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
    return 0;
else
    return joyStickY/DynamicRange;
}

EDIT SOLVED:

Added a new translation matrix and it's fixed the issue, thanks for the help.

  • I'll need the code for your `XBox360Controller::Get()` to provide any real help. But for the side note, just slap a `(float)` after the equals and it'll go away. Now, for my side note: for `crosshairPosition.y`, did you intend for `=-` to follow, or did you mean to type `=` or `-=`? `=-` doesn't seem to make a whole lot of sense. – Proxy Feb 12 '14 at 03:42
  • Thanks for reply, I have pasted the code for XBox360Controller::Get() below. In regards to the crosshair position =- I have a feeling that was my attempt to prevent invert axis as when i was pressing down on the analog stick the crosshair was going up. I tried a few things so not actually sure that this was the fix, might have just been a bit I forgot to deleted so thanks for pointing it out! – Billy Yaxley Feb 12 '14 at 14:02
  • Sorry I'm new to this haha! Didn't realise enter added the comment! How do I embed code in the comments? – Billy Yaxley Feb 12 '14 at 14:05
  • I've actually got to go now, but the line of code that looked suspicious was the `return` statement - what does dividing by `DynamicRange` scale the joystick movement to? (What are the new minimum/maximum values? My guess is that that value in pixels will take up about a square inch on your screen). – Proxy Feb 12 '14 at 14:10
  • Not a problem, I'll have a look into it - have just pasted the requested code anyhow! – Billy Yaxley Feb 12 '14 at 14:11
  • Please post your solution as an answer and accept it so this question is marked as solved. – Nico Schertler Feb 13 '14 at 09:41

0 Answers0