Scene Actor's bounding square (screen space)?

Thank you very much for sharing amazing work. I used the last component of your blueprint, everything works well but when I have multiple objects, there is only one bounding box ? how can I have multiple bounding boxes ? Thank you.

Hi!
If you have pre-set up objects - each of them needs to create his own Widget for himself in his very own blueprint and naturally add it to the Viewport.

I have not tried to get bounding boxes of raw ‘spawn’ function using a base class so I cannot help you in that matter, but It should also work.

1 Like

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.

The local/absolute size conversion to be integrated in the calculation of the positions is missing.
I don’t find the way to calculate this factor in cpp.

Ok, I basicaly answered myself:

// Get the player screen widget geometry
FGeometry geometry = UWidgetLayoutLibrary::GetPlayerScreenWidgetGeometry(pController);

// Calculate the local to absolute scale
FVector2D localToAbsoluteScale = geometry.GetLocalSize() / geometry.GetAbsoluteSize();

It’s just as in blueprint.

Would it be possible for you to make a video of you doing the process? Im completely lost on what to do.