|
Fluid object movement with gamepad stick [problem] |
gamedevmike
Member #16,733
September 2017
|
Hello. The problem I encountered is that I somehow cannot make my character move (right, left) fluently when the left stick is held (position 1.0 or -1.0 on axis 0, stick 0). Here's how I'm trying to achieve it: 1if( event.type == ALLEGRO_EVENT_TIMER )
2{
3 al_get_joystick_state( joy1, &gamepadstate )
4 if( gamepadstate.stick[0].axis[0] && event.joystick.pos == 1.0 ) position += movespeed;
5 if( gamepadstate.stick[0].axis[0] && event.joystick.pos == -1.0 ) position -= movespeed;
6}
This is based on keyboard movement procedure, which looks like this: 1if( event.type == ALLEGRO_EVENT_TIMER )
2{
3 if( al_key_down( &keystate, ALLEGRO_KEY_RIGHT ) ) position += movespeed;
4 if( al_key_down( &keystate, ALLEGRO_KEY_LEFT ) ) position -= movespeed;
5}
Unfortunately, such an approach does not work. Position output is valid under And finally some questions. Thanks in advance. Sorry if I'm unclear anywhere. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Simple answer - don't use polling, use events and keep track of the state yourself. You may find the events have correct data where the joystick state does not. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
beoran
Member #12,636
March 2011
|
An analog stick may not report an axis at a full 1.0 or - 1.0. This is normal for analog sticks since they can sense how much you are tilting the stick. In stead of comparing with 1, set a threshold for motion >0.1 or so. |
gamedevmike
Member #16,733
September 2017
|
Thanks for answers. @beoran Tried this. It's not the case. |
Audric
Member #907
January 2001
|
The error is that for ALLEGRO_EVENT_TIMER event, you are allowed to read event.timer, but not event.joystick - the latter will read as "garbage" data. You can use something like this: 1if( event.type == ALLEGRO_EVENT_TIMER )
2{
3 bool controler_left = 0;
4 bool controler_right = 0;
5 // poll keyboard
6 ALLEGRO_KEYBOARD_STATE keystate;
7 al_get_keyboard_state(&keystate);
8 if( al_key_down( &keystate, ALLEGRO_KEY_RIGHT ) ) controler_right = 1;
9 if( al_key_down( &keystate, ALLEGRO_KEY_LEFT ) ) controler_left = 1 ;
10
11 // poll joystick
12 al_get_joystick_state( joy1, &gamepadstate );
13 float axis_x = gamepadstate.stick[0].axis[0];
14 if( axis_x > 0.3) controler_right = 1;
15 if( axis_x < -0.3) controler_left = 1;
16
17 // logic
18 if (controler_right && !controler_left) position += movespeed;
19 if (controler_left && !controler_right) position -= movespeed;
20
21}
The alternative is skip the part where you poll the entire keyboard/joystick, and instead react to the events, for example : if( event.type == ALLEGRO_EVENT_JOYSTICK ) { // A movement of the horizontal axis on first joystick if (event.joystick.stick == 0 && event.joystick.axis == 0) { float axis_x = event.joystick.pos; if( axis_x > 0.3) controler_right = 1; if( axis_x < -0.3) controler_left = 1; } }
|
|