Transform world rotation to local

SORRY: I mean world location not rotation :slight_smile:

Hi!

Is it possible to transform world location to local location? I found Convert Transform to Relative node… if it is it, but i’m looking for code solution. Thanks.

If I understand you correctly, you would like to find the local space from a specfiic Actor…

So you can do the following…

FVector Location = Actor->GetActorLocation();
FRotator Rotation = Actor->GetActorRotation();
FVector VectorInActorLocalSpace = Rotation.UnrotateVector(WorldSpaceVector - Location);

Actor(your actor you want to be referenced to) and WorldSpaceVector(you vector to convert) are your inputs.

Hope that helps!

To transform from world space to local space, simply store your object’s starting location in the world and add it as an offset to any local translations you would like to do. For example, if your object starts off in the world at (100, 0, 0) and you would like it to move 20 more units along the X, you would add (100, 0, 0) to (20, 0, 0) to get an ending location of (120, 0, 0) in the world.

In my particular case, I have my own component class derived from the mesh component. If I wanted to translate a component that is attached to something else, I could simply translate it in local space as expected. But if that component happens to be the root component of an actor, it’s local space is relative to the world origin, which really means that it’s moving in world space. I solved my interpolation translations thusly:

void UInteractiveStaticMeshComponent::BeginPlay()
{
	// This object doesn't know its world location when added to the parent, 
	// but it does by the time BeginPlay is called
	if (_isRootComponent)
	{
		_rootStartingLocation = GetRelativeTransform().GetTranslation();
	}
	Super::BeginPlay();
}

And then in my interpolation function, I had:

void UInteractiveStaticMeshComponent::PlayInterpolationAnimation(FInterpolationAnimation destination)
{
	// Set the destinations for where this object is translating/rotating to
	if (_isRootComponent)
	{
		// The root component moves in world space, so move relative to the world starting location
		_targetTranslation = _rootStartingLocation + destination._location;
	}
	else
	{
		_targetTranslation = destination._location;		
	}

From here, I could animate all my components in local space when setting their relative location, regardless of whether they were actually using local space or world space.

currentTranslation = (UKismetMathLibrary::VInterpTo(currentTranslation, _targetTranslation, deltaTime, _animationTranslationSpeed));
		SetRelativeLocation(currentTranslation);