View difference between Paste ID: yeESVkhZ and
SHOW: | | - or go back to the newest paste.
1-
1+
/**
2
 * Returns whether a circle is within view of an object at the origin 
3
 * facing some direction.
4
 * 
5
 * @param origin
6
 * 	The origin of the view.
7
 * @param direction
8
 * 	The direction of the view, must be a normalized vector.
9
 * @param fov
10
 * 	The vector where x=cos(FOV/2) and y=sin(FOV/2), must be normalized by definition.
11
 * @param circle
12
 * 	The center of the circle.
13
 * @param radius
14
 * 	The radius of the circle.
15
 * @param entirely
16
 * 	True if this method should return whether the circle is completely 
17
 * 	within view, or false if this method should return whether the circle 
18
 * 	is partially within view.
19
 * @return
20
 * 	True if the target is in view, otherwise false.
21
 */
22
public static boolean isCircleInView( Vector origin, Vector direction, Vector fov, Vector circle, float radius, boolean entirely )
23
{
24
	// Calculate upper and lower vectors
25
	// Calculate forward vector between target and origin
26
	// Rotate forward by upper, resulting vector.y is the distance from upper to target
27
	// Rotate forward by lower, resulting vector.y is the distance from lower to target
28
	// If it doesn't matter if it's entirely in view, add radius*2 to the distances.
29
	
30
	float fovy = Math.abs( fov.y );
31
	float dxfx = direction.x * fov.x;
32
	float dxfy = direction.x * fovy;
33
	float dyfx = direction.y * fov.x;
34
	float dyfy = direction.y * fovy;
35
	float upperX = (dxfx - dyfy);
36
	float upperY = (dxfy + dyfx);
37
	float lowerX = (dxfx + dyfy);
38
	float lowerY = (dyfx - dxfy);
39
	float forwardX = circle.x - origin.x;
40
	float forwardY = circle.y - origin.y;
41
	float upperDist = (upperX * forwardY + upperY * forwardX);
42
	float lowerDist =-(lowerX * forwardY + lowerY * forwardX);
43
	
44
	if (!entirely)
45
	{
46
		upperDist += radius * 2;
47
		lowerDist += radius * 2;
48
	}
49
	
50
	return (lowerDist >= radius && upperDist >= radius) || // FOV <= 90
51
			 (fov.x < 0 && (upperDist >= radius || lowerDist >= radius)); // FOV >= 90
52
}