Page 1 of 1

STK Controls

PostPosted: 29 Mar 2013, 14:39
by AntiFTW
Hi,

I'm working on a schoolproject which is about modifying an existing game to extend it so it can be controlled using something else then the standart input devices.

Now ive been looking through the code to find where the input is handled, but I cant really get my head around it.. there are several files that look promising but I cant seem to find the actual implementation of the input ( where the keyboard input is recognized and passed through).

I would really appreciate some assistance over where I could find this.

Thanks in advance,
regards,

AntiFTW

Re: STK Controls

PostPosted: 30 Mar 2013, 17:01
by Auria
Hi,

look under src/input. The DeviceManager will find all connected devices in initialize() and create an object for each of them; for keyboards, a KeyboardDevice; for gamepads, a GamePadDevice. Those devices can be found in file input_device.cpp.

The input event is first received in EventHandler::OnEvent, found under /src/guiengine.
The input event is then given to InputManager::input, which gives it to InputManager::dispatchInput,
which gives it to DeviceManager::translateInput. The DeviceManager then filters the input event through the list of devices by calling KeyboardDevice::processAndMapInput if a keyboard event, or by GamepadDevice::processAndMapInputif a gamepad event. If a gamepad event, which GamepadDevice object to used is determined by calling getGamePadFromIrrID. Each device object is then responsible to reckognize the input and map it to a game action if relevant.

So to map a new device, the easy way is to inject new events into the EventHandler, and add a new device to the devicemanager (possibly a GamePadDevice with a special ID) that will reckognize and accept this input

Re: STK Controls

PostPosted: 30 Mar 2013, 17:25
by AntiFTW
Thanks a lot for this clear and extended reply, I think I will able to figure it our with this information. If not I will let you know :)

Thanks again!!

Re: STK Controls

PostPosted: 02 Apr 2013, 06:48
by Funto
For information, injection of custom input events is what is done for the wiimote, you can take inspiration from that.

Re: STK Controls

PostPosted: 08 Apr 2013, 18:25
by AntiFTW
Okay,

So I found where I want to be, but because I am using sound input I need to have a seperate thread to update a variable, because it will interrupt the game if I dont. Is there a multithreading construct available within STK? Am using PortAudio to recieve mic input and try to turn according to that volume.

Thanks again :)
Anti

Re: STK Controls

PostPosted: 08 Apr 2013, 22:34
by Funto
Again, have a look at wiimote_manager sources, it does exactly that.

Re: STK Controls

PostPosted: 09 Apr 2013, 18:58
by AntiFTW
Ok thanks.. getting closer :)

Now I have another problem, which I dont understand, but it seems simple so maybe I am missing something:

I created a function in controller.hpp:

virtual void setMicValue(float mVal) = 0;

and only give the definition in playercontroller.cpp, so also added void setMicValue(float mVal); to playercontroller.hpp but it gives me an error:

3>d:\bewaren\uu\mmmi2\supertuxkart-0.8\src\karts\kart.cpp(782): error C2259: 'EndController' : cannot instantiate abstract class
3> due to following members:
3> 'void Controller::setMicValue(float)' : is abstract

What am I missing, there are several other abstract functions in there that only get defined in playercontroller??

EDIT: void setMicValue(float mVal) is a simple set function

EDIT 2: found a workaround, put the variable in the controller class and moved the setfunction there also, just as a void, not as a virtual void.

Re: STK Controls

PostPosted: 09 Apr 2013, 21:29
by AntiFTW
I got good news, my steering responds to the microphone, but I seem to have a problem of it not resetting, when I pushed a button (and released) it keeps steering to that side. Any quick tips? Will go look myself now but I had to post I had something working :) And maybe someone recognized the problem and knew exactly what to do :)

Thanks for all the quick responses, I got the basics working now (not that clean yet, should create a seperate thread, but its supposed to be a proof of concept) the clear and friendly responses helped a lot!!

If anyone wants to try it out when I got the bugs fixed, just yell :)

Going to look for that reset now.

Regards,

Re: STK Controls

PostPosted: 10 Apr 2013, 08:26
by hiker
AntiFTW {l Wrote}:I created a function in controller.hpp:

virtual void setMicValue(float mVal) = 0;

and only give the definition in playercontroller.cpp, so also added void setMicValue(float mVal); to playercontroller.hpp but it gives me an error:

3>d:\bewaren\uu\mmmi2\supertuxkart-0.8\src\karts\kart.cpp(782): error C2259: 'EndController' : cannot instantiate abstract class
3> due to following members:
3> 'void Controller::setMicValue(float)' : is abstract

What am I missing, there are several other abstract functions in there that only get defined in playercontroller??

If a class has an abstract function, it can't be instantiated (since this function doesn't exist). In this case, you added this function only to PlayerController. But the other controllers (EndController, and likely the AI controller) are all based on Controller, so they need this function defined as well.

Best solution is probably to only add this function to PlayerController (i.e. don't add it as abstract to Controller), and in the calling class cast the Conrtoller to PlayerController. Note that there is actually a function isPlayerController you can use to make sure you really have a player controller ;) From world.cpp:
{l Code}: {l Select All Code}
        if(!m_karts[index[pos]]->getController()->isPlayerController())
            continue;
        Kart *k = (Kart*)m_karts[index[pos]];
        PlayerController *controller = (PlayerController*)(k->getController());

Sorry, didn't find an easier example.


EDIT: void setMicValue(float mVal) is a simple set function

EDIT 2: found a workaround, put the variable in the controller class and moved the setfunction there also, just as a void, not as a virtual void.

Yes, that works, but if you only need it in PlayerController, the above solution is better.

Cheers,
Joerg

Re: STK Controls

PostPosted: 10 Apr 2013, 14:50
by AntiFTW
Thanks, I get what you mean :) Ill change it indeed.. Still having some problems understanding why it doesnt reset, only change I made is

1)getting a mic volume value using Portaudio(between 0 and one),
2)and I added this code to the player controller (the float micValue is available within the class so)
case PA_STEER_LEFT:

if(micValue<0.5)value=0;
else if(micValue>0.5 &&micValue<=1.0)value=micValue*32768;
else if(micValue>1.0)value=1;
m_steer_val_l = value;

So instead of getting the value from the action() function, I use a value that gets updated every mainloop().. the 0.5 is to filter out noise on the mic input, and it works with steering, but sometimes it keeps one direction locked.

1*gives some strange execution (it only works on recordings under 0.1 seconds if I am in a Skype call) took me a while to figure that out and not really sure what to do about that, but it works with skype so I can test it at least :P

Re: STK Controls

PostPosted: 11 Apr 2013, 03:08
by hiker
AntiFTW {l Wrote}:Thanks, I get what you mean :) Ill change it indeed.. Still having some problems understanding why it doesnt reset, only change I made is

I don't have a really good idea. Note that there might be some smoothing happening, if STK thinks your device is digitial (i.e. value is 32768, an analog device should report values up to 32767 only).

Otherwise you have to debug this, probably by printing the values you receive, and the ones that are sent to STK. There is also the command line option "--gamepad-debug"n which already prints some values. Perhaps that will give you a clue.

Cheers,
Joerg

Re: STK Controls

PostPosted: 18 Apr 2013, 23:31
by Funto
So, the more you yell to your PC, the more the kart steers??
That's a curious gameplay :D