有时候我们需要判断某个物体是否在另一个物体的扇形范围内,可以参考一下UE4源码中的代码。 源码在**“AIHelpers.h”** 命名空间是 FAISystem
bool CheckIsTargetInSightCone(const FVector& StartLocation, const FVector& ConeDirectionNormal, float PeripheralVisionAngleCos,
float ConeDirectionBackwardOffset, float NearClippingRadiusSq, float const FarClippingRadiusSq, const FVector& TargetLocation)
{
const FVector BaseLocation = FMath::IsNearlyZero(ConeDirectionBackwardOffset) ? StartLocation : StartLocation - ConeDirectionNormal * ConeDirectionBackwardOffset;
const FVector ActorToTarget = TargetLocation - BaseLocation;
const float DistToTargetSq = ActorToTarget.SizeSquared();
if (DistToTargetSq <= FarClippingRadiusSq && DistToTargetSq >= NearClippingRadiusSq)
{
const FVector DirectionToTarget = ActorToTarget.GetUnsafeNormal();
return FVector::DotProduct(DirectionToTarget, ConeDirectionNormal) > PeripheralVisionAngleCos;
}
return false;
}
|