OpenClaw icon indicating copy to clipboard operation
OpenClaw copied to clipboard

joystick or controls

Open Noisk8 opened this issue 6 years ago • 5 comments

Is it possible to change the controls or use a joystick? what would be the proper way to include SDL_joystick in the ActorController.cpp and .h file?

Noisk8 avatar Nov 21 '18 23:11 Noisk8

Uh, haven't seen the code for quite some time :) But here goes:

  1. Create some new config option in config.xml file, e.g. <UseJoystick>true</UseJoystick> and save it to BaseGameApp::m_GlobalOptions at startup with default value for this being false.

  2. In these methods:

ActorController::Update ActorController::VOnKeyDown ActorController::VOnKeyUp

Implement custom logic based on the key which is being handled.

  • In ActorController::Update you should handle keys which are polled every frame - e.g. get position of the joystick and translate it to move/climb/jump speed/direction
  • In ActorController:VOnKeyDown/VOnKeyUp handle events - like when you press a joystick button and want to shoot or attack - this is handled here

Someone added some "alternate" controls via some pull request which enabled controlling of claw via WASD and shotting/attack through mouse buttons. This is distinguished by e.g. this line:

https://github.com/pjasicek/OpenClaw/blob/master/OpenClaw/ActorController.cpp#L55

Hope this helps.

pjasicek avatar Nov 22 '18 19:11 pjasicek

Thanks to see if I understand, I am not clear how to do the config.xml file, I use linux and see that there is a config file linux

the line would be something like this?

(SDL_GetScancodeFromKey(key) == SDL_SCANCODE_LALT || SDL_GetScancodeFromKey(key) == SDL_CONTROLLER_BUTTON_A )

Noisk8 avatar Nov 22 '18 21:11 Noisk8

Well I looked at the "Controller" abstraction code by SDL and it is not that simple at all. Game Controller input is handled by a different subsystem so it is handled differently than keyboard. You can look here for more details:

http://lazyfoo.net/tutorials/SDL/19_gamepads_and_joysticks/index.php

In the engine startup routine you would have to add initialization of the SDL Game Controller and keep the reference to the controller somewhere in the g_pApp. Then when processing input you would have to check for any game controller events and handle them accordingly.

pjasicek avatar Nov 22 '18 21:11 pjasicek

Here are 3 commits for my basic implementation of controller support: https://github.com/mialy/OpenClaw/commits/controllers

I'm really bad at C/C++, and what stops me even more is I was unable run build on Windows with Visual Studio (Linux works though) . Still, implementation supports Xbox One S controller + some other NES-like, but with fixed buttons, meaning if button "1" in your controller is identified as "menu" and not "A" or "X" button, you have bad luck. My plan was to implement key mapping in C# launcher, but now I haven't much time. I'm glad, If this code could helps somebody.

mialy avatar Feb 09 '19 15:02 mialy

If I connect my joystick is activated the menu controls? or just activates the buttons automatically?

Noisk8 avatar Mar 04 '19 20:03 Noisk8