/// <summary> /// Checks to see if the target is within the given angle /// and within the given distance. Returns false if no /// target has been set. Both the angle and the distance are inclusive. /// </summary> /// <param name="maxAngle">The maximum angle (in radians) /// that the target can be detected.</param> /// <param name="maxDistance">The maximum distance that the target can be detected.</param> /// <returns></returns> public bool CheckTargetInSight(float maxAngle, float maxDistance) { //Get the point maxAngle distance along a circle where radius = maxDistance Vector2 topPosition = new Vector2( (float)(_position.X + maxDistance * Math.Cos(-maxAngle)), (float)(_position.Y + maxDistance * Math.Sin(-maxAngle))); //Get the point -maxAngle distance along a circle where radius = maxDistance Vector2 bottomPosition = new Vector2( (float)(_position.X + maxDistance * Math.Cos(maxAngle)), (float)(_position.Y + maxDistance * Math.Sin(maxAngle))); // Draw partial circle Raylib.DrawCircleSector( new System.Numerics.Vector2(_position.X * 32, _position.Y * 32), maxDistance * 32, (int)((180 / Math.PI) * -maxAngle) + 90, (int)((180 / Math.PI) * maxAngle) + 90, 10, Color.GREEN); //Checks if the target has a value before continuing if (Target == null) { return(true); } //Find the vector representing the distance between the actor and its target Vector2 direction = Vector2.Normalize(Target.Position - Position); //Get the magnitude of the distance vector float distance = direction.Magnitude; //Use the inverse cosine to find the angle of the dot product in radians float angle = (float)Math.Acos(Vector2.DotProduct(Forward, direction.Normalized)); //Return true if the angle and distance are in range if (angle <= maxAngle && distance <= maxDistance) { return(true); } return(false); }