Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quakeboy/6256434 to your computer and use it in GitHub Desktop.
Save quakeboy/6256434 to your computer and use it in GitHub Desktop.
Converting angle to indexes, or vector -> angle -> indexes.. I have done this at least 4 times in my life for various games/projects before, yet have to do it each time again. So here is a gist for it
float degangle = atan2f(t_dir.x, t_dir.y)*180/M_PI;
//pushing it down, so that -22.5 becomes the new 0
degangle += 22.5f;
//now making the final range as 0 - 360
if (degangle < 0) degangle += 360;
//now this var is ranged 0 - 7, with -22.5f to 22.5f being 0 and so on...
m_DirectionIndex = (int)floorf(degangle/22.5f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment