HUD - Get Actors in Selection Rectangle - Any way to improve on it ?

I have got a dragging, RTS style selection box working using the “Get actors in Selection Rectangle” but the results are really sloppy.
I can be almost half a centimetre away from a mesh and it will still recognise and grab it. I understand this is because the node is reading the mesh bounds ?
Is there a different, more precise way to do this ? Or a way to improve the node’s results ?

I’m trying something different but I need some help.
I found this formula and I thought to implement it in blueprint but i’m failing. Here is the formula:

(From: algorithm - Finding whether a point lies inside a rectangle or not - Stack Overflow)

and here is my blueprint interpretation:

What am I doing wrong ?

I managed on my own. Leaving this here for the people of the future (WARNING: i’m not a programmer):



1 Like

This thread helped me a lot, thanks!

If anyone wants to do this in c++ (and a little more efficient, no offense!), here is what I did:

h

FVector2D MultiSelectionStart;
FVector2D MultiSelectionEnd;
TArray<AActor*> MultiSelectionActors;
APlayerController* PlayerController;

// edit vectors so that A is top left and B bottom right
static void AllignVector2D(FVector2D& A, FVector2D& B)
{
	FVector2D C, D;
	C.X = FMath::Min(A.X, B.X);
	C.Y = FMath::Min(A.Y, B.Y);
	D.X = FMath::Max(A.X, B.X);
	D.Y = FMath::Max(A.Y, B.Y);
	A = C;
	B = D;
};
// check if A is "inside" B and C
static bool IsInRect(FVector2D& A, FVector2D& B, FVector2D& C)
{
	AllignVector2D(B, C);
	return B.X <= A.X && A.X <= C.X && B.Y <= A.Y && A.Y <= C.Y;
};

cpp

GetActorsInSelectionRectangle(AActor::StaticClass(), MultiSelectionStart, MultiSelectionEnd, MultiSelectionActors);

// iterate back to forth, to prevent issues when removing entries
for (int32 i = MultiSelectionActors.Num() - 1; i >= 0; i--)
{
	AActor* actor = MultiSelectionActors[i];

	FVector2D screenPos;
	PlayerController->ProjectWorldLocationToScreen(actor->GetActorLocation(), screenPos);

    // remove actor from selection, if outside of selection rect
    if (!IsInRect(screenPos, MultiSelectionStart, MultiSelectionEnd))
        MultiSelectionActors.RemoveAt(i);
}