RelativeLocation VS Component Location VS ForwardVector

void UTankAimingComponent::BarrelMoveTowards(FVector AimDirection)
{
	//Work-Out difference between current barrel rotation, and AimDirection
	auto BarrelRotator = Barrel->GetForwardVector().Rotation();
	UE_LOG(LogTemp, Warning, TEXT("BarrelRotator: %s"), *BarrelRotator.ToString());
	auto    AimRotator = AimDirection.Rotation();
	UE_LOG(LogTemp, Warning, TEXT("AimRotator: %s"), *AimRotator.ToString());
	auto  DeltaRotator =  AimRotator - BarrelRotator;
	UE_LOG(LogTemp, Warning, TEXT("DeltaRotator: %s"), *DeltaRotator.ToString());
	UE_LOG(LogTemp, Warning, TEXT("DeltaRotator.Pitch: %f"), DeltaRotator.Pitch);
	Barrel->Elevate(DeltaRotator.Pitch);
}
	auto MC_RelativeSpeed = FMath::Clamp<float>(RelativeSpeed, -1.f, 1.f);
	UE_LOG(LogTemp, Warning, TEXT("MC_RelativeSpeed: %f: "), MC_RelativeSpeed);

	auto ElevateionChange = MC_RelativeSpeed * MaxDegreesPerSecond * GetWorld()->DeltaTimeSeconds;
	UE_LOG(LogTemp, Warning, TEXT("ElevationChange: %f"), ElevateionChange);

	UE_LOG(LogTemp, Warning, TEXT("GetRelativeRotation().Pitch: %f"), GetRelativeRotation().Pitch);
	auto RawNewElevation  = GetRelativeRotation().Pitch + ElevateionChange;
	UE_LOG(LogTemp, Warning, TEXT("RawNewElevation: %f"), RawNewElevation);

	auto MC_Elevation = FMath::Clamp(RawNewElevation, MinElevationDegrees, MaxElevationDegrees);
	UE_LOG(LogTemp, Warning, TEXT("AFTER CLAMP RawNewElevation: %f"), MC_Elevation);
	SetRelativeRotation(FRotator(MC_Elevation, 0.f, 0.f));
UE_LOG(LogTemp, Warning, TEXT("GetRelativeRotation().Pitch: %f"), GetRelativeRotation().Pitch);

When I change GetRelativeRotation to GetComponenRotation (inside TankBarrel)I got a weird result.


First, when I Log the values, it shows me the first Log BarrelRotator
Barrel->GetForwardVector()/GetComponentLocation.Rotation();(inside TankAimingComponent)
it is different from GetRelativeRotation().Pitch (inside TankBarrel).

BUT, when I changed GetRelativeLocation to GetComponentLocation(inside TankBarrel)
that was the result :thinking:

LogTemp: Warning: AFTER CLAMP RawNewElevation: 0.000000

And notice BarrelRotator.Pitch( inside TankAimingCompoent ) it’s closer to GetCompoentLocation.Pitch ( inside TankBarrel )

  1. Can you explain How that happened?
  2. Can you tell me what is deff between RelativeLocation and ComponentLocation?
  3. GetForwardLocation returns just X-axis, doesn’t it?
    if that is true why Y-axis and Z-axis are not 0?

please if you have answers, number them.

Thanks in advance

  1. I’m a little confused as to what you’re trying to showcase as you aren’t saying what you expected.
  2. RelativeLocation is for the location relative to the parent (in this class it would be relative to the turret). ComponentLocation would be the location of the component in world space
  3. Yes but it gets it in world space.


I expected the first value (BarrelRotator) is equal to the second value (GetRelativeRotation.Pitch ).

UE_LOG(LogTemp, Warning, TEXT(GetRelativeRotation().Pitch: %f), GetRelativeRotation().Pitch);

As you can see above I made this code to make sure the rotation of Barrel in the TankBarrel is the same value as the rotation of Barrel in TankAimingComponent (BarrelRotator), but it shows me they are different from each other. I expected it to be the same value.


I expected AFTER CLAMP RawNewElevation = RawNewElevation cuz they didn’t reach MaxElevationDegreesvalue

When I just switch between GetRelativeRotation to GetComponenRotation, the unexpected result happened.

Also, I need to know when should I use GetRelativeRotation/Location and GetComponentRotation/Location? and how do they give us different results?

The BarrelRotator is equal to the ComponentRotation not the RelativeRotation.

image

Did you mean to type something else for the right hand side?

Because they aren’t the same thing. RelativeRotation is relative to the parent component. ComponentRotation is the rotation in world space.

Lets use location as that’s easier to demonstrate

TankBody is at [3,5]. It is the root component, component location is the same as relative location as there’s nothing to be relative of.

TankTurret is at [4,7], it is a child of TankBody.

TankTurret->GetComponentLocation() would return [4,7]
TankTurret->GetRelativeLocation() would return [1,2] as that is the location relative to the parent component (TankBody).

Now lets say the tank has moved 2 to the right so TankBody is [5,5]

TankTurret->GetComponentLocation() would return [6, 7]
TankTurret->GetRelativeLocation() would still return [1, 2] as its relative location hasn’t changed at all.

thanks a lot that was helpful!

Did you mean to type something else for the right hand side?

No, I mean it should be 0, but when I look at it again recently I realize RawNewElevation is less than 0,
silly me :sweat_smile:. but I still have a Q, why RawNewElevation is less than 0? I expected to be greater than 0. this happens when I switch from GetRelativeRotation to GetComponentLocation() (inside TankBarrel) it gives me a value less than zero.
with GetRelativeRotation() (inside TankBarrel)



as you can see above, I expected this result.

But, with GetComponentRotation() (inside TankBarrel)


the barrel of tank is acting very strange.

To be able to pitch downwards.

That’s not strange because you’re using the absolute rotation of the barrel. Could you explain why you are expecting it to be the same?

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms