Search code examples
javascriptmatter.js

My own model in matter.js


I'm using Matter.js to program a simple game. I'm having trouble figuring out how best to hook my model into Matter.js.

My game features bacteria, and I'd like to have a class Bacterium so that I can manage these guys. In my current implementation, this class creates and stores its own Matter.Body, which I pass to the engine. This is where I'm running into trouble. I'd like to update certain aspects of my model when bacteria collide with each other, and with walls, but I can't find an efficient way to locate which bacteria in my model are colliding in my Matter collision handler.

Should I extend Matter.Body's prototype to do this? Any advice, or larger matter projects on the web that I should look at?


Solution

  • Take a look at the Render.collisions function in the Render.js class. This function renders all the collisions that occur in the frame that is being rendered. I think by understanding how they render it there you can work out what you need in you situation.

    Remember that matterjs uses a game loop which is really different from the event based setup that webpages normally work with. Take a look at the Render.js class to better understand how to work with working with the engine that matterjs provides.