Hi All,
I’m trying to port the following UTWeapon code to UE4, but I’m a bit stuck on two areas, and was wondering if anyone has some insights on how to solve this:
The two areas I’m not sure of are as follows:
- Equivalent of Bitwise operations on Rotators in Unrealscript vs. UE4?
- Equivalent of the Rotator operator “ClockWiseFrom” in UE4?
Some help would be greatly appreciated.
Thanks!
Some more details below:
Unrealscript:
This part is called from Weapon.SetPosition()
SpecRotation.Pitch = LagRot(SpecRotation.Pitch & 65535, LastRotation.Pitch & 65535, MaxPitchLag, 1);
simulated function int LagRot(int NewValue, int LastValue, float MaxDiff, int Index)
{
local int RotDiff;
local float LeadMag, DeltaTime;
if ( NewValue ClockWiseFrom LastValue )
{
if ( LastValue > NewValue )
{
LastValue -= 65536;
}
}
else
{
if ( NewValue > LastValue )
{
NewValue -= 65536;
}
}
DeltaTime = WorldInfo.TimeSeconds - LastRotUpdate;
RotDiff = NewValue - LastValue;
if ( (RotDiff == 0) || (OldRotDiff[Index] == 0) )
{
LeadMag = ShouldLagRot() ? OldLeadMag[Index] : 0.0;
if ( (RotDiff == 0) && (OldRotDiff[Index] == 0) )
{
OldMaxDiff[Index] = 0;
}
}
else if ( (RotDiff > 0) == (OldRotDiff[Index] > 0) )
{
if (ShouldLagRot())
{
MaxDiff = FMin(1, Abs(RotDiff)/(12000*DeltaTime)) * MaxDiff;
if ( OldMaxDiff[Index] != 0 )
MaxDiff = FMax(OldMaxDiff[Index], MaxDiff);
OldMaxDiff[Index] = MaxDiff;
LeadMag = (NewValue > LastValue) ? -1* MaxDiff : MaxDiff;
}
else
{
LeadMag = 0;
}
if ( DeltaTime < 1/RotChgSpeed )
{
LeadMag = (1.0 - RotChgSpeed*DeltaTime)*OldLeadMag[Index] + RotChgSpeed*DeltaTime*LeadMag;
}
else
{
LeadMag = 0;
}
}
else
{
LeadMag = 0;
OldMaxDiff[Index] = 0;
if ( DeltaTime < 1/ReturnChgSpeed )
{
LeadMag = (1 - ReturnChgSpeed*DeltaTime)*OldLeadMag[Index] + ReturnChgSpeed*DeltaTime*LeadMag;
}
}
OldLeadMag[Index] = LeadMag;
OldRotDiff[Index] = RotDiff;
return NewValue + LeadMag;
}
C++ I have so far:
In my weapon’s SetPosition equivalent:
void APWNWeapon::UpdateDisplay(float deltaTime, FRotator newRotation, FVector newLocation)
{
...
// Convert floats to ints and do bitwise operation.
FinalRotation.Yaw = LagRot((float)((int32)newRotation.Yaw & (int32)360), (float)((int32)newRotation.Yaw & (int32)360), MaxYawLag, 0);
...
}
float APWNWeapon::LagRot(float NewValue, float LastValue, float MaxDiff, int32 Index)
{
float RotDiff;
float LeadMag, DeltaTime;
// Remove "Rotator" values
NewValue /= 65535.0f;
LastValue /= 65535.0f;
// UE4 equivalent to "ClockWiseFrom"?
**if (NewValue ClockWiseFrom LastValue)**
{
if (LastValue > NewValue)
{
LastValue -= 360.0f;
}
else if (NewValue > LastValue)
{
NewValue -= 360.0f;
}
//}
DeltaTime = GetWorld()->TimeSeconds - LastRotUpdate;
RotDiff = NewValue - LastValue;
if ((RotDiff == 0) || (OldRotDiff[Index] == 0))
{
LeadMag = ShouldLagRot ? OldLeadMag[Index] : 0.0;
if ((RotDiff == 0) && (OldRotDiff[Index] == 0))
{
OldMaxDiff[Index] = 0;
}
}
else if ((RotDiff > 0.0f) == (OldRotDiff[Index] > 0))
{
if (ShouldLagRot)
{
// TODO:
MaxDiff = fminf(1, abs(RotDiff) / ((12000.0f / 65535.0f) * DeltaTime)) * MaxDiff;
if (OldMaxDiff[Index] != 0.0f)
MaxDiff = fmaxf(OldMaxDiff[Index], MaxDiff);
OldMaxDiff[Index] = MaxDiff;
LeadMag = (NewValue > LastValue) ? -1.0f * MaxDiff : MaxDiff;
}
else
{
LeadMag = 0.0f;
}
if (DeltaTime < 1.0f / RotChgSpeed)
{
LeadMag = (1.0f - RotChgSpeed * DeltaTime) * OldLeadMag[Index] + RotChgSpeed * DeltaTime * LeadMag;
}
else
{
LeadMag = 0;
}
}
else
{
LeadMag = 0;
OldMaxDiff[Index] = 0;
if (DeltaTime < 1 / ReturnChgSpeed)
{
LeadMag = (1 - ReturnChgSpeed * DeltaTime) * OldLeadMag[Index] + ReturnChgSpeed * DeltaTime * LeadMag;
}
}
OldLeadMag[Index] = LeadMag;
OldRotDiff[Index] = RotDiff;
return NewValue + LeadMag;
}