I have a shape like this and I want to generate positions for enemies to spawn. I want the positions to be inside the black line but outside of the red line. I’ve tried using static positions and math.random to generate them but, since the border isn’t directly on an axis, the position generated wouldn’t be correct. And I would have to generate a lot of points just to get the position of the border.

Let’s say I was trying to generate random positions inside this orange square. I would use math.random(-1,1) to generate Y and math.random(-2,2) to generate X. But it actually generates a square like this:

I would have to do something like this to generate all the points on the square

I feel like that would be very inefficient and would take up a lot of processing just to do.
If anyone could help me, It would be appreciated!

The most simple method is to pick a random point in the bounding box of the geometry and then checking whether it is inside the polygon. Repeat until a valid position is found.

Otherwise, you will need triangulation techniques to generate triangles which then you randomly pick out of all possible triangles and then randomly pick a point in the triangle. This can be a hassle to deal with.

This is not an easy question to answer.

Also I thought about a skewer method. I’m not certain if raycasting only hits surfaces or the body of an object. Raycast from a random point of the bounding box into the geometry, then again with offset into the geometry and hope that it hits the other surface, and then pick a random point in the ray inside the geometry.

Here is a nice module for zoning. I’ve seen projects with this randomly spawn coins in areas determined by parts. Not sure if it’s perfect for your use case though, and I suspect they might have just gotten a random position and checked if it’s inside a zone.