# Random Point on BoundingBox Perimeter

**URL:** <https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416>\
**Category:** C++\
**Tags:** unreal-engine, UE5-0, question, CPP, vector-math\
**Created:** [November 3, 2022, 4:34pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416 "2022-11-03T16:34:48Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcham](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@markcham](https://forums.unrealengine.com/u/markcham)\
**Post date:** [November 3, 2022, 4:34pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/1 "2022-11-03T16:34:48Z")

</div>

Kismet has a function for random point in bounding box, but I’m looking for a random point along a bounding box perimeter.

I have a fixed location for the bounding box origin and have access to the box extent.

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 3, 2022, 5:04pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/2 "2022-11-03T17:04:08Z")

</div>

Why not just pick a random pair of coordiates from the box extent (in world space) and get a random distance between them (clamped with thir own dustance) as the offset?

Oh and uou need a function to check that the verts are on the same plane (not diagonally across from each other)

---

<div class="post-metadata">

**Author:** ![markcham](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@markcham](https://forums.unrealengine.com/u/markcham)\
**Post date:** [November 3, 2022, 5:16pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/3 "2022-11-03T17:16:17Z")

</div>

Hi thanks for the reply. I’m not sure I understand your suggestion. I might not have explained what I’m looking for well enough.

The final result that I’m looking for is the random point on the bounding box perimeter. If i picked two coordinates in the box extent and got their distance, what exactly would that be accomplishing?

---

<div class="post-metadata">

**Author:** ![eldany.uy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/eldany.uy/32/821920_2.png) [@eldany.uy](https://forums.unrealengine.com/u/eldany.uy)\
**Post date:** [November 3, 2022, 5:37pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/4 "2022-11-03T17:37:19Z")

</div>

I don’t know if this help you and if I understood what you need.  
You want a point from the SURFACE of the bounding box instead  
a random point of the VOLUME?

What about this:

put a cube in the location of the bounding box and same dimmensions  
The cube is just to generate a trace hit.

Then you make a trace by channel from a random place of the outside to the center of the cube…something similar to this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/2/1/b21e43ef456181736315e3a21cd7c0e3e5249689.png)

---

<div class="post-metadata">

**Author:** ![markcham](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@markcham](https://forums.unrealengine.com/u/markcham)\
**Post date:** [November 3, 2022, 5:41pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/5 "2022-11-03T17:41:42Z")

</div>

> [@eldany.uy](#):
>
> outside to the c

Oh I think a line trace could work. I should’ve mentioned that I’m using the bounding box as a 2-dimensional square, with the X value of the box extent being zero.

---

<div class="post-metadata">

**Author:** ![eldany.uy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/eldany.uy/32/821920_2.png) [@eldany.uy](https://forums.unrealengine.com/u/eldany.uy)\
**Post date:** [November 3, 2022, 5:48pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/6 "2022-11-03T17:48:40Z")

</div>

wait…you want a point in the perimeter of a 2D box??

how about this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/c/4/7c497414c1a3e2277349305ee689ccdcfc854eee.png)

basically it takes a random value for X and Y (like in the surface) and then it  
‘clamps’ the value to one of the 4 sides

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [November 3, 2022, 5:51pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/7 "2022-11-03T17:51:48Z")

</div>

> [@markcham](#):
>
> I’m using the bounding box as a 2-dimensional square, with the X value of the box extent being zero.

😑 So not something like this at all?

---

<div class="post-metadata">

**Author:** ![markcham](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@markcham](https://forums.unrealengine.com/u/markcham)\
**Post date:** [November 3, 2022, 5:54pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/9 "2022-11-03T17:54:11Z")

</div>

Sorry! I’m still interested in how you accomplished this if you’re willing to share. Eventually I’ll have to implement in 3D, but for now I’m doing 2D.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [November 3, 2022, 5:56pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/10 "2022-11-03T17:56:30Z")

</div>

It’s this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/8/4/884721c0391777a232569750922fc033295b6602.jpeg)

But something is not _entirely_ correct here, it seems to overfocus on the edges too much. Would like to fix it up but need to fix dinner first ;p

Do experiment with the `Vector Bounded to Box` node - perhaps it will get you there.

---

<div class="post-metadata">

**Author:** ![eldany.uy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/eldany.uy/32/821920_2.png) [@eldany.uy](https://forums.unrealengine.com/u/eldany.uy)\
**Post date:** [November 3, 2022, 6:04pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/11 "2022-11-03T18:04:43Z")

</div>

I like the approach. Just wondering why it seems hit points tend so much to the edges of the box

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [November 3, 2022, 6:09pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/12 "2022-11-03T18:09:51Z")

</div>

I had had to do something similar in the past but couldn’t get the distribution right. What I ended up doing was tracing from a random point on the sphere surface towards a random point in the volume.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/5/9/1592aedb173ebf03c027153f4e7ec51241e1c68d.jpeg)

And then finding Closest Point on Line 🤷 It was too long ago. This created no hot-spots afaik.

There might be a better way.

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [November 3, 2022, 9:49pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/13 "2022-11-03T21:49:08Z")

</div>

> [@eldany.uy](#):
>
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/c/4/7c497414c1a3e2277349305ee689ccdcfc854eee.png)
> 
> image1444×521 157 KB

To elaborate on this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/9/7/b97e3ddbd0c20d6531f2918c5195ba1b8b30cdde.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/4/2/d42a6161f3a1ca2f42f63e7f5d887746745ed5a4.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/d/f/1dfb500c37bb1a75cd8e84fc45073d779aaf665c.png)  
 ![PointsInBounds](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/3/e/c3edf87cb4905415bca121549602f316a4b88c0e.gif)

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [November 4, 2022, 12:04am UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/14 "2022-11-04T00:04:49Z")

</div>

If you want the points to be evenly distributed across the surface, you need to do this in two steps:

1. Determine which face to put the point on. Each face needs to be picked with a relative probability of the area of that face.
2. Once the face is determined, pick a random point on the face. This can easily be done by scaling barycentric 0…1 coordinates for “u” and “v” along the two face basis (edge) vectors.

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [November 4, 2022, 4:22pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/15 "2022-11-04T16:22:14Z")

</div>

BoundedToBox sticks to the egdes because it’s a simple clamp, no projection.  
The sphere has to be larger than the box, so it has high chance of generating points where two axis (say X and Y) are both larger than box extent. BoundedToBox will simply clamp that to BoxExtent.X and BoxExtent.Y so the points end up on egde.

Here’s my attempt 😛

```cpp
FVector RandomPointOnBoundingBoxPerimeter(FVector Origin, FVector BoxExtent)
{
	const FVector Dir = FMath::VRand();
	const FVector Dist = BoxExtent / Dir;
	return Origin + Dir * Dist.GetAbsMin();
}

```

---

<div class="post-metadata">

**Author:** ![markcham](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@markcham](https://forums.unrealengine.com/u/markcham)\
**Post date:** [November 4, 2022, 6:52pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/16 "2022-11-04T18:52:05Z")

</div>

Since my question (although vague) was how to choose a random point on the perimeter of a 2D box, @eldany.uy was the first to point me in the right direction. This is how I ended up implemented in C++. I’m kind of embarrassed I didn’t think of this before… but I’m glad there was also a discussion about how to do it in 3D. Thanks everyone!

```nohighlight
SpawnLocation = BoxBounds.Origin;
// Y is left-right Z is up-down
const float OffsetX = 0.f;
float OffsetY;
float OffsetZ;
if (const int32 RandomDirection = UKismetMathLibrary::RandomIntegerInRange(0, 3); 
	RandomDirection == 0)
{
	// top
	OffsetY = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Y, ScaledBoxBounds.Y);
	OffsetZ = ScaledBoxBounds.Z;
}
else if (RandomDirection == 1)
{
	// right
	OffsetY = ScaledBoxBounds.Y;
	OffsetZ = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Z, ScaledBoxBounds.Z);
}
else if (RandomDirection == 2)
{
	// left
	OffsetY = -ScaledBoxBounds.Y;
	OffsetZ = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Z, ScaledBoxBounds.Z);
}
else
{
	// bottom
	OffsetY = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Y, ScaledBoxBounds.Y);
	OffsetZ = -ScaledBoxBounds.Z;
}
FVector Offset = { OffsetX, OffsetY, OffsetZ };
SpawnLocation = FirstSpawnLocation + Offset;

```

---

<div class="post-metadata">

**Author:** ![eldany.uy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/eldany.uy/32/821920_2.png) [@eldany.uy](https://forums.unrealengine.com/u/eldany.uy)\
**Post date:** [November 4, 2022, 7:06pm UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/17 "2022-11-04T19:06:43Z")

</div>

Glad to help in the brainstorming process!  
There are always a lot of ways of achieve a goal and  
I am sure there are many better than this one but hey! it works 🙂

instead of nested If conditions you can use switch like this:

```auto
switch (RandomDirection)
{
case 0:

	// top
	OffsetY = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Y, ScaledBoxBounds.Y);
	OffsetZ = ScaledBoxBounds.Z;

case 1:

	// right
	OffsetY = ScaledBoxBounds.Y;
	OffsetZ = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Z, ScaledBoxBounds.Z);

case 2:

	// left
	OffsetY = -ScaledBoxBounds.Y;
	OffsetZ = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Z, ScaledBoxBounds.Z);

default:

// bottom
	OffsetY = UKismetMathLibrary::RandomFloatInRange(-ScaledBoxBounds.Y, ScaledBoxBounds.Y);
	OffsetZ = -ScaledBoxBounds.Z;

}

```

Greetz!  
Dany

---

<div class="post-metadata">

**Author:** ![Tizeng](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tizeng](https://forums.unrealengine.com/u/Tizeng)\
**Post date:** [April 19, 2023, 6:27am UTC](https://forums.unrealengine.com/t/random-point-on-boundingbox-perimeter/683416/18 "2023-04-19T06:27:15Z")

</div>

Thank you so much for providing this approach!!
