detect compass position

trying to work out what way im facing
would like to print out what way im facing. but its not working like it should

here is a picture explaining the positions

would also like to print NE ES an so on
just havent worked out the basic must be something simple

http://i.imgur.com/V9G1Bd9.png

1
2
3
4
5
6
7
8
9
10
		if (forward.x < -1.0f && forward.x < 1.0f && right.x > 0.0f)
			return "North";
		else if (forward.x > 0.0f && right.x < 1.0f && right.x > -1.0f)
			return "East";
		else if (forward.x < 1.0f && forward.x > -1.0f && right.x < 0.0f)
			return "South";
		else if (forward.x < 0.0f && right.x > -1.0f && right.x < 1.0f)
			return "West";
		else
			return "";
Last edited on
but its not working like it should
What the problem exactly is?
I see problem with first condition, with forward.x < -1.0f

I suggest to convert your cooeds into polar angle and work with it. It will be easier to implement other directions that way
it dont seem to print out all the directions, north never got printed.

polar angle??
polar angle??
http://en.wikipedia.org/wiki/Polar_coordinate_system#Position_and_navigation

north never got printed.
I posted the problem:
forward.x < -1.0f && forward.x < 1.0f Do you see it?
Your if statements are not going to do what you think they're going to do:
 
forward.x < -1.0f && forward.x < 1.0f 

The second condition is irrelevant. If x < -1, your if statement is fine. Both conditions will evaluate true. However, there is no point to the second condition. If you're trying to catch x > 0 and < 1, that second condition will never execute because any positive number will cause the first condition to fail.
oops yeah i see that thanks.
i will look into polar seems confusing

lol now im only getting north, south, west
Last edited on
Just think of it as an angle from 0 - 360.

i was never good at maths
Simply treat it like a compass.

0 degres is North.
90 degrees is East.
180 degrees is South.
270 degrees is West.

For four directions, you want to test +/- 45 degrees.
e.g. If a heading is > 315 (360-45) degrees and < 45 degees, then that is a "northerly" direction.

If you want to use NE, SE, SW and NW, then you want to break your compass into 8 sectors. Hint: +/- 22.5 degrees
Topic archived. No new replies allowed.