Call Cone Check from C++

Is it possible to call the cone check decorator node from in code somehow? Or will I need to just take the logic from within the node and create my own functions.

Thanks.

-Jasper

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

Thanks for the answer, it did end up working. Here is my implementation.

bool UTASK_Idle::IsInsideCone(FVector _observerLocation, FVector _observerLookDirectionNorm, float _coneHalfAngle, float _coneLength, FVector _targetLocation) {

	FVector ObserverLookDirection = _observerLookDirectionNorm * _coneLength; // Direction of cone, including length
	float fConeHalfAngle = FMath::Cos(FMath::DegreesToRadians(_coneHalfAngle));
	FVector DirectionToTarget = (_targetLocation - _observerLocation).GetSafeNormal();

	// Check angle between direction to target and cone angle
	bool InInfiniteCone = ObserverLookDirection.CosineAngle2D(DirectionToTarget) > fConeHalfAngle;

	//DrawDebugCone(
	//	GetWorld(),
	//	_observerLocation,
	//	_observerLookDirectionNorm,
	//	_coneLength,
	//	FMath::DegreesToRadians(_coneHalfAngle),
	//	0.0f,
	//	100.0f,
	//	FColor::Red,
	//	true,
	//	5.0f
	//);			

	// If inside of the infinitely long cone
	if (InInfiniteCone)
	{
		float fDistance = FVector::Dist(_observerLocation, _targetLocation);
		if (fDistance > _coneLength)
		{
			if (GEngine) GEngine->AddOnScreenDebugMessage(0, 0.0f, FColor::Cyan, "Deer can't see the player.");
		}
		else
		{
			if (GEngine) GEngine->AddOnScreenDebugMessage(0, 0.0f, FColor::Cyan, "Deer can see the player!");
		}

		// Check distance
		return fDistance <= _coneLength;
	}
	else
	{
		return false;
	}
}