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

Author Topic: Best way to program 8 directional movement?  (Read 2029 times)

0 Members and 1 Guest are viewing this topic.

TehKyle

  • Newbie
  • *
  • Posts: 8
    • View Profile
Best way to program 8 directional movement?
« on: March 13, 2011, 05:18:49 pm »
I want to reduce speed on diagonal movement, but I don't know how do code it properly.

Also, how do you deal with Left + Right and Up + Down conflict?
If you code something like

if (L) xFlag= -1;
if (R) xFlag= 1;
if (U) yFlag= -1;
if (D) yFlag= 1;
Move (xFlag, yFlag, framespeed)

You have Right and Down overwriting Left and Up when you press them together.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Best way to program 8 directional movement?
« Reply #1 on: March 13, 2011, 05:32:27 pm »
Code: [Select]
const float speed = ...;
sf::Vector2f v;

if (left)    v.x -= speed;
if (right)   v.x += speed;

if (up)      v.y -= speed;
if (down)    v.y += speed;

if (v.x != 0.f && v.y != 0.f)
    v /= std::sqrt(2.f);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: