# Field of view!

I’m currently prototyping some solutions and implementations for a project that I wish to start working on!

However I have never developed a game before, and my expertice is in webdevelopment, so I’m not that well read up on best practices and common solutions for problems.

I have come quite far in prototyping and implementing some of the basic game logics, like moving, interacting with objects, and so on.

Next step in my road map before I have enough tools and experiance to start the actuall work of the game engine I need to develop some sort of “field of view” for living objects in the world.

The objects need to be able to react to when player is closing up on them, in their f"ield of view"!

Sphere

One way i thought of implementing this is by creating a “invisible” sphere arround the objects with a radius that acts as a “aggro range”.

But I had a hard time figuring out how to make the object react different if the player closed up to the object from the rear, or from the blind sides left and right!

Triangles

Then I figured out i could use triangle shaped meshes;

One infornt of the object functioning as the “field of view” drawing out from the player kinda far.

Two on each side, so that developers could handle actions when players enter the objects blind sides.

And one in the back.

Then i could fire some events, or look for a colision on these meshes in an update method for the object to then make the object react upon the player closing in!

How would any of you more experianced developers approach this “issue”?

Thank you

Sounds like a fair solution, its totally dependent on how you set up your game. For networking/mmo style games its often hard doing this in a proper manner, so one might derive that info from position, distance and looking direction for singleplayer/local games its easy to use geometry info to deliver exact results.

Ok! Thank you for the input normen!

Finally i managed to finnish the “FOV and reaction areas” for players and “NPC’s”…

Pretty proud actually!

http://img35.imageshack.us/img35/4582/fovprototype.png this is how the fields are organized. Tomorrow I’ll start working on making NPC’s react when players enter these fields.

• I know that the model is rotated in a strange way, but it’s because it’s currently following the camera in every angle.
1 Like

This reminds me of some pretty simple but effective code a friend of mine once did for rubberband selection (which is effectively a pyramid shape in the world). It checks if a point is inside the pyramid by sending a ray in a random direction from that point and seeing how many faces of the pyramid it hits. (two or zero=outside, exactly one=inside)

t,b1,b2,b3,b4 is the top and base points of the “pyramid” (have to be circular from 1-4!):

[java] public boolean isVectorInPyramid(Vector3f v){

int cnt = 0;

Ray ray = new Ray(v, rndVec());

//seiten

if ( ray.intersect(t, b1, b2) ) cnt++;

//if (cnt>1) return false;

if ( ray.intersect(t, b2, b3) ) cnt++;

if (cnt>1) return false;

if ( ray.intersect(t, b3, b4) ) cnt++;

if (cnt>1) return false;

if ( ray.intersect(t, b4, b1) ) cnt++;

if (cnt>1) return false;

//unterseite

if ( ray.intersect(b1, b2, b3) ) cnt++;

if (cnt>1) return false;

if ( ray.intersect(b1, b3, b4) ) cnt++;

if (cnt>1) return false;

if (cnt==1)

return true;

else //->(cnt==0 || cnt>1)

return false;

}[/java]

…but I guess your method is more flexible and maybe even faster

The fastest method would be your initial sphere collision approach + vector maths. When there is an entity inside your “aggro” range you further do a cross product of the enemy’s view vector and your current position. From the product you can instantly deduce the angle between you and his line of sight. Sphere collision is fast and cross product too. I think this would be the “standard game programmers” way to do it.

Urks, I meant dot product, of course. Dot product gives you the cosine of the angle between two vectors and the cross product gives you a vector that stands perpendicular to the given two. Always mix them up

Ahh, the deeper I go in to this game programming world, the more I realize that I need to get better at math!

Well, I’ll wrap up my current implementation, and then I’ll go on and see if I can implement your advice Dodikles! Thank you

The underlying math of the collision system is quite good, I think your solution might be just as fast as @Dodikles’ when you have more complex situations where you have to add other things like to the equation like walls obstructing the view etc. The pure location/angle thing is the solution I was hinting at as being “for mmo” because you don’t have much more info. But again, totally dependent on what you want to achieve.

Ahh finally I got the time to finnish the code.

http://pastebin.com/wFbZxevU LifeFormObject

http://pastebin.com/4RBDApJX ReactionField

Anything that I did really stupid here? things I could have done better?

Looks good but I’d suggest making it a Control for the Spatial instead of extending the CharacterNode (which is deprecated in the current svn anyway) this way you can also make vehicles or anything else “view enabled”.