Get Random Point In Bounding Box Problem If Rotated

Try this


FVector ARotBox::GetRandomInRotated() 
{	
	FVector rotatedVec = FVector::Zero();	
	if (UnrotatedBox != nullptr) {

		UnrotatedBox->SetWorldRotation(FRotator::ZeroRotator);		
		FVector RandomPoint = FMath::RandPointInBox(UnrotatedBox->Bounds.GetBox());				
		FMatrix LocalToWorldInverse = GetActorTransform().ToMatrixWithScale();		

		rotatedVec = UKismetMathLibrary::GreaterGreater_VectorRotator(LocalToWorldInverse.GetOrigin() - RandomPoint, LocalToWorldInverse.Rotator()) + GetActorLocation();
		DrawDebugBox(GetWorld(), GetActorLocation(), UnrotatedBox->Bounds.BoxExtent, FColor::Blue, false, 20000.f);		
		DrawDebugBox(GetWorld(), GetActorLocation(), UnrotatedBox->Bounds.BoxExtent, GetActorRotation().Quaternion(), FColor::Red, false, 20000.f);
	}
	return rotatedVec;
}

3 Likes