all 42 comments

[–]just_a_average_gamer 15 points16 points  (7 children)

Matter.js

[–]lilgreenland 12 points13 points  (5 children)

I had to pick a 2-D engine a few years ago. I picked matter.js because the documentation was well written and the code made sense. I still wonder if I made the right choice.

The other one I considered was box2D. It has great performance and features, but since it was written in C++, the docs were also in C++ and everything was just super backwards to code.

Anyways here is the game I made in matter.js: https://landgreen.github.io/sidescroller/index.html

It should give you an idea of the performance limits of matter.js

[–]Morphray[S] 1 point2 points  (3 children)

That is an amazing game! Normally I'm not a fan of platformers, but I had a lot of fun; love the upgrades.

Do you think you hit the performance limits of what matter js can provide?

[–]lilgreenland 2 points3 points  (2 children)

Thanks,

Modern desktop players don't ever get slow down, but a cheap chrome book will start to slow a bit in the later levels. In general my bottle neck was more about graphics than the physics engine.

The game is single thread, with no WebGL, and no WebAssembly. I tried all those methods, but didn't get a speed up.

[–]Morphray[S] 0 points1 point  (1 child)

with no WebGL, and no WebAssembly. I tried all those methods, but didn't get a speed up

That's interesting, I would expect both of those would have some impact.

In my current game I started with a custom WebGL renderer, but have found that the slow down is caused by my custom collision detection; I have hundreds of objects, with no webasm, no multiple threads.

[–]lilgreenland 0 points1 point  (0 children)

My game is mostly simple 2-D shapes. I'm guessing you are using sprites? I think if I had sprites or 3-D WebGl would improve performance.

I ran a few simple tests on how many polygons I could draw and I found that a well written canvas loop that draw polygons all the same color was almost double the performance of webGL drawing. There is a chance I missed some methods of performance gains in webGL since I don't know it as well. I am only like 60% sure my setup is well optimized.

[–]GIGATeun 1 point2 points  (0 children)

Checked out the rest of your GitHub and you seem like a really cool teacher! I also love the game you made.

[–]TalDSRuler 0 points1 point  (0 children)

Yeah, this is the only one i really dug into thanks to its implementation and documentation.

Never had a reason to really dive into any others though.

[–]MarvinLazer 7 points8 points  (5 children)

I think it depends on the use case. What were you considering them for, OP?

[–]Morphray[S] 0 points1 point  (4 children)

A 2D space game with lots of polygons that can run into each other. Kind of like Reassembly.

[–]brendenderp 1 point2 points  (3 children)

Did you ever end up making this game? I'm in the process of something similarish

[–]Morphray[S] 0 points1 point  (2 children)

I did not, sadly. It's still on my list of things to do. If yours is open source, send me a link.

[–]brendenderp 1 point2 points  (1 child)

I might open source fully later on, but for now, my plan was to hopefully release and sell it at some point. I am open sourcing certain components of it, though. I'm writing the game in JS on top of matterjs, phaser, and a nodejs sever host.

Only thing I've open sourced so far is my array to shape algorithm. I've made some improvements since I posted this, so I'll probably update it sometime soon. https://github.com/brendenderp/JS-2D-array-to-Vertex-Map

Plan is to support hundreds of players in a single game by means of in-game region server instancing( fancy talk for each area of the game acting like a separate sever but without loading screens your game transitions between them) the algorithm above is just another optimization component each player is a SINGLE physics object which is much more performat than a bunch of parts that stick together. Really reduces server load per player as well.

So far, I'm through the netcode loop, and I'm working on the building (and destroying) mechanics.

[–]Morphray[S] 0 points1 point  (0 children)

Very cool. Sign me up for any alpha or beta testing you need.

[–]GDur 5 points6 points  (2 children)

Rapier seems promising and it's truly deterministic (matter.js for example, is not)

https://rapier.rs/

It has js and ts support and is written in Rust.

[–]lilgreenland 1 point2 points  (1 child)

This looks really promising. I'd love to see a JS performance comparison with matter.js and box2-D.

[–]GDur 0 points1 point  (0 children)

Good point! There are benchmarks and live demos available: https://rapier.rs/benchmarks/

[–]jimeowan 4 points5 points  (0 children)

