Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Relating to this question; What's the best way of translating a 2D vector into the closest 8-way compass direction?

My query is if this would work in the following order(below) and if so, how would i achieve that?

 enum compassDir {
    S = 0, SE = 1,
    E = 2, NE = 3,
    N = 4, NW = 5,
    W = 6, SW = 7
share|improve this question

marked as duplicate by msell, Seth Battin, Byte56 Jul 25 '13 at 13:05

This question was marked as an exact duplicate of an existing question.

-1 Are you asking for clarification to the answer of another question? Write a comment instead. – Anko Jul 19 '13 at 11:00
Your starting angle should have an offset. In 2D that would be +90 degrees. or +PI/4 radians. Since south is pointing downwards. – Sidar Jul 19 '13 at 11:31
I feel like this question could easily be answered by trying it. This is almost entirely a coding challenge. The answer I gave goes clockwise around the compass, so the enums are ordered in that way. You have your enums with a different offset and order. Think about how you might reverse the order the while loop traverses the compass. Yes it's possible, but I'm not going to write the code for you :) I agree with Anko, this should be a comment on the original post. – Byte56 Jul 19 '13 at 12:57
Yeah sorry guys, dumb question. I had it the right code all along but after the clarification by Sidar, i realised i had made a typo when the direction gets set. Shit happens :p – dan369 Jul 20 '13 at 11:20

The unit vectors for the eight compass directions can easily be hardcoded into an array in the same order as your enum. Then one simply uses the dot-(or inner-)product of the given vector against the vector array. The closest compass direction will yield the maximal dot-product value.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.