I'm trying to do it myself right now, but there's a ton of things wrong with it - for example, the object won't slow down to a halt if I let go of the directional key. For some reason, it speeds up.. if anyone could help me with this, I'd greatly appreciate it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// in main.cpp
typedef sf::Vector2f v2f;
if (Input.IsKeyDown(sf::Key::Left))
player->Move(v2f(-.5,0));
elseif (Input.IsKeyDown(sf::Key::Right))
player->Move(v2f(.5,0));
elseif (Input.IsKeyDown(sf::Key::Up))
player->Move(v2f(0,-.5));
elseif (Input.IsKeyDown(sf::Key::Down))
player->Move(v2f(0,.5));
else
player->Move(v2f(0,0));
// So that even if the user does not add motion to the object,
// it will still be able to move.
1) You have no condition for if (dif.x == 0 && dir.y == 0) to reset the accel variable. So when no key is pressed, it's going to use the last value. Because you're always adding the accel to the velocity (line 30/31) the accel will forever increase once no key is pressed.
2) You're not reducing the amount of accel/velocity when no key is pressed. This is tied closely with 1).
Thanks, motion is still a little weird, but at least it slows down properly. I guess I'll have to actually study the physics of an object if I want to do this more accurately, though.