Is there any good idea to confirm that no collision occurs in entire world in runtime

First of all English is not my native language.
And I am a newcomer

My problem is: At runtime, gameobjects that collide with each other are pushed away by the physics engine to ensure that there is no collision. Is there a way to know that no collisions have occurred after the physics engine is applied, and at runtime?

Project information that might come in handy. I am designing a procedural generated map script, my running process is based on : __Programming recent news | Game Developer
The author’s .gif picture is also attached as an example

I want to know if there is a physics engine API that can view collision events in the entire world.

Any suggestions are greatly appreciated.

7527158--928982--Example.gif

What’s wrong with just printing something out if OnCollisionEnter is triggered for each box? If it prints something, then another collision occured.

I would not recommend using unity collision functions for this because they’re not intended to be used this way.

They only happen during the physics processing loop, as seen here:

https://docs.unity3d.com/Manual/ExecutionOrder.html

You could use overlap and cast kinds of operations, but it is probably better to just do the comparisons yourself to have more explicit control.

This guy has also written a lot about procedural dungeon creation:

https://journal.stuffwithstuff.com/2014/12/21/rooms-and-mazes/

1 Like

Hey!
That seems like a pretty advanced tutorial for a newcomer.

Thera are many ways to do it. One is that you use. I woudl recommend Unity - Scripting API: Physics2D.OverlapBox

public class Room : MonoBehaviour
{
    //Settings to have the detection efficient
    [SerializeField]
    private float checkDelay = 0.5f;
    [SerializeField]
    private int checkLimit = 100;
    private int currentCheckLimit = 0;

    //Settings for the OverlapBoxCast
    public Vector2 exampleRoomSize = new Vector2(10, 30);
    public Vector2 roomCenterPointWorldCoordinates = new Vector2(5, 10);
    public LayerMask roomCollidersLayerMask;

    private bool isColliding;

    public bool IsColliding
    {
        get { return isColliding; }
        private set { isColliding = value; }
    }

    //When the room is created start checking for collisions
    private void Start()
    {
        StartCoroutine(CheckCollisionCOroutine());
    }

    /// <summary>
    /// Coroutine that checks for collisions with other rooms
    /// </summary>
    /// <returns></returns>
    private IEnumerator CheckCollisionCOroutine()
    {
        yield return new WaitForSeconds(checkDelay);

        IsColliding = CheckCollision();

        if (IsColliding == true && currentCheckLimit < checkLimit)
            StartCoroutine(CheckCollisionCOroutine());
    }

    /// <summary>
    /// Methods that uses OverlapBox to cast a boxcast and detect if there are any collisions
    /// </summary>
    /// <returns></returns>
    public bool CheckCollision()
    {
        return Physics2D.OverlapBox(roomCenterPointWorldCoordinates, exampleRoomSize, 0, roomCollidersLayerMask) == null;
    }

}

Basically what it does is checks every 0.5 second if there is a collisions or not. After 100 checks if the collision is still there it gives up on this room.

Again it seems like a pretty advanced tutorial. If you are interested in the procedural 2d dungeon generation maybe you will find my tutorial useful:

I hope it helps!

1 Like

Thanks for everyone’s suggestions.
I just finished the core part of my script, and the attached file .gif is a demo.
Thank guys

7530221--929612--demo.gif

3 Likes