]>
git.r.bdr.sh - rbdr/sumo/blob - lib/systems/elastic.js
1 import { System
} from '@serpentity/serpentity';
4 kTightStiffness: 0.001,
5 kBaseStiffness: 0.0008,
6 kLooseStiffness: 0.0000001
9 import ElasticNode
from '../nodes/elastic';
12 Changes the stiffness on the node when it's less extended
14 * @extends {external:Serpentity.System}
15 * @class ElasticSystem
16 * @param {object} config a configuration object to extend.
18 export default class ElasticSystem
extends System
{
20 constructor(config
= {}) {
25 * The node collection of entities that have external force
27 * @property {external:Serpentity.NodeCollection} elastics
29 * @memberof ElasticSystem
35 * Initializes system when added. Requests elastic nodes
38 * @memberof ElasticSystem
40 * @param {external:Serpentity.Engine} engine the serpentity engine to
41 * which we are getting added
45 this.elastics
= engine
.getNodes(ElasticNode
);
49 * Clears system resources when removed.
53 * @memberof ElasticSystem
61 * Runs on every update of the loop. Checks length of elastic and adjusts
66 * @param {Number} currentFrameDuration the duration of the current
68 * @memberof ElasticSystem
70 update(currentFrameDuration
) {
72 for (const elastic
of this.elastics
) {
73 const constraint
= elastic
.body
.body
;
75 const currentDistance
= Math
.abs(
77 Math
.pow(constraint
.bodyA
.position
.x
- constraint
.bodyB
.position
.x
, 2) +
78 Math
.pow(constraint
.bodyA
.position
.y
- constraint
.bodyB
.position
.y
, 2)));
80 if (currentDistance
<= constraint
.length
) {
81 constraint
.stiffness
= internals
.kLooseStiffness
;
85 if (currentDistance
>= 2.6 * constraint
.length
) {
86 constraint
.stiffness
= internals
.kTightStiffness
;
90 constraint
.stiffness
= internals
.kBaseStiffness
;