The way i did it to index directions is to take that direction and turn it into a bering then basically turn that into a index based on the closest ranged value. You can use the dot or cross to get radians and do the below that way but im going to explain the idea with degrees.
Since you already figured out how to get the facing into a vector2 you can then take that vector2 and use Math.Atan2() on it to get a radian (you can use ToDegrees to change it to degrees), this is now in bearing form.
You should wrap the degrees you got to be within 0 to 360 to keep it straight forward.
if(degreeResult < 0)
degreeResult += 360;
if(degreeResult > 360)
degreeResult -= 360;
Then you calculate were that direction falls within a series of images in a spritesheet.
To do that you create a predefined orientation limit per sprite.
Or… which i recommend instead you calculate the index directly based on the above value.
to expound on both…
Say you have 8 sprite facings or 16 or what not.
If you wished to define per sprite just its own limits you would set up each sprite to have a range.
sprite0 is more then 315 degrees and less then 45 degrees then sprite index = 0
sprite1 is more then 45 degrees and less then 90 degrees then sprite index = 1
sprite2 is more then 90 degrees and less then 135 degrees then sprite index = 2
ect…
The above is just to show in relation to how you are doing it already.
However this above sort of switch case can lead to some restrictive extensibility problems down the road when you have variable sized animations per character with more then one character in more then one spritesheet.
You can just find the index by algorithm directly this requires that you format your sprite sheets to have a certain standardization or look to them if you like, or that you maintain a list of source rectangles that have a logical ordering. Which you may already have.
You organize your images on your sprite sheet so they appear in a specific order by there looks that match a characters apparent visual rotation (clockwise or counter clockwise) this is a preferential choice which direction a character is facing be the starting direction a character faces in a sheet.
Whichever choice you make, it should be standardized to all sprite sheets to keep everything simple and clean.
You could then use the direction your sprite is facing from the math.atan2 function like so to determine the source rectangle to draw.
int numberOfFacingsPerThisAnimationSet = 8;
// it could be that for the above value all your character animations have 8 images then this is constant, or they don't and this changes depending on the character and animation in use.
int degreeFacingRange = 360 / numberOfFacingsPerThisAnimationSet;
int spriteIndexOffset = (degreeResult + (degreeFacingRange /2)) / degreeFacingRange;
if(spriteIndexOffset >= numberOfFacingsPerThisAnimationSet )
spriteIndexOffset = 0;
int spriteIndex = charactersAnimationSetStartOffset + spriteIndexOffset;
SpriteIndex then just determines which sprite in the sheet is choosen from left to right in a row column or from some starting index were a series of animations begin.
To say for a spriteSheet you have a array of incrementing source rectangles and also have sets of animated sprites for each direction they can face. You have a offset to were each animation set begins and this value is added from the starting offset for a animation to determine which source rectangle index in the sheet (in concert with the current animations starting offset) is to be used to draw the sprite.
The code i just typed out is unchecked pseudo code but its so simple it’s probably right as is.
As you can see this is mostly a standardization of your spritesheets which makes the math part simpler structured and extensible to variable sized animations and more then one character with animation sets in a spritesheet.