Legacy GM "Random" overlapping objects

Sk8dududu

Member
I need to make a room where upon entering, it creates approximately 20 circular objects at random locations.
The issue is that placing them truly random isn't aesthetically pleasing because it creates clusters, instead I want them somewhat evenly spread out.

I attempted to make it so they could not spawn on top of each other so that it would automatically be even, but then they never overlap which is kind of essential.

This picture is what I am aiming for.
For the object I have it set so that it creates a bubble using a random sprite and setting it to a random size, all I need to to figure out the "random" spawn locations. I don't want any of them to overlap past the center point of the object, and not to be empty spaces in the room.
Screenshot 2019-05-20 20.16.12.png

This is what I have already even though I'll likely need to change it.
This is a controller alarm I'm using to spawn them.
Code:
instance_create(irandom_range(50,490),irandom_range(50,910),obj_bubble);
if instance_count < 20
    {
    alarm_set(0,5);
    }
And this is the objects create event to check if it should stay or not.

Code:
image_xscale = random_range(0.50,1);
image_yscale = image_xscale;
depth = - 10;
if instance_nearest(x,y,obj_bubble) < 350
    {
    instance_destroy();
    }
 
Last edited:

TheouAegis

Member
One idea I had is to divide the area up into equally sized sectors. Then within each sector pick a random horizontal and vertical offset.
Code:
enum field {
    left = 50,
    right = 490,
    top = 50,
    bottom = 910,
    rows = 5,
    cols = 5,
    width = field.right-field.left,
    height = field.bottom-field.top,
    cell_width = field.width/field.cols,
    cell_height = field.height/field.rows,
    max_bubbles = 25    //I settled on this number because it fits your dimensions
}
bubbles = 20+irandom(5);
list = ds_list_create();
i = 0;
repeat field.max_bubbles ds_list_add(list,i++);
ds_list_shuffle(list);
i = 0;
alarm[0] = 5;
Code:
///Alarm 0 Event
if i < bubbles {
    var loc = list[|i++];
    var col = loc mod field.cols,
    row = loc div field.cols,
    horz = irandom(field.cell_width),
    vert = irandom(field.cell_height);
    with instance_create(col * field.cell_width+horz, row * field.cell_height + vert, obj_bubble) {
        image_xscale = random_range(0.5,1);
        image_yscale = image_xscale;
        depth = -10;
    }
    alarm[0] = 5;
}
else
   ds_list_destroy(list);
Something like that.
 
Top