ericman314 / physicum

Physicum is an interactive, 2D rigid-body mechanics simulator with an emphasis on accuracy.
MIT License
1 stars 1 forks source link

Choosing a Physics Engine #1

Open ericman314 opened 7 years ago

ericman314 commented 7 years ago

The goal of this project is accuracy. The result of a simulation should be able to match the analytical solution to near machine precision whenever possible. So which physics engine should we use? Can we use an existing one, or do we need to write our own? Are there any existing engines that were built to be accurate?

Are the computations done in those engines directly transferable to "real" units, like m and kg, or were they only written with pixels in mind? What about sub-timestep calculations, so we can calculate the exact moment of a collision between two bodies? How is friction calculated? How are the constraints (for example, pin joints) calculated? Can a body be given an arbitrary moment of inertia (such as solid or shell)? What kinds of approximations are made to improve performance?

Using an existing engine would greatly accelerate this project's development, and would add credibility--but the main goal is to just have fun, and building a new, hyper-accurate physics engine might open the door to academics and educators.

ericman314 commented 7 years ago

I think Matter.js is out--even though it is very popular, well documented, and actively maintained. This example illustrates that collisions do not always conserve energy. This could be due to the fact that collisions are computed on a discrete timestep, and not at the exact moment of the collision. So sometimes a collision occurs when the ball is slightly above the ground, other times right at or even slightly below the ground (maybe?). The difference means that it is probably the potential energy that is being affected rather than the kinetic energy. I can't know more without studying the code, but since the intention of Matter.js and others are to provide high performance, realistic-enough simulations, this particular problem is not such a big deal for them. Here is another reference: https://github.com/liabru/matter-js/issues/256

Box2D may be a good choice. It is actively maintained and has high reputability. It even employs a time-of-impact calculation. Some JavaScript derivatives include kripken/box2d.js, Box2DJS, Box2Dweb, and Planck.js.

p2.js is another option but I haven't researched it much.

It's hard to balance my desire to get a working example quickly and my interest in writing an engine from scratch! At least I know if we do write an engine from scratch, there will be plenty of code examples in these other libraries to help us when we run into problems.