Well I’m not too sure how to approach this, so I’ll give what I think could work as a starting point at the very least through a 2D example of a similar problem.
Without Borders
So imagine you had a scenario where you wanted to know any random point in 2D space on a 2D rectangle with the dimensions (5, 7), with the Vector2 point in space being (160, 50) - assuming that point is the midpoint of the rectangle, as well as the rectangle is vertical. So here’s how I’d do it:
- Get the furthest left and right X coordinate points of the rectangle in 2D space. For the left side, you’d want to take the rectangle’s X coordinate in space (160) - the X length of the triangle (7) - the midpoint of the rectangle (3.5), expressed as 160 - (7 - 3.5). From this, we’d get the answer of 156.5, which is the furthest left our random points should be able to go. Then we’d just do the same thing for the right side, but instead of subtracting 160 by 7 - 3.5 like above, we’d do 160 + (7 - 3.5).
- Also get the furthest bottom and top Y coordinate points of the rectangle in 2D space. This is pretty much the same as step 1, but for the Y coordinate. For the lowest point do 50 - (5 - 2.5), and for the top do 50 + (5 - 2.5).
- Now, finally, to get a random point on the 2D rectangle do this:
local Xmin = 156.5
local Xmax = 163.5
local Ymin = 47.5
local Ymax = 52.5
local randomPointX = math.random(Xmin, Xmax) --> Random number between 156.5 and 163.5
local randomPointY = math.random(Ymin, Ymax) --> Random number between 52.5 and 52.5
randomPointX
and randomPointY
now represent your new randomly-selected point. And I didn’t put any metric denotations in this, just to make it more general.
With Borders
If you did the same scenario as the above, but with borders on all sides that are 1 units thick, here’s the solution (I believe).
- To calculate the
Xmin
and Xmax
values now you’d have to take into account the 1 unit of the border. So for Xmin
, instead of doing the earlier equation, you’d now do (160 - (7 - 3.5)) + 1, and for Xmax
do (160 + (7 - 3.5)) - 1.
- And following in the lead of the revised step 1, for
Ymin
you’d do (50 - (5 - 2.5)) + 1, and for Xmax
do (50 + (5 - 2.5)) - 1
- Then the final result for finding a random point would be:
local Xmin = 157.5
local Xmax = 162.5
local Ymin = 48.5
local Ymax = 51.5
local randomPointX = math.random(Xmin, Xmax) --> Random number between 157.5 and 162.5
local randomPointX = math.random(Ymin, Ymax) --> Random number between 48.5 and 51.5
So with borders, you’re just adding or subtracting the thickness of the border to the original solutions for Xmin
, Xmax
, Ymin
, and Ymax
.
Let me know if there’s any errors in the above, or any questions. And sorry, I know this isn’t a fix to your problem, but I hope it helps with the logic you can use when incorporating 3D vectors and the offsets you set up.