Skip to main content
Adding a missing line of code, thanks Sam
Source Link
John McDonald
  • 6.8k
  • 2
  • 33
  • 46
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.

var isfacing = function (obj1,obj2,area){
    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.

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.

Source Link
John McDonald
  • 6.8k
  • 2
  • 33
  • 46

var isfacing = function (obj1,obj2,area){
    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.