Im using C++ and SDL2 to create a simulation in a 2D map of an object. This object uses time-based movement, and i use mouse clicks to determine where the object goes in the map. This object has 8 image sprites representing 8 directions to which it can go (N,S,W,E,NE,SE,SW,NW).
But clicking in some points in the map, it doesnt go straight in the same direction. Ex: to go somewhere NE, it goes straigth E then goes NE to the final position. I know how to determine the real-time position of the object in the xy-axis, but i need a condition in a class method that checks whenever the object changes direction in order to change the object sprite. I have tried several solutions, but nothing worked.
It would be something like this. This doesnt work, but you get the intention.
if(positionX ++ && positionY --){sprite = 2;}
Based on Retired Ninja´s comment, i have solved the problem. I just use the velocity to get the direction that the object moves at any time.
if (mVelX > 0 && mVelY < 0) { sprite = 0; }//NE
else if (mVelX > 0 && mVelY == 0){ sprite = 1; }//E
else if (mVelX > 0 && mVelY > 0) { sprite = 2; }//SE
else if (mVelX == 0 && mVelY > 0) { sprite = 3; }//S
else if (mVelX < 0 && mVelY > 0) { sprite = 4; }//SW
else if (mVelX < 0 && mVelY == 0) { sprite = 5; }//W
else if (mVelX < 0 && mVelY < 0) { sprite = 6; }//NW
else if (mVelX == 0 && mVelY < 0) { sprite = 7; }//N