We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4082 - Joystick Axis Issue Answer
Summary: Joystick Axis Issue Answer
Status: RESOLVED INVALID
Alias: None
Product: SDL
Classification: Unclassified
Component: joystick (show other bugs)
Version: HG 2.1
Hardware: x86_64 Windows 10
: P2 critical
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-02-15 20:25 UTC by Lísias de Castro
Modified: 2018-03-11 05:26 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lísias de Castro 2018-02-15 20:25:33 UTC
I am trying to pass the axis value to my joystick variable.
But instead of answer 0 to horizontal and 1 to vertical, the
event.jaxis.axis returns 0,1,2 in a row, or 4 individually.
From the event.jaxis.value I can get -255, 255, -32767, 32767.
But as soon as the event.jaxis.axis does not respond correctly,
I can't update my joystick value.
Here is my code.

case SDL_JOYAXISMOTION:
                {
                    B32 i;
                    for (i = 0; i < SDL_NumJoysticks(); i++) {
                        if (to->machine->sdl2->event.jaxis.which == i) {
                            if(to->machine->sdl2->event.jaxis.axis == 0){
                                if(to->machine->sdl2->event.jaxis.value == -1){
                                    to->machine->joystick.left = On;
                                }
                                if(to->machine->sdl2->event.jaxis.value == 1){
                                    to->machine->joystick.right = On;
                                }
                                if(to->machine->sdl2->event.jaxis.value == 0){
                                    to->machine->joystick.left = Off;
                                    to->machine->joystick.right = Off;
                                }
                                printf("%hhu\n",to->machine->sdl2->event.jaxis.axis);
                            }
                            if(to->machine->sdl2->event.jaxis.axis == 1){
                                if(to->machine->sdl2->event.jaxis.value == -1){
                                    to->machine->joystick.up = On;
                                }
                                if(to->machine->sdl2->event.jaxis.value == 1){
                                    to->machine->joystick.down = On;
                                }
                                if(to->machine->sdl2->event.jaxis.value == 0){
                                    to->machine->joystick.up = Off;
                                    to->machine->joystick.down = Off;
                                }
                                printf("%hhu\n",to->machine->sdl2->event.jaxis.axis);
                            }
                            //printf("X %hi Y %hi\n",SDL_JoystickGetAxis(to->machine->sdl2->joystick,0),SDL_JoystickGetAxis(to->machine->sdl2->joystick,1));
                        }
                    }
                    system("cls");
                }
Comment 1 Lísias de Castro 2018-02-22 01:49:41 UTC
I confirmed this is a bug in fact.
I made an abstract machine that can change
between allegro and sdl. The same code works
and everything but in the joystick axis.
The joystick button works, but when I try
to use the arrows it does not responds up and down.
The left, right responds, but it responds as up,
down together.
Can I do something else to help solving this?
That lib is almost working perfectly. Only this little ditail
to work fine.
Comment 2 Lísias de Castro 2018-02-27 04:45:27 UTC
I figured out a way to make the joystick work. But I don't think it is the normal way;
The x axis is answering as 1,2,3, and the y axis is answering as 4;
so when I want to get the x status, I can check for one of the 3 numbers, and when I want to check the y axis I have to check the number 4. So I can get the joystick status.

case SDL_JOYAXISMOTION:
            {
                if (machine->sdl->event.jaxis.which == 0) {
                    switch (machine->sdl->event.jaxis.axis) {
                        case 0:
                        {
                            if (machine->sdl->event.jaxis.value < -8000) {
                                bit_on(&machine->joystick.arrow, JOY_LEFT);
                            } else if (machine->sdl->event.jaxis.value > 8000) {
                                bit_on(&machine->joystick.arrow, JOY_RIGHT);
                            } else {
                                bit_off(&machine->joystick.arrow, JOY_LEFT);
                                bit_off(&machine->joystick.arrow, JOY_RIGHT);
                            }
                        }
                            break;
                        case 4:
                        {
                            if (machine->sdl->event.jaxis.value < -8000) {
                                bit_on(&machine->joystick.arrow, JOY_UP);
                            } else if (machine->sdl->event.jaxis.value > 8000) {
                                bit_on(&machine->joystick.arrow, JOY_DOWN);
                            } else {
                                bit_off(&machine->joystick.arrow, JOY_UP);
                                bit_off(&machine->joystick.arrow, JOY_DOWN);
                            }
                        }
                            break;
                    }
                }
            }
                break;

if you all don't fix it. At least can advert the people, in order for them to be able using their joysticks. I did what I could to aid. Now it is with you guys.
Comment 3 Sam Lantinga 2018-03-11 05:26:28 UTC
The joystick axis order isn't defined for any platform, and in fact, different APIs and different joysticks definitely return different ordering for different axes. You can see this by looking at the variety of mappings to the same controls that are in src/joystick/SDL_gamecontrollerdb.h.

If you want a pre-defined control layout, I recommend using the API in SDL_gamecontroller.h, otherwise you'll be making non-portable hacks for each platform and joystick.