5
\$\begingroup\$

This is a rather complex question and I'm more hoping for general algorithms anyone might have heard of.

I wish to generate randomly sized rooms. Each of these rooms has a type and a corresponding list of furniture to be placed inside it (ie: kitchens can have fridges, both kitchens and living rooms can have TVs, etc). Each piece of furniture has it's own collider (cylinder, cube, mesh...). Are there any good algorithms to randomly lay them out?

The best idea I've managed so far is to subdivide the room into a grid the size of the smallest piece available. Then I would randomly place the furniture until every grid square is at least partially filled. However, this is hardly ideal and the rooms would more or less look like warehouses.

[Optional]How do I randomly place the furniture in such a way as to make it at least look slightly realistic?

\$\endgroup\$

1 Answer 1

7
\$\begingroup\$

I had to solve a similar problem for my game. I did it using templates. Basically, start by labeling your room with the following:

enum TileState
{
    Floor, // An unoccupied free cell
    Wall,  // A cell occupied by a wall
    Edge,  // An unoccupied cell on the edge of the room (like a door)
    Object // A cell occupied by an object
}

Then, you can create templates sort of like this:

// This template corresponds to an object in the corner.
TileState[][] cornertemplate = { { Wall,  Wall},
                                 { Wall, Object} };

Now, you can try a bunch of random x, y locations and rotations of the template, and see if the template is valid. In this context, a template is valid whenever all the Wall tiles are occupied by a wall, and all of the Object tiles are currently free (either Floor or Edge). Whenever the template is valid, create an object at the appropriate place.

You can make more complicated templates by overlaying them on top of each other, or stringing them together. For instance, you can require a chair always be next to a table. Here is a room I generated using this approach:

enter image description here

In this case, the tables had a template that required all the space around them to be free of walls. The bookshelves had to be up against at least one wall, and the lamps had to be in corners.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ @PedroCaetano www.dwarfcorp.com \$\endgroup\$
    – mklingen
    Apr 16, 2015 at 16:56
  • 1
    \$\begingroup\$ Nice game! I watched the video. You (or someone else) talked about releasing the new alpha to the backers. So I presume there is no way in testing it out for me, since I didn't back and the campaign ended? \$\endgroup\$ May 31, 2015 at 8:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .