3

I want to randomly place objects USING ALPHA MAP (only black color, not grayscale). The black areas on the map are using to determine where we CAN place an object.

Why do I want to do this? For example, we have some terrain with a river. We want to randomly place a chest on that terrain and the point is we want our chest NOT TO BE IN THE RIVER. We have a special location to place that chest and that location can have a very complex structure.

Simple map with lake and river:

link for simple map with lake and river image

Black/white map of the location for placing objects:

link for black/white map of the location for placing objects image

Of course, we can just take random points with Random.Range() and check each point by comparing it with pixel values: "Is the point on the black area?" BUT if we would have a very small (<10% of total area) and complex "available" area (for example, islands in the swamp) then there will be a very large amount of "garbage" points. Therefore it is very inefficient method.

Does the quick and performance technique exist to get desirable amount of "available" points?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • I would keep a black and white version of the map separately to test. From that you just have to check the b&w version for placement test. – Evan Carslake Mar 14 '16 at 19:52

1 Answers1

2

In theory you could segment the alpha map by color to get a geometric polygonal representation of the regions, then you could generate a point algorithmically inside a set of black/white polygons. But If you're willing to trade memory for speed, there's a much simpler solution: just represent the alpha map as two arrays(black & white) of pixels' coordinates and then randomly pick a point from a needed array.

yuyoyuppe
  • 1,582
  • 2
  • 20
  • 33