Monday, 12 August 2013

How to detect where in a defined area a tap happens relative to a point

How to detect where in a defined area a tap happens relative to a point

So, in my game, the player has 8 stances, 0-7. (0=north, 1=northeast,
2=east, etc. ) if I get taps, how do I detect which "slice" it is in? This
is how my code performs as of now.
- (IBAction)tapped:(id)sender {
CGPoint loc = [((UITapGestureRecognizer *)sender) locationInView:self.view];
GLKVector2 offset = GLKVector2Subtract(GLKVector2Make(loc.x, 320 - loc.y),
testEntity.sprite.position);
float radians = atan2(offset.y, offset.x);
}
Where do I go from here? The player entity has a method for turning, but
the atan2 returns a lot of random numbers. Is there an easy way to detect
where the player should turn?
Edit: So now I realize these are in radians. How do I detect which eighth
of the "pie" it is in? Also, I figured out to get it to degrees by making
sure that if radians < 0 then I add it to ð*2. Is there a way without
going like if(degrees > 270 - 35 && degrees < 270 + 45) over and over
again to detect the "slice?"
Thanks in advance,
Joe

No comments:

Post a Comment