#Need help with a Physics Dungeon Generation Algorithm

1 messages · Page 1 of 1 (latest)

night sundial
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DungeonSpawner : MonoBehaviour
{
    [SerializeField] private Rigidbody2D dungeon;
    private List<Rigidbody2D> _rooms;

    private void Awake()
    {
        _rooms = new List<Rigidbody2D>();
    }

    private void Start()
    {
        for (int i = 0; i < 5; i++)
        {
            Rigidbody2D room = Instantiate(dungeon);
            room.position = Vector2.zero;
            _rooms.Add(room);
        }

        StartCoroutine(SettleTimer());

    }

    private IEnumerator SettleTimer()
    {
        yield return new WaitForSeconds(1);
        foreach (Rigidbody2D room in _rooms)
        {
            room.bodyType = RigidbodyType2D.Static;
        }
        
        Debug.Log("All done");
    }
    
}```

I have a Dungeon TileMap. Attached to it is a RigidBody2D along with a basic Box Collider.

The idea is this: I will spawn X prefab dungeons at Origin. I will allow the Physics Engine to determine thier final placement. I stole this idea from a KidsCanCode video but its in Godot 3 / there is another algorithm that someone wrote in Godo with a similar idea. 

The issue I am having is the rooms are not fanning out like I'd expect.
#

Instead they fan out to look like this.

#

How do I get them to look more like... 1 sec.

ember hearth
#

I'll say right away that relying on physics to fix the collisions for you, is gonna be very unreliable and uncontrollable.

night sundial
#

Like this.

#

Except not as many rooms.

ember hearth
#

If you just want to push the objects outwards untill there's no collision, you can do that manually.

night sundial
#

Is the only way to go about this to apply a random force (in a random direction) and to set the RigidBody2D to static once the CollisionExit() event has been called?

ember hearth
#

In any case, using physics for this is a bad idea.

#

If you want to use the "pushing out randomly" approach, you can do it without physics and check for collisions with bounds.