Hi,
First thing first, this is great to find a solution still working years after years! Many thanks @anonymous_user_1ed03e56 and @VtojeC_V for your greatly apriciated help!
I’m trying to achieve the same results but with cpp only (except for widget drawings on screen).
Do you know which functions and object to use to convert the 3D points into 2D points on the screen?
I currently have this code:
// Get the player as a VeinPlayerCharacter
TObjectPtr<AVeinPlayerCharacter> character = castChecked<AVeinPlayerCharacter>(m_pLastInRangeCharacter);
// Get the player's follow camera
TObjectPtr<UCameraComponent> pFollowCamera = character->GetFollowCamera();
// Get the player's controller
TObjectPtr<APlayerController> pController = CastChecked<APlayerController>(character->GetController());
FVector actorBoundsOrigin;
FVector actorBoundsExtent;
GetOwner()->GetActorBounds(true, actorBoundsOrigin, actorBoundsExtent);
DrawDebugBox(GetWorld(), actorBoundsOrigin, actorBoundsExtent, FColor::Yellow, false, -1.f, 0, 5.f);
// Make an array of the corners of the actor's bounding box
TArray<FVector> corners;
corners.Add(actorBoundsOrigin + FVector(actorBoundsExtent.X, actorBoundsExtent.Y, actorBoundsExtent.Z));
corners.Add(actorBoundsOrigin + FVector(actorBoundsExtent.X, actorBoundsExtent.Y, -actorBoundsExtent.Z));
corners.Add(actorBoundsOrigin + FVector(actorBoundsExtent.X, -actorBoundsExtent.Y, actorBoundsExtent.Z));
corners.Add(actorBoundsOrigin + FVector(actorBoundsExtent.X, -actorBoundsExtent.Y, -actorBoundsExtent.Z));
corners.Add(actorBoundsOrigin + FVector(-actorBoundsExtent.X, actorBoundsExtent.Y, actorBoundsExtent.Z));
corners.Add(actorBoundsOrigin + FVector(-actorBoundsExtent.X, actorBoundsExtent.Y, -actorBoundsExtent.Z));
corners.Add(actorBoundsOrigin + FVector(-actorBoundsExtent.X, -actorBoundsExtent.Y, actorBoundsExtent.Z));
corners.Add(actorBoundsOrigin + FVector(-actorBoundsExtent.X, -actorBoundsExtent.Y, -actorBoundsExtent.Z));
// Draw the corners of the actor's bounding box
for (FVector corner : corners)
{
DrawDebugPoint(GetWorld(), corner, 20.f, FColor::Red, false, -1.f, 0);
}
// Make an array of the corners of the actor's bounding box projected on the player's screen
TArray<FVector2D> projectedCorners;
for (FVector corner : corners)
{
pController->ProjectWorldLocationToScreen(corner, projectedCorners.AddDefaulted_GetRef());
}
// Get the minimum and maximum X and Y values of the projected corners
float minX = projectedCorners[0].X;
float maxX = projectedCorners[0].X;
float minY = projectedCorners[0].Y;
float maxY = projectedCorners[0].Y;
for (FVector2D projectedCorner : projectedCorners)
{
if (projectedCorner.X < minX)
{
minX = projectedCorner.X;
}
if (projectedCorner.X > maxX)
{
maxX = projectedCorner.X;
}
if (projectedCorner.Y < minY)
{
minY = projectedCorner.Y;
}
if (projectedCorner.Y > maxY)
{
maxY = projectedCorner.Y;
}
}
m_v2OwnerBoundingBoxStart = FVector2D(minX, minY);
m_v2OwnerBoundingBoxEnd = FVector2D(maxX, maxY)
It seems to work fine to this point but when I’m trying to pass this information to the widget to draw the bounding square on the screen it seems to be wrong.