Random Object Position Without Overlapping

Blitz3D Forums/Blitz3D Programming/Random Object Position Without Overlapping

RustyKristi(Posted 2016) [#1]
I have 2d grid with 0 at center and dimension of -10000 to 10000 units. I wonder if there's a random positional mapping already done here that I have missed without overlapping.

The objects are 300x300 units in width and length.


RemiD(Posted 2016) [#2]
personally i use a combination of a 2d distance check and sometimes a meshesintersect check with low tris shapes (if too near of another entity).
and i use goto Line to redefine the position/orientation until a number of (100) tries have been attempted to prevent an unlimited loop.
if done correctly this is fast...
;create a temporary shape with the wanted properties
ChoosePositionOrientationTries% = 0
.LineChoosePositionOrientation
;define position
PositionEntity()
;define orientation
RotateEntity()
;check if the temporary shape is far enough or too near from another shape
;if far enough
 ;create a final shape with the wanted properties
 ;position oriente the final shape with this position orientation
;if too near
 ;use meshesintersect to check if the temporary shape intersects or not with the other shape (facultative)
 ;if it does not intersect
  ;create a final shape with the wanted properties
  ;position oriente the final shape with this position orientation
 ;if it intersects
  ;if ChoosePositionOrientationTries < 100
   ;go to LineChoosePositionOrientation
  ;else
   ;exit



Matty(Posted 2016) [#3]
Create an array of the right size and specify whether a cell is occupied or not and then check before placing and if unoccupied place the object and mark the cells as occupied.


RemiD(Posted 2016) [#4]
yes if your shapes are positioned on a grid, it is better to use a 2d array
Cell_State(99,99) ;(X,Z) Empty or Full

depending on where is the origin in your shape (center or backleft), and depending on its origin global position, and depending on its width/depth, you can determine the area used by the shape (minx, minz, maxx, maxz)
each time you position a shape, calculate the minx, minz, maxx, maxz, then check if the cells in this area are empty or full, if empty, you can position the shape and then modify the state of the used cells, if full, you can't position the shape here, choose another position

Floor() will be a useful function to determine the integer value without the float value (45.123456 becomes 45)


RustyKristi(Posted 2016) [#5]
thanks Matty and RemiD. I already got the map x,y and I was looking for any alternative if there's one.