Recursive Collision

Recommended Posts

Hi all,

I have been stuck for some time on procedurally generating levels for my game. As it stands, I have created rooms in a confined space but most of them overlap. Although this is expected, I can't find a way to separate them. My attempts have been centered around physics collisions, but I have failed hard. Here is my current code:

Level.prototype.separate = function() {
	//For Convenince sake
	var arcade = this.game.physics.arcade,
		rooms = this.rooms;

	for ( var i = 0; i < rooms.length - 1; i++ ) {
		while ( arcade.intersects( rooms[ i ], rooms[ i + 1 ] ) ) {
			arcade.collide( rooms[ i ], rooms[ i + 1 ] );

	//For testing purposes

Level.prototype.create = function() {
	this.game.physics.arcade.enable( this.rooms );
	this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;

	//Gets coordinates and dimensions for Rooms and creates them.

	console.log( "Level State Completed" );
	//this.game.state.start( 'InGameState' );

Level.prototype.update = function() {

This doesn't work. It results in an infinite loop. I have tried used Arcade's overlap as well.

The issues I have had boil down to these two questions:

1. How can I test for individual room collisions when they sit in an array together?

2. When two rooms collide with one another, one or both of the rooms may still be colliding with other rooms. However, Arcade's collide method isn't recursive, and throwing the separate function into update might not guarantee the rooms be separated and hurts performance.

Does anyone have any ideas or potential solutions to this?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.