Page 1 of 5

[beh] Box2d+ (2.3.0 emscripten) [update: 2017-11-18]

PostPosted: Mon Sep 11, 2017 9:36 pm
by Colludium


This is an ongoing project to port the emscripten/asm.js version of Box2d v2.3.0 to Construct 2. The performance is amazing - more stable than Chipmunk and much more stable than the standard asm.js physics. Plus there are some useful game-dev friendly features. It is easy for a beginner to use but powerful enough to keep the more experienced happy. At least, that's the plan. Of note, it doesn't play nicely with the standard physics plugin and will cause your browser to vanish into a singularity if both are used in the same project (the emscripten libraries interfere at runtime).

There main demo has 4 parts:

1. The start screen gives you an idea of performance and demonstrates one-event conveyor belts with kinematic bodies. Collision points and polygons can be selected to be drawn and objects can be thrown around.
2. Bitmap / scan image. This is a demo with 2 objects - one fixed and one that will be re-spawned; when running, both types can be dragged. You can use the default images or upload your own into the demo. When you press Start the engine will scan the images and assign collision polygons from the image alpha.


3. Stress test. A comparison with Chipmunk (which, from my previous testing, is the best non-emscripten physics library in JavaScript) and the standard asm.js physics behavior. The test creates a pyramid of boxes that you can interact with or just watch collapse....

4. Draw objects. Draw some physics shapes, convex or concave, and watch them fall down. The shape vertices are stored in a c2 array which is loaded into the Box2D+ plugin to form the collision polygon.


Here's the demo (v1.0.1.8): link

Summary of features:

Plugin features already completed:
Scan image to create polygon from its alpha (uses adjusted versions of hull.js and simplify.js)
Circle, polygon and box shapes
Speed clamp
Rotational speed clamp
Collision point
Dynamic, Static and Kinematic bodies
Collision filtering (category and mask bits)
Object gravity scale
World x and y gravity values
Load collision hull from c2 json array
Get collision hull as c2 json array (create polygons outside your game and upload from string)
Raycast world queries
Assimilate Joint - demo: link
All joints are demo'd here: link
AABB world queries (find objects in a defined box area)
Gravity attractor
Polygon editor capx (demo: link)
Platformer behavior capx. Demo (with one-way doors/floors and wall-climb): link
More joint helper expressions added
Obtain collision impulse and collision impulse angle*
Obtain pre-collision and post-collision velocity (x and y values)*
Collision Pre-Solve trigger and an action to disable the collision.*

Features to do:
A platform behavior helper plugin, so you won't need to mess with complicated events to control the movement and doors etc.
The test is just about finished....!
Ideas and suggestions are welcome.

Not included / what's been excluded:
Mouse joint (no need, better done using events and hidden object).
Friction joint.
Rope joint. The box2d rope joint stretches and breaks far too easily to be useful. Work-around: use revolute joints.

*latest changes

Re: [beh] Box2d+ (2.3.0 emscripten)

PostPosted: Tue Sep 12, 2017 8:31 am
by radbrothers
–°ool, it is better than asm.js physics! I'll follow your work!

Re: [beh] Box2d+ (2.3.0 emscripten)

PostPosted: Tue Sep 12, 2017 7:40 pm
by Colludium
Thanks @radbrothers!

Update to the WIP plugin and the demo: Now added raycast. Left click/drag to draw a raycast line.

Re: [beh] Box2d+ (2.3.0 emscripten)

PostPosted: Sat Sep 16, 2017 4:32 am
by justifun
Awesome work! look forward to updates

Re: [beh] Box2d+ (2.3.0 emscripten)

PostPosted: Sat Sep 16, 2017 5:11 pm
by Colludium
justifun wrote:Awesome work! look forward to updates

Thank you :)

Here you go: I've managed to get some joints to work!! Just some tweaking now, then I'll create a joint demo.

Joints now working:
Revolute / Limited Revolute / Revolute motor / Gear on revolute

Joints not planned for inclusion:
Mouse joint (no need, better done using events and hidden object)
Friction joint

Re: [beh] Box2d+ (2.3.0 emscripten) [update: 2017-09-16]

PostPosted: Tue Sep 19, 2017 3:58 am
by gamecorpstudio
this looks very promising, tell me you have some alpha/beta for people to use!

Re: [beh] Box2d+ (2.3.0 emscripten) [update: 2017-09-16]

PostPosted: Tue Sep 19, 2017 6:19 pm
by Colludium
gamecorpstudio wrote:this looks very promising, tell me you have some alpha/beta for people to use!

Thanks! Not yet - I'm still trying to add features. But when I do I will announce it.

New feature added - a c2 bespoke "Assimilate" joint. The weld joint suffers from the same problem that all physics library 'fixed' types of joints suffer from - because the engine iterates over each object in turn, the joints aren't truly solid because each object exerts slightly incorrect forces onto the others they are joined to. And lots of joints between lots of objects can cause joint fighting (it's spectacular, but frustrating).

The cure: object assimilation. In this Box2d+ plugin you can assimilate an object as the child of another (in box2d language, you turn it into a fixture) - and back again, of course. Here's an example:

*The cpu should read ~12%. I've been mixing up my c2 with my javascript...

Try it out for yourselves: link

Left mouse to drag, right click anywhere to toggle assimilate joints to join all of the icon squares to the round object.

Re: [beh] Box2d+ (2.3.0 emscripten) [update: 2017-09-16]

PostPosted: Tue Sep 19, 2017 6:24 pm
by gamecorpstudio
Colludium wrote:
Try it out for yourselves: link

Amazing, im interested in the collisions only which is much faster and correct than i ever experienced with C2.

Hope you can finish this! :D Keep at it! il buy it even if its premium released!

The cpu utilisation is a bit high for 200 objects (maybe because is still a WIP? or that isn't the cpu in real time? i see 1% being stuck there.) usually those objects use around 0.02% on my pc.

Re: [beh] Box2d+ (2.3.0 emscripten) [update: 2017-09-19]

PostPosted: Tue Sep 19, 2017 6:25 pm
by Colludium
@gamecorpstudio - Thank you!! :)

Re: [beh] Box2d+ (2.3.0 emscripten) [update: 2017-09-19]

PostPosted: Fri Sep 22, 2017 10:46 am
by Colludium
Alrighty... Putting together the demos highlighted a limitation to how events work for this plugin. For example, on a collision trigger, if you want to use an Action on the object that was hit (not the one with the trigger condition) then you have to sort through the other box2d objects and identify the one associated with the uid obtained from the collision. You can get around this to some extent by using families, but that method can get complicated when filtering.

So I'm going to make all Actions in this plugin 'global'. There will be a uid field that'll be auto-populated with Self.uid but you will be able to over type it if you wish to pick another object instead.

If you think this is a crazy idea, please let me know now before I change too much! Thanks :)