aboutsummaryrefslogtreecommitdiff
path: root/lib/systems/lissajous_position_updater.js
blob: 030d9bab44be5391cc9060a384fe70e9728500e3 (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
45
46
47
48
49
import { System } from '@serpentity/serpentity';
import LissajousCurve from '../nodes/lissajous_curve';

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

/**
  * Calculates the lissajous curve over time
  * NOTE: This shouldn't store data, I should have a different entity for
  * the actual drawable vertices. Either put the storage in the curve, or
  * create a separate entity.
  */
export default class WebGLRenderer extends System {

  constructor() {

    super();
  }

  added(engine){

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

  removed(){

    delete this.curves;
    delete this.time;
  }

  update(dt){

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

    for (const curve of this.curves) {
      curve.position.x = this._getPosition(curve.amplitude.a, curve.frequency.a, this.time, curve.phase.a);
      curve.position.y = this._getPosition(curve.amplitude.b, curve.frequency.b, this.time, curve.phase.b);
      curve.position.z = this._getPosition(curve.amplitude.c, curve.frequency.c, this.time, curve.phase.c);
    }
  }

  _getPosition(amplitude, frequency, time, phaseShift) {

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