Jump to content

Javascript/Typescript Physics Engines


cmann
 Share

Recommended Posts

What physics engines are available for Javascript and/or Typescript? I've searched myself but maybe there's one or two I missed.
I also found a post in these forums but that's over two years old now; It's a little disappointing to see that some of them aren't actively developed and lacking a lot of features.

I put together a test to compare the different engines I tried - I've attached the files if anyone wants to try it out.

Here are the engines I've tried:

  • PhysicsJs http://wellcaffeinated.net/PhysicsJS/
    - Incomplete: lacking simple features like constraints
    - Seems like it was last updated 2 years ago
    - Possibly the worst performance
    - Strange API (in my opinion) and difficult to use
    - Built in mouse interaction behaves strangely
  • MatterJs http://brm.io/matter-js/
    - Also incomplete and lacking constraints
    - Has a built in mouse constraint but it acts strangely
    - Updated recently
    - Simple and easy to use
    - Doesn't handle stress test well
  • p2.js http://schteppe.github.io/p2.js/
    - Complete
    - Updated within the last few months
    - Doesn't handle stress test well
    - Easy to use
    - Good documentation
  • Box2DWeb https://github.com/hecht-software/box2dweb
    - Complete - everything you would expect from Box2D
    - Last updated one or two years ago
    - Semi-poor documentation - you have use the box2d flash documentation which is fairly thorough but slightly outdated
    - Much faster and a lot more accurate than the above three libraries.
  • Nape http://napephys.com/
    - Complete - lots of features - but isn't very active
    - Has a great marching squares utility which can convert bitmaps into polygons
    - Good site and documentation
    - Easy to use but the pooling get/dispose stuff can be a bit confusing
    - Similar (or maybe even slight better) performance to Box2DWeb and possibly more accurate
    - Large - minified it's still 1.7MB

The one problem with Nape though is that it isn't really meant for Javascript and I had a lot of trouble getting it set up in Typescript.
There were some project to convert it to js and generate definition files but I had to modify them a lot to get something complete that worked for me. I also had to manually convert the debug draw classes from as3 to js and Easeljs.
Luckily that only needs to be done once - after that I didn't have any problems using it though.

I also tried PhysicsType2D (http://physicstype2d.com/) but couldn't get it to work.

Overall I was a bit disappointed; In terms of performance, the 'native' js libraries fell short by a lot compared to the two ported ones, and on top of that they lacked a lot of important features.
Even the ported ones didn't perform as well as their Flash/As3 versions - the Nape stress test (you can find it here) which I copied, runs significantly better in flash.

PhysicsLib.zip

UPDATE:

You can find the updated github repo here: https://github.com/cmann1/Javascript-Physics-Library-Tests/

Link to comment
Share on other sites

2 hours ago, Fatalist said:

Thanks for this, I've searched for js physics engine benchmarks but could not find anything.

You should make a github repo so people can add additional tests and engines.

No problem.

I've never used github or anything similar before but I might just try that.
I may also add a few more tests and make it easier to create tests without having to hand write code for each engine.

Link to comment
Share on other sites

A small update: I've added a rag doll demo.

As expected Nape and Box2D handle this without a problem. It was also simple to do in p2.js but it falls behind a bit in performance with a large number of rag dolls.

I could hack something together that's close to a rag doll in MatterJs, but neither that nor PhysicsJs really has the required features to make it work properly.

Link to comment
Share on other sites

  • 7 months later...

Hi, @cmann

Great job. I'm using the Nape typescript port, but I'm running into problems:

this.space.listeners.add(new InteractionListener(CbEvent.BEGIN, InteractionType.ANY, CbType.ANY_BODY, CbType.ANY_BODY, this.onInteractionBegin));

all the constants (CbEvent.BEGIN, InteractionType.ANY, CbType.ANY_BODY, CbType.ANY_BODY) are null, any idea why this happens?

EDIT: This is strange, some other constants like BodyType.STATIC are fine, even though they seem to be defined in the same way in nape.max.js ...

Link to comment
Share on other sites

Honestly I haven't looked at this in a while, but I'll try to help.

If you use the following for example, what output do you get?

console.log(nape.callbacks.CbEvent.BEGIN);
console.log(nape.callbacks.CbType.ANY_BODY);
console.log(nape.phys.BodyType.DYNAMIC);

 

Link to comment
Share on other sites

18 minutes ago, cmann said:

Honestly I haven't looked at this in a while, but I'll try to help.

If you use the following for example, what output do you get?


console.log(nape.callbacks.CbEvent.BEGIN);
console.log(nape.callbacks.CbType.ANY_BODY);
console.log(nape.phys.BodyType.DYNAMIC);

 

Cheers for the help, @cmann

I get null, null and BodyType object:

2017-07-06_0029.png

Link to comment
Share on other sites

Ok so I think I found the problem.

Some of those constants have get_XXX function, then use defineProperty to link that function to the property name, eg. for BodyType.STATIC

// There's a function like this
BodyType.get_STATIC = function() {
	...
};

// Then lower down there's this:
Object.defineProperty(BodyType, "STATIC", {get: BodyType.get_STATIC});

 

Now for some reason (I'm have no idea how) some of those defineProperty calls were removed or possibly never added.
The thing is that to get nape working in js was a lot of work - I had to convert it from haxe to js, then had to do a lot of processing to get that working so it's possible I screwed something up at some point without realising it.

I quick fix could be to directly use the get_ functions instead, eg.

// Instead of this
CbEvent.BEGIN

// Use this
CbEvent.get_BEGIN()

 

Link to comment
Share on other sites

@ozdy

I've updated nape.max.js in github
I had scripts that generated the nape js files, but can't remember how they work (it's a bit of a mess), so I just manually copy/pasted the necessary defineProperty lines from another copy I had

Hopefully this should fix the problem for you.

Link to comment
Share on other sites

14 hours ago, cmann said:

@ozdy

I've updated nape.max.js in github
I had scripts that generated the nape js files, but can't remember how they work (it's a bit of a mess), so I just manually copy/pasted the necessary defineProperty lines from another copy I had

Hopefully this should fix the problem for you.

Thank you for your work, I won't be able to test in a couple of days but will let you know!

Link to comment
Share on other sites

  • 2 years later...
  • 9 months later...
  • 9 months later...

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...