Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm not sure if the title matches what I'm actually facing

I'm trying to test if I can see a point is in my field of view (in the context of an NPC seeing in it's direction.)

I know that:

cos0 = (A.B)/||A||||B||

We have our point x. We have our heading h. We have our position p.

cos0 = (h.(p-x))/||h||||x-p||

So to test whether a point is in it's view we simply check if

cos0 >= view/2;

But for some reason the bigger the angle, the closer you have to be to the center in order for it to detect you. For smaller angles it's the opposite:

Here is what I have now. (click the " Play flash full screen " , for some reason the hosting settings won't show it in it's original size)

You can see that the bigger views actually respond only when you are near the center. While the smaller views start rotating even though you're not even in their view.

for now I'm using the mouse coordinates. I'm pretty sure all vectors operate in the same space coordinates.

Properties and constructor:

private var heading:Vec2 = new Vec2(); // Always a unit vector
private var position:Vec2 = new Vec2();
private var fovDeg:Number = 30.0;
private var fovRad:Number = 0.0;
private var distance:Number = 300;
private static const toRad:Number = (Math.PI / 180);

public function Fieldofview(degree:Number = 45.0 ) 
{
   fovDeg = degree;
   fovRad = fovDeg * toRad;
   fovRad /= 2;
   heading._x = 1.0;
   heading._y = 0.0;
   drawField();
}

The function to test wether it's within the view


public function pointIsInView(p:Vec2) : Boolean
{
    //vector from our position to point to test 
    var diffVec:Vec2 = p.sub(position); 
    var diffNorm:Vec2 = diffVec.normalize();
            //both are unit vectors so the dot should be enough
    var dot:Number = diffNorm.dot(heading);

    if (diffVec.length() > distance ) {
        return false;
    }

    //Check if we are in bound
    if ( dot >= fovRad) {
        return true;
    }

    return false;

}

Anyone have an idea what is causing this?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

"dot" is a cosin not an angle, can not be compared to "fovRad" that is an angle

You can get angle of "dot" with an arccosine function, or get the cosine of "fovRad" to compare them.

share|improve this answer
    
You're right. I had to compare it to the ratio not the radiance. Which I was stuck up on. Thanks! –  Sidar Jan 30 '13 at 14:13
//Check if we are in bound
if ( dot >= fovRad) {
    return true;
}

You are comparing cos(0) to fovRad.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.