项目作者: vasturiano

项目描述 :
An elastic collision force type for the d3-force simulation engine
高级语言: JavaScript
项目地址: git://github.com/vasturiano/d3-force-bounce.git
创建时间: 2017-05-05T18:34:51Z
项目社区:https://github.com/vasturiano/d3-force-bounce

开源协议:MIT License

下载


d3.forceBounce

NPM package
Build Size
NPM Downloads

An elastic collision force type for the d3-force simulation engine.

This force is similar to d3.forceCollide, but allows for fully elastic collisions that conserve kinetic energy. This makes d3.forceCollide appropriate for preventing the overlap of nodes, and d3.forceBounce better suited when the intent is to achieve an elastic collision effect, with varying degrees of elasticity (coefficient of restitution).

Here’s a @vasturiano/collision-forces-comparison">visual comparison between the two forces.

It can be used, for example to @vasturiano/entropy">simulate particle collisions or in a @vasturiano/newtons-cradle">Newton’s cradle.

See also d3.forceSurface.

Quick start

  1. import d3ForceBounce from 'd3-force-bounce';

or using a script tag

  1. <script src="//cdn.jsdelivr.net/npm/d3-force-bounce"></script>

then

  1. d3.forceSimulation()
  2. .nodes(<myNodes>)
  3. .force('bounce', d3.forceBounce()
  4. .radius(5)
  5. );

API reference

Method Description Default
elasticity([number]) Getter/setter for every collision’s coefficient of restitution, aka elasticity. A value of 1 represents a purely elastic collision with no energy loss, while a 0 will fully eliminate the bounce in the collision direction. Values >1 can be used to introduce acceleration at each collision. Values <0 are not recommended. 1
radius([num or fn]) Getter/setter for the node object radius accessor function (fn(node)) or a constant (num) for all nodes. 1
mass([num or fn]) Getter/setter for the node object mass accessor function (fn(node)) or a constant (num) for all nodes. Mass affects the symmetry of the energy transfer between two colliding nodes. By default it is proportional to the node’s area. Math.pow(radius(node), 2)
onImpact([fn]) Callback function triggered at every collision, with the signature onImpact(node1, node2)

❤️ Support This Project

If you find this module useful and would like to support its development, you can buy me a ☕. Your contributions help keep open-source sustainable!
paypal