# Get Random Point In Bounding Box Problem If Rotated

**URL:** <https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, CPP, question\
**Created:** [December 26, 2023, 9:33am UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921 "2023-12-26T09:33:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImAGoodSpoon1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/imagoodspoon1/32/757809_2.png) [@ImAGoodSpoon1](https://forums.unrealengine.com/u/ImAGoodSpoon1)\
**Post date:** [December 26, 2023, 9:33am UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/1 "2023-12-26T09:33:35Z")

</div>

Hey guys, I’m having a problem where I want to get a random point in a bounding box (from a box component). Everything goes right if the actor containing the box component not being rotated OR being rotated per 90 degrees (0, 90, 180, 270, etc). If its being rotated like 45 degrees, the bound won’t shape like the actual boxcomponent.

Here’s what I mean.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/0/6/606b63665effc53a7cf7c00dd6c2470371063be4.png)

If the box component is rotated like that, there are some bounds area that is not in the box overlap and that will cause the points outside the boxcomponent. Do you guys have any idea or solution regarding this problem? How can I make it only generate the point inside the BoxComponent even if its rotated like that.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [December 26, 2023, 10:45am UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/2 "2023-12-26T10:45:28Z")

</div>

It’s a pain in the bum, basically 🙂

The bounds calculation is correct, so we need to calculate via a box the doesn’t rotate with the actor, and then correct.

I have two components, one that always corrects to zero rotation when I move the actor

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/3/b/03b4a90c5a1a299427183f6d3558ec4c02c60fd6.png)

We need to find the points in the unrotated box, and do a couple of transforms to get the rotated box

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/a/4/0a44aa55263e7a25b50462faf26a7714974ff371.png)

It works.

The random point node gives you world coordinates, not much use here. So we have to make them local, rotate them by the amount the actor is rotated, and then make them world again 🙂

![rotatedbounds](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/d/f/cdf30b24b304b36e57f056a99382b8965e614198.gif)

---

<div class="post-metadata">

**Author:** ![ImAGoodSpoon1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/imagoodspoon1/32/757809_2.png) [@ImAGoodSpoon1](https://forums.unrealengine.com/u/ImAGoodSpoon1)\
**Post date:** [December 27, 2023, 1:39am UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/3 "2023-12-27T01:39:31Z")

</div>

Hello @ClockworkOcean ,

I think it’s different from my case, I also try your way but it still generated out of the box (Or I’m the one that implemented it wrong).

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/1/d/81d8bdf6c6565de8b0e6c875e612879a9357334f.jpeg)  
I want to generate some point inside the BoxComponent (which is the brown box), when I use GetRandomPointInBoundingBox, it’s generated also outside the box, when I try to check the Origin and Extent using DrawDebugBox, I can see the problem that the bounds itself (which is the blue box one) is not the same shape as the BrownBox. My code is simple:

```auto
const FVector& Origin = Area->Bounds.Origin;
const FVector& BoxExtent = Area->Bounds.BoxExtent;

// Get Random Point In Bounding Box
const FVector BoxMin = Origin - BoxExtent; 
const FVector BoxMax = Origin + BoxExtent;	
FVector RandomPoint = FMath::RandPointInBox(FBox(BoxMin, BoxMax));

DrawDebugBox(GetWorld(), Origin, BoxExtent, FColor::Blue, false, 200.f);

```

So I don’t know how to only generate the point inside the BrownBox (since using the Bounds don’t work because the bound shape isn’t the same). The bound shape gone wrong IF the actor is rotated like 50 degrees or some degrees that not 90,180,270,etc. If the actor is rotated 90 degrees and likely, the bounds is perfect.  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/2/9/529ed8cd924acc01a2697af465ffbf653a75227f.jpeg)

Since I need some to be rotated like 50 degrees to fit the map, I don’t know how.

I also try your code:

