Skip to content

Error/crash when creating & destroying worlds for more than 32 times #39

@MLukman

Description

@MLukman

VERSION

Phaser Box2D v1.1.0

PROBLEM

  1. The game crashes due to error when creating and destroying worlds for more than 32 times (which is valid use case for retrying levels and it's cleaner than cleaning up all bodies, forces, particles etc)
  2. This is due to the library pre-create an array of 32 worlds that is used when the CreateWorld() is called.
  3. However, calling b2DestroyWorld() does not allow the used worlds to be re-used. The subsequent CreateWorld() will simply use the next world in the array.

EXPECTED RESULT

The b2DestroyWorld() should allow the world to be reused, probably by resetting the world inUse flag back to false.
Or perhaps the function needs a Phaser-specific version that does the resetting, similar to CreateWorld() is the Phaser-specific version of b2CreateWorld().

STEPS TO REPRODUCE

  1. Create a Scene with the following create() function:
	async create() {
		const worldDef = b2DefaultWorldDef();
		const bodyDef = b2DefaultBodyDef();
		bodyDef.type = DYNAMIC;
		bodyDef.position = new b2Vec2(0, 0);
		for (var i = 0; i < 35; i++) {
			const worldId = CreateWorld(worldDef).worldId;
			console.log("Created Box2D world with id:", worldId);
			b2CreateBody(worldId, bodyDef);
			b2DestroyWorld(worldId);
			await new Promise(resolve => setTimeout(resolve, 500));
		}
	}
  1. Run the game on web platform
  2. Inspect the console log to see the error:
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions