# Node looking at another node check? [SOLVED]

Hey guys,

This seems like an easy problem, but I can’t seem to get it solved!

I know there is a lookAt() function that will point a Quaternion to a certain point… however, how do you detect if a Quaternion is looking at a certain point (within a certain angle range)? I want my enemies to only detect the player if they are looking at the player!

I was trying to use the Yaw/Pitch angles of the “camera” node, passing it through atan2 without much luck… I’d like to know the “right” way to do it

Thank you!

i guess rays would come in use here, start ray from enemy to where hes facing and if it picks up player, go attack

Hello!

A while back I had a discussion here about using boundingSpheres that could be adapted to work with what you’re saying. Not sure if it’s the best solution, but it would allow you to define a “field of view” for your enemies to aggro your player.

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/boundingvolume-questions/

Essentially involves a boundingSphere that you attach to your enemy arbitrarily (In other words, It’s just there for this calculation. This is NOT the physics bounding check, this is just regular ol’ object v object checking). On your enemy’s update you’d want to check if the player is touching/enveloped in this sphere, and furthermore using some dot product magic ™ to figure out if the player is in the enemy’s field of view. Using this technique, if the player is behind the enemy, the dot product will come back to tell you that the enemy cannot see the player. ~ In a nutshell.

Pseudo:

1. Check to see if the player is in my bounding sphere that defines my view distance
2. Check to see if the player is in my field of view (front dot product)
3. (OPTIONAL) Check if something is obstructing my view of the player

(3) Prevents aggro through walls, but I wouldn’t worry about it until you’re ready to worry about it.

Using dot products made me feel like some sort of awesomely powerful math wizard or something. That stuff is so useful XD

Cheers!

~FlaH

Thank you for the replies

ryuu said:
i guess rays would come in use here, start ray from enemy to where hes facing and if it picks up player, go attack

The problem I forsee with using a ray is that it'd need to be exactly pointed at the player... if the enemy is looking just off to the side, the ray will miss the player -- even though the enemy would "see" the player. :/

tehflah said:
Hello!

A while back I had a discussion here about using boundingSpheres that could be adapted to work with what you're saying. Not sure if it's the best solution, but it would allow you to define a "field of view" for your enemies to aggro your player.
...

HHrrrrmmmm... I suppose this would be possible, but it does seem a bit inefficient..? I'd have to store another sphere collision object with each "enemy" object, and move it into proper position for each "sight" check... I'd think (hope) there would be a way to do this with a few Quaternion & Vector3f calculations.. unique idea that I didn't think of, though... maybe I could make this work... until then, any other suggestions? :/

Either you use multiple rays at intervals (which is not exact, but good enough for several AAA games out there), or you need to think of doing some kind of frustrum. I wouldn’t know how to do that, but i guess looking at how the Camera calculates what do draw might be a good start.

Got it!

Yaw/Pitch are the already calculated and stored Yaw & Pitch values for myNode (we are testing if myNode is looking at target.myNode):

[java] // are we even looking at the target?

Quaternion rotator = new Quaternion();

rotator.lookAt(target.myNode.getWorldTranslation().subtract(myNode.getWorldTranslation()),

Vector3f.UNIT_Y);

rotator.toAngles(tempAngles);

if( Math.abs(Helpers.AngleDiff(tempAngles[1], Yaw)) < 0.85f &&

Math.abs(Helpers.AngleDiff(tempAngles[0], Pitch)) < 0.85f ) {

// it is in our view! should do distance checking / obstructing object checking now…

}[/java]

Helpers.AngleDiff code:

[java] public static float AngleDiff(float ang1, float ang2) {

float ang = ang1 - ang2;

if (ang > 3.141593f) ang = -(6.283185f - ang);

if (ang < -3.141593f) ang = (6.283185f + ang);

return ang;

}[/java]

EDIT: Basically, we get the Yaw/Pitch values of our node if it were looking exactly at the player. Then, compare how different these values are to the current node’s Yaw/Pitch values. If the values are too far off, we are looking away!