Sign in to follow this  
shezad

Joypad controller input

Recommended Posts

shezad    136
Hi, I need to be able to get a combination of key press and joypad movement to decide an action, does anybody have any idea the best way to do it? For example i've got the class joy class joy{ getButton1(); getButton2(); GetStickDirection() }; the logic would be something like : if(button 1 and direction x) does this But I wouldn't like to use a lot of if/then/else Cheers, Shezad

Share this post


Link to post
Share on other sites
M2tM    948
What you probably want is to register your actions to some kind of a callback which determines if the action's trigger is being satisfied.

Instead of having accessors for figuring out each and every kind of button, instead store a state of the exact keyboard and joystick configuration. In SDL I know you can do this with the keyboard state, but I'm not sure about the joystick state. In any case you'd probably want to wrap that together into a cohesive interface.

Set up a function object to deal with the activation of the action:
*Edit: Modified to be "UpperCut" instead of a "JumpLeft", read the comments below for clarification.

struct UpperCut{
UpperCut(key One key Two):Activate(One), Direction(Two){}

bool operator()(InputState &inputState, Character &character){
if(inputState.isDown(Activate) && inputState.isDown(Direction)){
return character.upperCut();
}
return false;
}

key Activate;
key Direction;
}








Then you could register all your actions with some object which basically loops through all of the action functors and calls them each input cycle. This is really a contrived example to show what you were asking (with a joystick direction and a button press), more realistically you'd probably want to have a general jump command and pass in some kind of directional vector based on the joystick input state. You could do that with this approach as well.

Of course you also want to be able to avoid hard-coding keys, but some actions may require only one key and others may require two or three. This is only one approach and you may think of a better one. Good luck!

[Edited by - M2tM on March 17, 2010 6:09:31 PM]

Share this post


Link to post
Share on other sites
bzroom    647
I usually try to keep it continuous.


Vec2 moveVec = joystick.XY( );

velocity.x = Lerp( velocity.x, moveVec.x, 0.25f );
velocity.z = Lerp( velocity.z, moveVec.z, 0.25f );

if( onGround && joystick.Button1( ) )
velocity.y = JUMP_VEL;

OrientCharacterToVelocity( velocity );
SetAnimationSpeed( moveVec.Length( ) );


I dunno, something like that :P

Share this post


Link to post
Share on other sites
M2tM    948
Of course, as I said, that example was contrived as directions tend to be vectors, but the idea of jumping left is a simple one and the one that jumped into mind first (when trying to satisfy the condition of a direction and a button) which is why I based the very simple example on that.

You'd more likely have the example be a one button jump command and deal with direction separately.

Re-thinking it a better example may have been one such as an uppercut by pressing up and a at the same time or similar.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this