I only have the object's position, the player position, and the vector representing the player's view, and I have the FoV (120 degrees horizontal/60 on each side, and 60 degrees vertical/30 on each side).
As I'm dealing with data that has already been recorded, I cannot use any Unity built-in functions and have to calculate it manually. My thoughts were to convert the 3D coordinates into 2D vectors, so that I can calculate if an object is in the horizontal plane first (by taking the X and Z values) and then if it is, check if it is in the vertical plane (take the Y and Z coordinates).
After, I calculate the the angle between the object and the player view to see if the absolute value of that is within the field of view (for example, if the object is on the left side of the player view, and the horizontal FOV is 120 degrees, then if the absolute value of the angle is less than or equal to 60 degrees it is within view).
Is this on the right track? I've been getting strange results (like only one of the four objects I'm looking for shows as visible throughout a log of values even though they should all at some point be visible).
Here is some code to show what I'm doing:
public static Double angle(Vector2D obj, Vector2D bodyPos, Vector2D headDir) {
Double result = 0.0;
// Get the vector between the body position and the object
Vector2D temp = Vector2D.subtract(obj, bodyPos);
// First find dot product
Double dotProduct = Vector2D.dotProduct(headDir, temp);
// Now find the length of each vector and get the product of them
Double lengthProduct = Vector2D.vectorLength(headDir)*Vector2D.vectorLength(temp);
// Find the intermediary result
result = dotProduct / lengthProduct;
// Apply arc cos to find the angle between the vectors
result = Math.acos(result);
// Convert angle to degrees
result = (result * 180)/Math.PI;
System.out.println("The angle is: " + result);
return result;
}
And here is the code that determines if something is in view by using that angle function:
public static boolean isInView(bodyPosition objPos, bodyPosition bodyPos, headDirection headDir){
boolean inView = false;
Double h_angle = 0.0;
Double v_angle = 0.0;
// Create 2D vectors for both the horizontal and vertical FOV measurements
Vector2D h_objPos = new Vector2D(objPos.getDirectionX(), objPos.getDirectionZ());
Vector2D v_objPos = new Vector2D(objPos.getDirectionY(), objPos.getDirectionZ());
Vector2D h_bodyPos = new Vector2D(bodyPos.getDirectionX(), bodyPos.getDirectionZ());
Vector2D v_bodyPos = new Vector2D(bodyPos.getDirectionY(), bodyPos.getDirectionZ());
Vector2D h_headDir = new Vector2D(headDir.getDirectionX(), headDir.getDirectionZ());
Vector2D v_headDir = new Vector2D(headDir.getDirectionY(), headDir.getDirectionZ());
// determine angle of FOV+
Double iHFOV = FOV_H_MID * 0.5;
Double iVFOV = FOV_V_MID * 0.5;
// Check horizontal FOV first; if it is within that, then check the vertical
// If both pass, then it is in view
h_angle = Vector2D.angle(h_objPos, h_bodyPos, h_headDir);
v_angle = Vector2D.angle(v_objPos, v_bodyPos, v_headDir);
if ( Math.abs( h_angle ) <= iHFOV) {
if ( Math.abs( v_angle ) <= iVFOV) {
inView = true;
}
}
return inView;
}
I found some resources that I will be reviewing in the mean time, but any help is appreciated. I haven't done any linear algebra courses so I'm trying to wrap my brain around it.