SFML community forums

Help => Graphics => Topic started by: SuperV1234 on April 05, 2010, 11:40:01 am

Title: 8-direction movement problem
Post by: SuperV1234 on April 05, 2010, 11:40:01 am
I created a simple 8-dir movement for my game, however when moving in a diagonal direction the player goes faster than intended.

This is the code. How can I prevent it?

Quote

            switch (input)
            {
                case KeyCode.Left:
                    nextX = -1;
                    break;
                case KeyCode.Right:
                    nextX = 1;
                    break;
                case KeyCode.Up:
                    nextY = -1;
                    break;
                case KeyCode.Down:
                    nextY = 1;
                    break;
            }

            X = X + nextX * speed;
            Y = Y + nextY * speed;

            nextX = 0;
            nextY = 0;
Title: 8-direction movement problem
Post by: Nexus on April 05, 2010, 12:38:06 pm
Decrease the step size when two keys are pressed simultaneously.
Code: [Select]
if (nextX != 0.f && nextY != 0.f)
{
    nextX *= 0.707106781f; // == sqrt(2) / 2
    nextY *= 0.707106781f;
}
Title: 8-direction movement problem
Post by: SuperV1234 on April 05, 2010, 12:57:16 pm
Thanks! :D