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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.