Matterjs how to re-check "collisionstart"?

Screenshot 2023-12-08 023547

I create a physics game using Matterjs, when an object collide with other same object, one of the object will be destroyed and the other one will be level up (change texture and bigger size)

The problem is, the collision with same object (type) is often not working right away, I’m sure it’s the method I use to handle the logic.

Mostly, the problem happened when Object type 3 already touching Object type 4, then The object type 3 collide with another type 3, then that object will be upgraded to type 4, and that new type 4 is already collide with another type 4 (before the levelUp happen), but nothing happen, I think because “collisionstart” called once after collide.

Hope you understand what I mean

My code:

this.matter.world.on('collisionstart', (event, bodyA, bodyB) =>
		{
			if(bodyA.gameObject && bodyB.gameObject){
				if(bodyA.gameObject.isObject && bodyB.gameObject.isObject){
					if(bodyA.gameObject.level === bodyB.gameObject.level){
						let centerPos = getCenterPosition(bodyA.gameObject, bodyB.gameObject);
						levelUpObject(bodyA.gameObject, centerPos);
						bodyB.gameObject.destroy(true, true);
					}
				}
			}
		});

Any solution to fix this ? or is there a function to re-call “collisionstart” ? maybe that function will fix my issue.

Thanks

I’m not sure if there is a function to force the collisionstart event to be triggered, but would you be able to use the collisionactive event? This event would fire for every update tick, so it should allow you to check for collisions between pairs that are still colliding.

More info can be found here: Phaser.Physics.Matter.Events.COLLISION_ACTIVE - Phaser 3 API Documentation (beta)

1 Like