JavaScript Physics engine with "angular stiffness" at joints

AI Thread Summary
The discussion focuses on the need for a JavaScript physics engine that supports angular stiffness at joints, specifically the ability to assign a reference angle to segments for restoring torque. The user is transitioning from verlet.js to a more versatile engine and is seeking recent comparisons of open-source engines like Box2D and Matter.js, which dominate the 2D physics space. While Box2D had a feature request for angular constraints, it has not been fully implemented, and workarounds exist. The user notes issues with verlet.js, particularly its failure to account for angular momentum and moment of inertia. Suggestions for posting implementation questions lean towards Box2D or Matter.js due to their more active development and community support.
Swamp Thing
Insights Author
Messages
1,028
Reaction score
763
I have been experimenting with a minimal "toy" physics engine for JavaScript, called verlet.js but now I want to switch to a more versatile and feature rich engine.

One feature I would like to use is the ability to assign a reference angle to a pair of segments (i.e. links, i.e. distance constraints), so that there will be a proportional restoring torque that will try to rotate the links back to the reference angle whenever they are bent away from that original angle. For example if we set 180 degrees for each joint in a chain, we get a cantilever that resists being bent away from a straight line. In the engine referred to above, this is done by specifying three particles (a, b, c), and the angle between ab and bc, plus a stiffness parameter. You do this for each set of three successive particles forming the cantilever.

Which JS engines support this feature or an equivalent feature?

More generally, I couldn't find any recent comparison between different open source JS physics engines -- the ones I did find are several years old. Is there a recent comparative review of JS physics engines, e.g. box2d, matter.js etc?
 
Last edited:
Technology news on Phys.org
Swamp Thing said:
Which JS engines support this feature or an equivalent feature?
Not sure but I'd start looking at Phaser or Impact.

Swamp Thing said:
Is there a recent comparative review of JS physics engines, e.g. box2d, matter.js etc?
I don't know of one but you can find a list of open source ones at https://github.com/collections/javascript-game-engines
 
pbuk said:
Not sure but I'd start looking at Phaser or Impact...
...you can find a list of open source (engines) at https://github.com/collections/javascript-game-engines
Those are mostly game engines. Many of them use Box2d or Matter.JS for their physics simulations.
The latter two seem to dominate the 2D physics arena in JS at present.

Swamp Thing said:
One feature I would like to use is the ability to assign a reference angle to a pair of segments (i.e. links, i.e. distance constraints), so that there will be a proportional restoring torque that will try to rotate the links back to the reference angle whenever they are bent away from that original angle.
Box2d had a feature request for this, but the fork that tried to implement it never made it into the mainstream. There are non-ideal workarounds that various people have suggested for Box2d and Matter.JS.

Meanwhile, the physics engine that I have been playing with (verlet.js, that I linked to in the OP) does try to provide angle constraints but there seems to be a flaw in it. When the joint is bent or extended, the system acquires some angular momentum.



This is not surprising, because it just rotates the three points on each time step without taking into account their distances from each other, which means it is ignoring the moment of inertia of the particles around the C.G.

(Also it assigns the same mass to all particles in the simulation).

Code:
AngleConstraint.prototype.relax = function(stepCoef) {
    var angle = this.b.pos.angle2(this.a.pos, this.c.pos);
    var diff = angle - this.angle;
   
    if (diff <= -Math.PI)
        diff += 2*Math.PI;
    else if (diff >= Math.PI)
        diff -= 2*Math.PI;

    diff *= stepCoef*this.stiffness;
   
    this.a.pos = this.a.pos.rotate(this.b.pos, diff);
    this.c.pos = this.c.pos.rotate(this.b.pos, -diff);
    this.b.pos = this.b.pos.rotate(this.a.pos, diff);
    this.b.pos = this.b.pos.rotate(this.c.pos, -diff);
}

If I want to post a question asking for suggestions as to how to implement this, should I put it here or in Classical Physics?
 
Swamp Thing said:
If I want to post a question asking for suggestions as to how to implement this, should I put it here or in Classical Physics?
Probably here but I suspect it is too small a niche for anywhere on PF. In any case isn't the implementation going to be more governed by the paradigms of the engine you are working within? I would have thought raising in the context of Box2D and/or Matter.js would be more fruitful. verlet.js appears to be dead, I wouldn't waste your time there.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top