```auto
const FVector Inversed = GetActorTransform().InverseTransformPosition(RandomPoint);
const FVector Rotated = GetActorRotation().RotateVector(Inversed);
const FVector TransformLocation = GetActorTransform().TransformPosition(Rotated);

DrawDebugSphere(GetWorld(), TransformLocation, 20.f, 5, FColor::Green, false, 200.f);

```

And as you can see in both of the image, the green sphere will be correct if the bounds is correct. Do you have any idea about this?

Thanks, I appreciate your answer.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [December 27, 2023, 8:09am UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/4 "2023-12-27T08:09:19Z")

</div>

Maybe get it working in BP first, then at least we keep the variables pinned down a bit 🙂

What is critical with my approach, is I get the points in an _unrotated_ box, and then transform them.

I don’t see anything about an unrotated box here? ( could be missing it ). So we have the actor rotation, but we also have a box ( same size as the rotated box ) that never rotates with the actor, because the construction script always holds it to zero. That’s the box we find the random points in.

---

<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:** [December 27, 2023, 1:03pm UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/5 "2023-12-27T13:03:44Z")

</div>

Try this

```auto

FVector ARotBox::GetRandomInRotated() 
{	
	FVector rotatedVec = FVector::Zero();	
	if (UnrotatedBox != nullptr) {

		UnrotatedBox->SetWorldRotation(FRotator::ZeroRotator);		
		FVector RandomPoint = FMath::RandPointInBox(UnrotatedBox->Bounds.GetBox());				
		FMatrix LocalToWorldInverse = GetActorTransform().ToMatrixWithScale();		

		rotatedVec = UKismetMathLibrary::GreaterGreater_VectorRotator(LocalToWorldInverse.GetOrigin() - RandomPoint, LocalToWorldInverse.Rotator()) + GetActorLocation();
		DrawDebugBox(GetWorld(), GetActorLocation(), UnrotatedBox->Bounds.BoxExtent, FColor::Blue, false, 20000.f);		
		DrawDebugBox(GetWorld(), GetActorLocation(), UnrotatedBox->Bounds.BoxExtent, GetActorRotation().Quaternion(), FColor::Red, false, 20000.f);
	}
	return rotatedVec;
}

```

 ![randomRotatedAxis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/5/0/250453291c9c254c0cdc1280933628cf1fb2ae1f.jpeg)

 ![rot2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/0/b/b0bc9d8733cd83b66234582eb8c158bd18ec7d3c.png)

---

<div class="post-metadata">

**Author:** ![ImAGoodSpoon1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/imagoodspoon1/32/757809_2.png) [@ImAGoodSpoon1](https://forums.unrealengine.com/u/ImAGoodSpoon1)\
**Post date:** [December 28, 2023, 3:13am UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/6 "2023-12-28T03:13:31Z")

</div>

Thank you very much you guys @ClockworkOcean @3dRaven , now it’s working!  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/b/b/ebb29704e0d90ddd270a0f622d381b8a83adfc22.jpeg)

also could you explain a little bit about your code like why I have to inverse it first before rotating it?

---

<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:** [December 28, 2023, 8:49am UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/7 "2023-12-28T08:49:28Z")

</div>

Its actually not iversed it’s just an old var name that i used & forgot to change (previously it had .Inverse() at the end)

but that would cause the tansform to flip. So its just direct work with fmatrix.

---

<div class="post-metadata">

**Author:** ![ImAGoodSpoon1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/imagoodspoon1/32/757809_2.png) [@ImAGoodSpoon1](https://forums.unrealengine.com/u/ImAGoodSpoon1)\
**Post date:** [December 28, 2023, 1:30pm UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/8 "2023-12-28T13:30:58Z")

</div>

I see, thanks again!

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [January 27, 2024, 1:31pm UTC](https://forums.unrealengine.com/t/get-random-point-in-bounding-box-problem-if-rotated/1559921/9 "2024-01-27T13:31:17Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
