How do I distribute sprites randomly on a line

0 favourites
  • 12 posts
From the Asset Store
solution for games like "fruit ninja" and drawing applications
  • Hey guys!

    I'm looking for an algorithm to randomly distribute sprites on the line.

    Let's say the line length is 100 px, and each sprite is 10 px wide.

    Number of sprites can be different, from 1 to 8.

    Sprites can touch, but not overlap.

    It's not a problem to distribute 4 sprites, but with 5 or more it's possible to run out of free space if I just place them randomly.

    Seems like an easy task, but I can't figure it out.

  • Well 10x10= 100

    So pick a random number you want of objects 1-10

    Make a non repeating list of values of that many

    For each in the list create object at list value*10

    Array works well with "contains value", and as a stack.

  • newt

    If I understand your method correctly, it will distribute sprites in 10px intervals. I don't want this.

    See my picture above - one sprite may be at x=8, another at x=21 etc.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks, oosyrag

    Unfortunately, most of the dropbox links in that post are dead and the one that's working is not very good. It uses "brute force" method - creates sprites at random positions and then destroys sprites that overlap.

  • Maybe something like this? It assumes the sprite's origin is on the left, that probably is a quick tweak. It's also untested so I'm not sure how well they distribute on the line. A second idea would be to first evenly distribute the sprites then jitter them around while preserving gaps to be >0

    n : number of 10px sprites

    FreeSpace = 100- 10*n

    Gap:0

    X:0

    Repeat n times

    ---set gap to random(freespace)

    --- subtract gap from freespace

    --- add gap to X

    --- create Sprite at (X,0)

    --- add 10 to X

  • Thanks, R0J0hound! You rock again!

    Your method works. Although sprites tend to group more towards the right end of the line, but this actually is even better for my game.

  • That's good to hear.

    If you later want it to be uniformly random here's a second idea. Basically for n objects there is n+1 gaps. My thought is if we generate a random number for each gap and then sum them up, we can then find a factor to convert the sum to freespace and use the array values and the factor to get gap widths.

    Sum=0

    Freespace = 100-10*n

    Array size (n+1,1,1)

    Array for each X

    --- set current X to random(1)

    --- add curx to sum

    Factor = freespace/sum

    X=0

    Repeat n times

    --- add array.at(loopindex)*factor to X

    --- create Sprite at (X,0)

    --- add 10 to X

  • Nice, thank you!

  • newt

    If I understand your method correctly, it will distribute sprites in 10px intervals. I don't want this.

    See my picture above - one sprite may be at x=8, another at x=21 etc.

    Ok, well you said not overlapping, but possibly touching.

    If non multiple of 10 was wanted, just do a randomiser afterwards

    for each

    distance(sprite(loopindex-1).x,sprite(loopindex-1).y, sprite.x,sprite.y) >5 and distance(sprite(loopindex+1).x,sprite(loopindex+1).y, sprite.x,sprite.y)>5, set x to self.x+ random(-5,5)

  • Yeah, this will work too, thanks.

  • Ok, but you have to filter out the first, and last instances.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)