Hello. I am working on a modular dungeon generator for my game. This generator starts with a specified module and spawns possible modules on each exitpoint until it reaches a number of iterations. It is working just fine now except if the start module is not rotated
AActor* startModule = GetWorld()->SpawnActor<AModule>(m_startModule, FVector(0,0,0), FRotator(0,0,0), spawnParams);
Some modules spawned at exits directed towards the +X axis are turned 180 degrees wrong. However if I spawn the start module rotated 45 degrees about the upvector it is working correctly.
Here the code that rotates the newly spawned modules and translates them to the right position.
AActor* newModule = GetWorld()->SpawnActor<AModule>(m_corr1, dummyLocation, FRotator::ZeroRotator, spawnParams);
m_dungeonModules.Add(newModule);
//get new module exits
TArray<UChildActorComponent*> newModuleExits;
newModule->GetComponents(newModuleExits);
//choose random exit
AActor* exitToMatch = newModuleExits[FMath::RandRange(0, newModuleExits.Num()-1)]->GetChildActor();
//match exits
MatchExits(pendingExits*->GetChildActor(), exitToMatch);
void ADungeonController::MatchExits(AActor* oldE, AActor* newE)
{
UE_LOG(LogTemp, Warning, TEXT("in MatchExits"));
AActor* newModule = newE->GetParentActor();
FVector forwardVectorToMatch = -oldE->GetActorForwardVector();
float correctiveRotation = Azimuth(forwardVectorToMatch) - Azimuth(newE->GetActorForwardVector());
newModule->SetPivotOffset(oldE->GetActorLocation());
newModule->SetActorRotation(FRotator(0, correctiveRotation, 0), ETeleportType::TeleportPhysics);
newModule->SetPivotOffset(FVector::ZeroVector);
FVector acLoc = newModule->GetActorLocation();
FVector correctiveTranslation = oldE->GetActorLocation() - newE->GetActorLocation();
newModule->SetActorLocation(acLoc + correctiveTranslation);
}
float ADungeonController::Azimuth(FVector vec)
{
float angle = AngleBetween(FVector::ForwardVector, vec) * FMath::Sign(vec.Y);
return angle;
}
float ADungeonController::AngleBetween(FVector from, FVector to)
{
from.Normalize();
to.Normalize();
float angle = FMath::Acos(FVector::DotProduct(from, to)) * (180 / M_PI);
return angle;
}
Any help is appreciated.