aboutsummaryrefslogtreecommitdiff
path: root/lib/systems/lissajous_position_updater.js
blob: 0f5792dffd26dcae307cb48d0b9e6d4ecd8c3689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { System } from '@serpentity/serpentity';
import LissajousCurve from '../nodes/lissajous_curve';

const internals = {
  kAmplitude: 0.8,
  kPeriod: Math.PI * 12000000
};

export default class WebGLRenderer extends System {

  constructor() {

    super();
  }

  added(engine){

    this.curves = engine.getNodes(LissajousCurve);
    this.time = 0;
  }

  removed(){

    this.curves = undefined;
    this.time = undefined;
  }

  update(dt){

    this.time = (this.time + dt / 100) % internals.kPeriod;

    for (const curve of this.curves) {
      curve.position.x = this._getPosition(internals.kAmplitude, curve.frequency.a, this.time, 0);
      curve.position.y = this._getPosition(internals.kAmplitude, curve.frequency.b, this.time, 0);
      curve.position.z = this._getPosition(internals.kAmplitude, curve.frequency.c, this.time, 0);
    }
    console.log('UP');
  }

  _getPosition(amplitude, frequency, time, phaseShift) {

    return amplitude * Math.sin(frequency * time + phaseShift);
  }
};