I need an algorithm for loading products onto a shelf

Hello! All I’m looking for is an algorithm which will load products of any size onto a shelf of any size perfectly so that the shelf is filled completely with little to no space left.

My guess is it works like this?

So I think the parts start from the “Start” and keep generating next to eachother until they reach the opposite corner, then they will generate to the next corner, however I am not sure how to fill in the blank area in the middle once the corners are done.

To recap:

An algorithm which loads a product of ANY SIZE into this shelf making sure as many products as possible can be filled and NO OVERLAPPING PRODUCTS or products hanging off the shelf AT ALL.

Any help is super useful, thanks!

1 Like

ALSO:

The product is called “Cola” in replicatedStorage AND ITS A MODEL but I’d like a function which can host a product in the parameters so I can load any product since there is more than one.

This would conflict with this.

Because for there to be no products hanging of the shelf, the size of each product must be a factor of the total size of shelf i.e size of shelf % size of product = 0

1 Like

(when I refer to “block” I mean the product that gets loaded)
You can use Area Size ÷ Block Size to get the amounts of blocks that can be fit inside within that area

For example:
Area: 10, 15
Block Size: 2, 3

10 / 2 = 5 blocks can fit on the X axis
15 / 3 = 5 blocks can fit on the Y axis
————————————————————————
Area: 6.5, 37.4
Block size: 4.2, 8.35

6.5 / 4.2 = 1.54761905 ≈ 1 block can fit on the X axis
37.4 / 8.35 = 4.47904192 ≈ 4 blocks can fit on the Y axis

As you see in the examples, to get the amount of blocks that can fit on one axis is to divide the area by the block size (A ÷ S). The result should then be rounded down (floor(A ÷ S)) to not have any hanging blocks

The code would go something like this, but I can’t test it out right now

local function LoadProduct(product: BasePart, area: BasePart) 
      local size = product.Size
      local x = math.floor(area.Size.X/size.X) 
      local y = math.floor(area.Size.Y/size.Y) 

      local pos = (area.CFrame*CFrame.new(area.CFrame.UpVector*area.Size.Y-area.CFrame.RightVector*area.Size.X)

      for i = 1, x*y do
            local clone = product:Clone() 
            clone.Position = pos
            clone.Parent = workspace

            pos += Vector3.new(x,0,0)
            if i % x == 0 then
                  pos -= Vector3.new(0,y,0) 
            end
      end
end

This is the result.

Also I forgot to mention, I don’t want the products to fill in the box, I want it to fill in the X and Z axis, not the Y axis.

This is what the result should look like:
image

It should be the shelf with the products nicely placed in an organised order on the surface. Just like a shelf.

THIS IS A ROUGH SKETCH, the products should obviously be organised.

It’s ok I figured it all out. Thanks for the help and time taken.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.