Download

Getting wrong Rotation for Spawned Actors along the X+ Axis

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.

Have you tried to use UKismetMathLibrary:: DegAcos instead of FMath::Acos??

Tried it now, but still the same result. Thanks anyways. I didnt know about KismetMathLib I am very new to UE4

xLegA, I’m sorry that my answer was not so helpful.

I hope you find the solution! mostly made me confuse was degree between radian value. It seems to that some FMath uses radian value, but FRotator is using Degree value…
I wish I could be helpful more. Good luck!

I think it has something todo with the FMath::Sign() method. I tried UKismetMathLibrary::SignOfFloat() now but this method implements FMath::Sign().
I think the accuracy is too high for floating point values.

I am experiencing a similar issue. Did you ever find a fix?

sorry for the late answer. I have not been working on this project lately but yes. I gave my initial module a rotation different from 0.


AActor* startModule = GetWorld()->SpawnActor<AModule>(m_startModule, FVector(0.f, 0.f, 0.f), FRotator(0.f, 45.f, 0.f), spawnParams);

That fixed it for me. If anyone could find another solution any help is appreciated since I dont know which ramifications my solution brings in the future. thinking of collision and such.