Call Cone Check from C++

There is no nice way to use cone check outside of the behavior tree, but the cone check is actually pretty easy. It’s basically those lines:

FORCEINLINE bool UBTDecorator_ConeCheck::CalcConditionImpl(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const
{
	const UBlackboardComponent* BBComponent = OwnerComp.GetBlackboardComponent();

	FVector ConeDir;
	FVector DirectionToObserve;

	return CalculateDirection(BBComponent, ConeOrigin, Observed, DirectionToObserve)
		&& CalculateDirection(BBComponent, ConeOrigin, ConeDirection, ConeDir)
		&& ConeDir.CosineAngle2D(DirectionToObserve) > ConeHalfAngleDot;
}

which boils down to

bool IsInsideOfCone(FVector ObserverLocation, FVector ObserverLookDirection, float ConeHalfAngle, FVector TargetLocation)
{
  float ConeHalfAngleDot = FMath::Cos(FMath::DegreesToRadians(ConeHalfAngle));
  FVector DirectionToTaget = (TargetLocation - ObserverLocation).GetSafeNormal();
  return ObserverLookDirection.CosineAngle2D(DirectionToTaget) > ConeHalfAngleDot;
}
1 Like