Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public static class VisibilityHelper
- {
- private static float DistantRadiusToAngle(float distance, float radius)
- {
- return Mathf.Asin(radius / distance) * Mathf.Rad2Deg;
- }
- public static bool IsObjetVisible(Vector2 viewPosition, Vector2 viewDirection, float viewDistance, float viewAngle, Vector2 objectPosition, float objectRadius)
- {
- Vector2 delta = objectPosition - viewPosition;
- float distanceToObject = delta.magnitude;
- //If view circle and object circle do not intersect - then object is out of view range
- if (viewDistance + objectRadius < distanceToObject)
- return false;
- float angleToObject = Vector2.Angle(viewDirection, delta);
- //Since out calculations are using decline from view direction - we need only half of our view angle
- float halfViewAngle = viewAngle / 2f;
- //We are converting object radius to angle, using trigonomentry
- float objectRadiusAsAngle = DistantRadiusToAngle(distanceToObject, objectRadius);
- //If object is declined too far from our view direction - then it is outside of our viewport
- if ((halfViewAngle + objectRadiusAsAngle) < angleToObject)
- return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment