0 Members and 1 Guest are viewing this topic.
private boolean inFOV2(Bot b, double uAngle, double lAngle){ double dist = Math.sqrt((xPos - b.xPos)*(xPos - b.xPos) + (yPos - b.yPos)*(yPos - b.yPos)); if(dist < 0){ dist = .001; } double buffer = Math.toDegrees(Math.asin(b.radius/dist)); double angle = Math.toDegrees(Math.atan2(b.yPos - yPos, b.xPos - xPos)); uAngle += buffer; lAngle -= buffer; return (uAngle - angle >= 0 && lAngle - angle <= 0); }//Called viainFOV2(b, angle + FOV, angle - FOV)
double angle = Math.toDegrees(Math.atan2(b.yPos - yPos, b.xPos - xPos));
Code: (java) [Select]double dist = Math.sqrt((xPos - b.xPos)*(xPos - b.xPos) + (yPos - b.yPos)*(yPos - b.yPos)); if(dist < 0){ dist = .001; }
double dist = Math.sqrt((xPos - b.xPos)*(xPos - b.xPos) + (yPos - b.yPos)*(yPos - b.yPos)); if(dist < 0){ dist = .001; }
Quote from: Scipi on January 20, 2014, 06:19:26 pmCode: (java) [Select]double dist = Math.sqrt((xPos - b.xPos)*(xPos - b.xPos) + (yPos - b.yPos)*(yPos - b.yPos)); if(dist < 0){ dist = .001; }Erm, distance from a sqrt shouldn't give you a negative... I don't believe that if conditional will ever be fulfilled.
You can use the dot product of the viewing direction vector and the vector between the two bots. The direction vector should be <cos(viewangle), sin(viewangle)> and the vector between the bots should be <b.xPos - xPos, b.yPos - yPos>. The dot product of these two vectors, (b.xPos - xPos)*cos(viewangle) + (b.yPos - yPos)*sin(viewangle), is equal to the magnitude of the two vectors times the cosine of the angle between them. The magnitude of the first is 1, and the magnitude of the second is the distance between the bots. So you can divide the aforementioned dot product by the distance between the bots and get the cosine of the angle between the view direction and the other bot. Then, you can just compare it to the cosine of half the field of view. Hopefully this all makes sense.
private boolean inFOV(Bot b, double uAngle, double lAngle){ double x1 = Math.cos(Math.toRadians(uAngle)); double y1 = Math.sin(Math.toRadians(uAngle)); double x2 = Math.cos(Math.toRadians(lAngle)); double y2 = Math.sin(Math.toRadians(lAngle)); double uDist = y1 * b.xPos + -x1 * b.yPos; double lDist = -y2 * b.xPos + x2 * b.yPos; return (uDist > b.radius && lDist < b.radius); }
private boolean inFOV3(Bot b, double angle, double FOV){ double x1 = Math.cos(Math.toRadians(angle)); double y1 = Math.sin(Math.toRadians(angle)); double dist = Math.sqrt((xPos - b.xPos)*(xPos - b.xPos) + (yPos - b.yPos)*(yPos - b.yPos)); double dx = b.xPos - xPos; double dy = b.yPos - yPos; if(dist == 0){ dist = .001; } //(b.xPos - xPos)*cos(viewangle) + (b.yPos - yPos)*sin(viewangle) double dotP = (b.xPos - xPos)*x1 + (b.yPos - yPos)*y1; double dAngle = dotP / dist; double halfField = Math.cos(FOV); return (dAngle >= halfField); }
Quote from: Scipi on January 20, 2014, 06:19:26 pmCode: (java) [Select]double buffer = Math.toDegrees(Math.asin(b.radius/dist));If I'm correct, the dist you calculated is the distance from the centers... radius/distance is, by your picture, opposite/adjacent, not opposite/hypotenuse, so I think you should be using atan there. That might be your problem. This results in a smaller buffer, which would explain why some objects inside don't work, but not why some objects outside work. Have you tried doing it without accounting for a buffer?Edit: Ninja'd, nvm.
Code: (java) [Select]double buffer = Math.toDegrees(Math.asin(b.radius/dist));
double buffer = Math.toDegrees(Math.asin(b.radius/dist));