Jump to content
  • Advertisement
Sign in to follow this  

SDL input player movement problem

This topic is 3722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a set up much like the second example in this tutorial: http://gpwiki.org/index.php/SDL:Tutorials:Practical_Keyboard_Input However I wish so that when moving in a diagonal it doesn't simple move the player say left and up by the same velocity units as that would mean moving at a diagonal moves you faster. So I have this little bit of code: float diagonal = m_MovementSpeed -(0.70710678118654752440084436210485 * m_MovementSpeed); It in effect is performing what trig would since the player only moves 8 directions. However on trying to implement this diagonal movement velocity change my code has become an almighty mess. It's a horrific mess of bools and if statements across the key down and key up events. It works to a point but it fails when I have 3 movement keys pressed. Can anybody come up with a cleaner and bug free method of implementing it? That means movement is always what it is meant to happen. For example say left, down and right are pressed then the player should move down m_MovementSpeed and then if right is released you go diagonal at the reduced velocity of x and y. Oh and to complicate matters in the game you can control either of 2 characters. If you hold a key (currently m for no reason) you move one of them and if you release it you move the other. They are separate objects (OO) and because of their relationship one is a data member of the other. That is currently controlled by a bool in both objects, and on input both bools are changed to represent whether they are being controlled or not. I've got myself in a muddle over this. Guess it's simple and I just need to come back to it later but it's bothering me. I'm sure you know the feeling.

Share this post

Link to post
Share on other sites
xvel = 0; yvel = 0;
if(left key down)
xvel +1
if(right key down)
xvel - 1
if up key down
yvel +1
if down key down
yvel -1

normalize vel vector.

As a quick idea any way, just figure out the direction then normalize the vector.

Share this post

Link to post
Share on other sites
The problem is that it doesn't account for the different velocity when moving diagonally. For instance:

if(m_dirUp == true && m_dirRight == true)
m_VelY += diagonal;
m_VelX -= diagonal;

So to then cancel that out doesn't work. where velocity is normally 2 it would happen that moving up/right diagonally and then releasing right would make x velocity:

x velocity = 1.42 - 2

So I have accounted for that with more if's and bool variables. Every time I do that I find that something doesn't work and I add more. So the solution is to have a very long list of if statements working out exactly what is pressed at any given time and what was released and performing the correct changes to the velocities. However that is already ugly, hence why I'm wondering if anyone else has a solution. I'm hoping I'm not the only person who has this setup and wants too have 8 way movement that is fairly accurate compared too diagonals moving faster.

Share this post

Link to post
Share on other sites
Original post by DontPanic
The problem is that it doesn't account for the different velocity when moving diagonally.

It does: that's what normalising the vector means.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!