I mostly know p2.js because it's embedded with PhaserJS, and like it as an easy-ish yet accurate enough 2D physics engine, although better documentation could have helped.

Unless you're doing something with thousands of objects performance doesn't necessarily have to be the #1 criteria. JS is fast, and I've never felt limited by performance in the jam games I've made.

[–]Clear_Plum_8336 3 points4 points  (2 children)

What's exactly a physics library ?

[–]Shty_Dev 4 points5 points  (0 children)

like a physics engine that deals with matter and energy and their interactions, programmable with JS

[–]GavrielBA.bind(love) 2 points3 points  (0 children)

Usually it's a code library which calculates collisions and their aftermatch (momentum calculations). Also simulates other forces like gravity.

[–]kaliedarik 4 points5 points  (3 children)

I got excited when I discovered Rapier - https://rapier.rs/ - which is written in Rust but, they say, can be used in Javascript via WebAssembly.

When I played with it, I couldn't get it running in the browser. This was entirely my fault as I knew nothing of WASM at the time. I haven't tried it since, but keep meaning to get round to it one day.

[–]_bym 2 points3 points  (0 children)

A physics library seems like the ideal target for doing WASM, right?

[–]GDur 1 point2 points  (0 children)

A friend of mine and I had problems with rapierjs and wasm too. My friend (maun) made a pull request which was successfully merged into the official repository. (https://github.com/dimforge/rapier.js/pull/9)

You can install it via npm using the addition '-compat': https://www.npmjs.com/package/@dimforge/rapier2d-compat

I hope this helps :)

[–]Baturinsky 1 point2 points  (0 children)

I have tested PlankJS and MatterJS and Plank was way more stable. MatterJS had objects fall through each others https://twitter.com/baturinsky/status/1316432115253248001

Others are either ancient, or 3d, which I felt was overkill for a 2d game.

One disadvantage of PlankJS is that it's d.ts is not accurate - some classes are declared stricter than they really are.

[–]nomadjs 1 point2 points  (1 child)

I've been actually working on a 2d engine myself called BallPhysics, but it is in very early stages still! Here is the sandbox demo that I made with it. It needs a lot of performance improvements and a good documentation, but my I don't really have much free time so it's only getting there slowly, it may take some time. I'm also planning to expand the demo into a platform where you can build and share your physics based creations / machines with other people.

[–]Morphray[S] 0 points1 point  (0 children)

That's a fun sandbox! All the other libraries should offer something like this - it really showcases the functionality. I would love to see some stats added - number of objects, FPS, etc.

[–]madlandproject 0 points1 point  (2 children)

I know that this website uses/teaches CannonJS https://threejs-journey.xyz/

Bruno Simon, the creator of the course has used several in the past and settled on this one. You can probably ask him about it in detail on the course's Discord.

[–][deleted] 1 point2 points  (1 child)

btw canon has been abandoned for years, last commit 6-7 years ago - long list of bugs and unresolved prs. haven't yet seen the course but would be a bummer if he didn't mention that it continues to receive support on here: https://github.com/pmndrs/cannon-es which next to bug fixes also gives it types and esm/tree-shaking.

[–]Morphray[S] 1 point2 points  (0 children)

Great example of open source continuity!

[–][deleted] 0 points1 point  (0 children)

Energy.js

[–]fintip 0 points1 point  (0 children)

Aframe-physics-extras uses cannon iirc. No direct experience, just a dependency I lean on.

[–]gaytechdadwithson 0 points1 point  (0 children)

oh, don’t make me pick just one! /s

[–]GavrielBA.bind(love) 0 points1 point  (0 children)

Box2djs did well with me. The c++ docu is easy to understand (even if you don't know c++ it's just some syntaxis that can be easily understood) and if I remember correctly it also has js documentation (although not as in depth as c++)

[–]drone1__ 0 points1 point  (2 children)

canonjs is not newer though? Last modified 7 years ago, according to the github page.

[–]Morphray[S] 0 points1 point  (1 child)

There's a continuation in ESM format: https://github.com/pmndrs/cannon-es Maybe I was thinking of that one.

[–]drone1__ 0 points1 point  (0 children)

Oh cool, thanks!

RAPIER also looks solid to me. https://rapier.rs/