# How to calculate the mesh size of a row in shelf of a library if the library is rounded

I have a library in unity connected to an online database and in database I add entries about books and information (metadata) about each book like author, title, publisher… I have connected the library in Unity with database using an api and once I add a book with its information in database, I show the book in Unity with book information. The library in Unity is rounded and has two floors. The ground floor has 7 shelves and each shelf has 5 rows. At the moment I have included the places for each book in every row manually, so I have put the colliders manually where the books can be placed. Now I want to do this automatically, so to put the places/colliders for each book automatically. So I want to calculate how many books a row in each shelf can take taking into consideration distance between books and width of book. This way I can calculate automatically how many books one floor can have and so it can also calculate automatically that if the number of books in database exceeds the number of books one floor can take, then another floor can be added.

I have uploaded images of library and a script that I have wrote to complete this task. In the script I try to get the mesh size of each row and I use a formula:

noOfBooks = sizeofRow.x - sizeofBook.z - distanceBetweenBooks;

so I get size of row minus the size of book width (I assign this myself) minus distance between books (I assign this myself). The result is that a row can take12 books but the rows can take like 45 books if I put those manually. So I am thinking that the script is not working very well.

Many thanks.

7276585–879187–MeshSize.cs (971 Bytes)

I’m having trouble following that wall of text, but if your books are not packed tightly enough, you need to track down the input values.

Purify your data all the way up to the start:

• load up the geometry in your 3D graphics editor and get a numeric value for how long the bookshelf is, such as by adding up the lengths of the segments going around it. Or if you know it is cylindrical, you can treat it as the circumference of a circle and just use basic geometry for that.

• fix your code until it returns the correct length given those inputs.

Now you have the right length, divide it by book count and see what visual spacing that gives you.

2 Likes

Your calculation is definitely off, you need division. If you have a shelf 100 units wide and a book that’s 1 unit wide, the number of books that could fit on the shelf without spacing is 100 / 1 = 100. The formula should be shelf size / (book size + margin)

1 Like

Thanks for your advice. Do you know how to get the width of each row in one shelf? Or should I use the same code:

sizeofRow = rendererRow.bounds.size;
Debug.Log(“The size of the row is:” + sizeofRow);

I transformed the code to the following:

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshSize : MonoBehaviour
{

public Vector3 sizeofRow;
public Vector3 sizeofBook;
public MeshRenderer rendererRow;
public  MeshRenderer rendererBook;

float noOfBooks;
int distanceBetweenBooks = 5;

void Start()
{
//we get the size of row
sizeofRow = rendererRow.bounds.size;
Debug.Log("The size of the row is:" + sizeofRow);

//we get the size of book
sizeofBook = rendererBook.bounds.size;
Debug.Log("The size of the book is:" + sizeofBook);

noOfBooks = sizeofRow.x / sizeofBook.x - distanceBetweenBooks;
int noOfBooks2 = (int) noOfBooks;

Debug.Log("Number of books that this row can take is:" + noOfBooks2);

}

}
``````

The result is: Number of books that this row can take is: 45

Is this correct? I am not sure if sizeofBook.x is correct I want to get the width of the book’s spine