#Dungeon room placement

1 messages · Page 1 of 1 (latest)

hazy gull
#

Hi, I'm generating dungeon rooms which connect at exits which are empties where doors would be, but I can't figure out the math to place them correctly. Here's the current functionality and a screenshot. This is for a game jam as well so it's slightly urgent.

foreach(DungeonRoomExit exit in exit_nodes)
{
    if (!exit.occupied)
    {
        int rand = Mathf.FloorToInt(Random.Range(0, room_prefabs.Count));
        DungeonNode new_room = Instantiate(room_prefabs[rand]);

        int rand2 = Mathf.FloorToInt(Random.Range(0, new_room.exit_nodes.Length));



        // find the new rotation of the new room
        float new_rot = new_room.exit_nodes[rand2].transform.rotation.eulerAngles.y + new_room.exit_nodes[rand2].transform.rotation.eulerAngles.y;

        new_room.transform.rotation = Quaternion.Euler(new Vector3(0, new_rot, 0));
        new_room.transform.position = exit.transform.position - new_room.exit_nodes[rand2].transform.localPosition;
                
        // draw connection
        Debug.DrawLine(exit.transform.position, new_room.exit_nodes[rand2].transform.position, Color.red, 60f);

        new_room.exit_nodes[rand2].occupied = true;
        exit.occupied = true;
        return;
    }
}
#

Ideally the red line between exits shouldn't be visible because they would overlap

minor pendant
#

you want them to overlap?

hazy gull
#

Yeah, let me take a better screenshot

#

In this case the two connections are across the second room

#

My goal is to have it rotated 180 from that position, but no matter what I try it comes out wonky

#

Here's the code that placed that room

#
foreach(DungeonRoomExit exit in exit_nodes)
{
    if (!exit.occupied)
    {
        int rand = Mathf.FloorToInt(Random.Range(0, room_prefabs.Count));
        DungeonNode new_room = Instantiate(room_prefabs[rand]);

        int rand2 = Mathf.FloorToInt(Random.Range(0, new_room.exit_nodes.Length));



        // find the new rotation of the new room
        float new_rot = exit.transform.localRotation.y + new_room.exit_nodes[rand2].transform.localEulerAngles.y;

        //new_room.transform.forward = new_room.exit_nodes[rand2].transform.forward;
        new_room.transform.rotation = Quaternion.Euler(new Vector3(0, new_rot, 0));
        new_room.transform.position = exit.transform.position + new_room.exit_nodes[rand2].transform.position;
                
        // draw connection
        Debug.DrawLine(exit.transform.position, new_room.exit_nodes[rand2].transform.position, Color.red, 60f);

        new_room.exit_nodes[rand2].occupied = true;
        exit.occupied = true;
        return;
    }
}
hazy gull
hazy gull
# minor pendant define wonky

The two connections never line up and are always at the wrong angle. The goal is for the empties at the openings to be completely flush

#

In this case, they're 180 from each other

#

And adding 180 or many of the things I've tried haven't helped and have just left me stumped

#

It feels like the euler angle should be the angle of the first opening plus the angle of the target opening, but that didn't work

minor pendant
minor pendant
#

sorry im a little slow on responses rn im tryna do some stuff with my editor

hazy gull
#

You're good, I'm just fiddling with it trying to make it work

hazy gull
#

One sec

#
foreach(DungeonRoomExit exit in exit_nodes)
{
    if (!exit.occupied)
    {
        int rand = Mathf.FloorToInt(Random.Range(0, room_prefabs.Count));
        DungeonNode new_room = Instantiate(room_prefabs[rand]);
        int rand2 = Mathf.FloorToInt(Random.Range(0, new_room.exit_nodes.Length));

                
        Transform con_1 = exit.transform;
        Transform con_2 = new_room.exit_nodes[rand2].transform;

        float angle = (180 + con_1.eulerAngles.y) - con_2.localEulerAngles.y;

        new_room.transform.position = exit.transform.position;
        new_room.transform.Rotate(new Vector3(0,angle,0));
        new_room.transform.position += con_1.localPosition;

        // draw connection
        Debug.DrawLine(exit.transform.position + Vector3.up, new_room.exit_nodes[rand2].transform.position + Vector3.up, Color.red, 60f);

        new_room.exit_nodes[rand2].occupied = true;
        exit.occupied = true;
        return;
    }
}
hazy gull
minor pendant
# hazy gull

well the difference between that one and the rest is that the empty wall side is on the left