# Check if an object is facing another based on angles

I already have something that calculates the bearing angle to get one object to face another. You give it the positions and it returns the angle to get one to face the other.

Now I need to figure out how tell if on object is facing toward another object within a specified field and I can't find any information about how to do this.

The objects are obj1 and obj2. Their angles are at obj1.angle and obj2.angle. Their vectors are at obj1.pos and obj2.pos. It's in the format [x,y]. The angle to have one face directly at another is found with direction(obj1.pos,obj2.pos).

I want to set the function up like this: isfacing(obj1,obj2,area){...} and return true/false depending if it's in the specified field area to the angle to directly see it.

I've got a base like this:

``````var isfacing = function (obj1,obj2,area){
var toface = direction(obj1.pos,obj2.pos);
if(toface+area >= obj1.angle && ob1.angle >= toface-area){
return true;
}
return false;
}
``````

But my problem is that the angles are in 360 degrees, never above 360 and never below 0. How can I account for that in this? If the first object's angle is say at 0 and say I subtract a field area of 20 or so. It'll check if it's less than -20! If I fix the -20 it becomes 340 but x < 340 isn't what I want, I'd have to x > 340 in that case.

Is there someone out there with more sleep than I that can help a new dev pulling an all-nighter just to get enemies to know if they're attacking in the right direction? I hope I'm making this harder than it seems.

I'd just make them always face the main char if the producer didn't want attacks from behind to work while blocking. In which case I'll need the function above anyways.

I've tried to give as much info as I can think would help. Also this is in 2d.

-
Is the angle relative to the X axis? A quick drawing would help make sure of what your data means. – sam hocevar Nov 10 '11 at 17:21
yes, I think if I know what you mean lol. 0/360 degrees is facing completely right – Isaiah Nov 10 '11 at 17:23

``````var isfacing = function (obj1,obj2,area){
var toface = direction(obj1.pos,obj2.pos);
var left = toface - area;
var right = toface + area;

// Wrap the left and right angles
if(left < 0){ left += 360; }
if(right >= 360){ right -= 360; }

// Do 2 checks:
// - Where the left is smaller than the right, we should be between both of them.
// - Where the either the left or right has wrapped around 360 (left > right),
//   we only need to be facing such that we are either greater than the
//   left OR less than the right
if ((left < right && obj1.angle > left && obj1.angle < right) ||
(left > right && (obj1.angle > left || obj1.angle < right))
{
return true;
}
return false;
}
``````

I like to spread things out, and I would invest in a whiteboard.

-
Wow that works great! Hopefully when I'm done with this project I can share something back to everyone. I'm creating a whole canvas engine for this project and get to keep ownership of it! So far I have sprites, actors, collision, a keyboard system with over 200 hotkey profiles down/up, a mouse system for left/middle/right down/up, events for things like animation ended, path regions, levels with overlays, resource manager with html5 audio, a scene manager, and a simple behavior tree ai engine for the actors! So far the game I'm working on has 20+sounds/songs, and hundreds of animations! :D – Isaiah Nov 10 '11 at 17:59
apaliathegame.com address to the game website if you're curious – Isaiah Nov 10 '11 at 18:03
+1 for the whiteboard. – dlras2 Nov 10 '11 at 18:29
This is really too complicated. I don't mind the rep points, but if that is the accepted answer, maybe you could simplify it a bit by picking from my answer? – sam hocevar Nov 10 '11 at 18:41
@Sam, I happen to think it's simpler because it breaks the logic up... logically. Sure, yours is shorter and more concise, but I have to think harder. – John McDonald Nov 10 '11 at 18:52

You want the absolute value of the angle difference to be smaller than `area` or greater than `360-area`.

Based on your code, I believe this should work:

``````var isfacing = function (obj1,obj2,area) {
var toface = direction(obj1.pos,obj2.pos);
var difference = abs(toface - ob1.angle);
return (difference <= area || difference >= 360 - area);
}
``````

Edit: my first attempt was totally buggy. Sorry.

-
This should be the answer. The algorithm is as above, get relative direction (toface - ob1.angle) and check the absolute difference. – dcousens Nov 10 '11 at 22:07