keywords: [UE4]用DrawDebugLine绘制矩形

slogan_image

自己实现的用DrawDebugLine绘制矩形的函数:

void AMyDebugActor::DrawDebugRectangle(const FVector& Position, const FRotator& Rot, const FVector2D& Size)
{
    FVector Center = GetActorLocation() + Position;

    FVector OrientFwd = (FRotationMatrix(FRotator(0.f, 90.f, 0.f)) * FRotationMatrix(Rot)).Rotator().Vector();
    FVector OrientBack = (FRotationMatrix(FRotator(0.f, -90.f, 0.f)) * FRotationMatrix(Rot)).Rotator().Vector();
    FVector PointR = (Center + OrientBack * Size.Y / 2) + Rot.Vector() * (Size.X / 2);
    FVector PointG = (Center + OrientBack * Size.Y / 2) + -Rot.Vector() * (Size.X / 2);
    FVector PointB = (Center + OrientFwd * Size.Y / 2) + Rot.Vector() * (Size.X / 2);
    FVector PointYellow = (Center + OrientFwd * Size.Y / 2) + -Rot.Vector() * (Size.X / 2);

    DrawDebugSphere(GetWorld(), PointR, 14.0f, 12, FColor(255, 0, 0));      //R
    DrawDebugSphere(GetWorld(), PointG, 12.0f, 12, FColor(0, 255, 0));      //G
    DrawDebugSphere(GetWorld(), PointB, 10.0f, 12, FColor(0, 0, 255));      //B
    DrawDebugSphere(GetWorld(), PointYellow, 8.0f, 12, FColor(255, 255, 0));    //Yellow

    DrawDebugLine(GetWorld(), PointR, PointG, FColor::Green);
    DrawDebugLine(GetWorld(), PointR, PointB, FColor::Green);
    DrawDebugLine(GetWorld(), PointYellow, PointB, FColor::Green);
    DrawDebugLine(GetWorld(), PointYellow, PointG, FColor::Green);
}