• Advertisement
Sign in to follow this  

[SDL] joystick

This topic is 3980 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm having some problems hooking up my joypad to my game in SDL. I have searched extensively for tutorials on using joysticks with SDL but I can find very little documentation. I check for SDL_JOYAXISMOTION events. When I call SDL_JoystickOpen(0); I immediately get 8 events. which, int axis, int value 0 0 -32768 0 1 -32768 0 2 -32768 0 3 -32768 0 0 128 0 1 128 0 2 128 0 3 128 my joystick has 2 analog thumbsticks and I suppose this is some sort of initialization, it is not really a problem what a problem is is that when I press any thumbstick in any direction and release it, it generates 2 events, which essentially "cancel" eachother. joy: 0 1 32767 // the downward movement joy: 0 1 -32768 // when released I use the thumbstick to move up and down through a menu a positive value for the 1st axis moves the menu up and a negative moves it down. when I press up the menu moves up but when I release it the event that that generates moves it down again. My joystick works fine with games so can anyone give me any hints as what I do wrong?

Share this post


Link to post
Share on other sites
Advertisement
You need to decouple the joystick's raw input from the menu's input event handler. Essentially you process the joystick input and form it into the correct input for the menu system.

The basic ideal is to only have the initial js move count and to disregard the move back to neutral. The other part is to only allow another menu move if the previous move had a release/return move.

Also you will have to account for dead zones, movement tolerance ranges, and movement jitter.
The logic will look something like this (not tested and probably wrong).

bool bJoyYReleasedDown = true;
bool bJoyYReleasedUp = true;
...
ON_EVENT_JOYSTICK_MOVED:
if(axis[AXIS_Y] < 0){
if(bJoyYReleasedDown){
menu_select--;
bJoyYReleasedUp = false;
}else{
bJoyYReleasedUp = true;
}
}else if(axis[AXIS_Y] > ){
if(bJoyYReleasedUp){
menu_select++;
bJoyYReleasedDown = false;
}else{
bJoyYReleasedDown = true;
}
}
break;



[Edited by - Jack Sotac on March 26, 2007 5:07:34 PM]

Share this post


Link to post
Share on other sites
Thanks that did the trick. I'm still having one problem; i'd like the menu selector keep going down when the player keeps the thumbstick down. But keeping the thumbstick down does not generate an event. Any suggestions on how to do this. Is there a way to poll the joystick at certain times to make it generate an event ?or should I just call getAxisPos() in my gameloop at certain times?

Solved it already, putting my code here incase someone runs into the same problem.

I'm polling the joystick values for the thumb axis in my main game loop using a timer otherwise you get way to much input. The regular ABCD buttons are still processed using events. Seems to work fine together.


/* TIMER to get joystick axes input*/
if (!timerset) {timerset=true; start=SDL_GetTicks();}
now=SDL_GetTicks();
if ((now-start)>50) { // tweak with this a bit.
timerset=false;
SDL_JoystickUpdate();
window.joyDown(0,SDL_JoystickGetAxis(joy,0));
window.joyDown(1,SDL_JoystickGetAxis(joy,1));
}


[Edited by - eelke_folmer on April 2, 2007 12:45:17 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement