Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: 8-direction movement problem  (Read 2879 times)

0 Members and 3 Guests are viewing this topic.

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
8-direction movement problem
« 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;

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
8-direction movement problem
« Reply #1 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;
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
8-direction movement problem
« Reply #2 on: April 05, 2010, 12:57:16 pm »
Thanks! :D