]> git.r.bdr.sh - rbdr/lissajous/blame - lib/systems/lissajous_position_updater.js
Add a few comments
[rbdr/lissajous] / lib / systems / lissajous_position_updater.js
CommitLineData
5f6ef99e
RBR
1import { System } from '@serpentity/serpentity';
2import LissajousCurve from '../nodes/lissajous_curve';
3
4const internals = {
362f9116 5 kAmplitude: 1,
5f6ef99e
RBR
6 kPeriod: Math.PI * 12000000
7};
8
32e2eed2
RBR
9/**
10 * Calculates the lissajous curve over time
11 * NOTE: This shouldn't store data, I should have a different entity for
12 * the actual drawable vertices. Either put the storage in the curve, or
13 * create a separate entity.
14 */
5f6ef99e
RBR
15export default class WebGLRenderer extends System {
16
17 constructor() {
18
19 super();
20 }
21
22 added(engine){
23
24 this.curves = engine.getNodes(LissajousCurve);
25 this.time = 0;
26 }
27
28 removed(){
29
362f9116
RBR
30 delete this.curves;
31 delete this.time;
5f6ef99e
RBR
32 }
33
34 update(dt){
35
6a87c253 36 this.time = (this.time + dt / 500) % internals.kPeriod;
5f6ef99e
RBR
37
38 for (const curve of this.curves) {
2b9badac
RBR
39 curve.position.x = this._getPosition(curve.amplitude.a, curve.frequency.a, this.time, curve.phase.a);
40 curve.position.y = this._getPosition(curve.amplitude.b, curve.frequency.b, this.time, curve.phase.b);
41 curve.position.z = this._getPosition(curve.amplitude.c, curve.frequency.c, this.time, curve.phase.c);
5f6ef99e 42 }
5f6ef99e
RBR
43 }
44
45 _getPosition(amplitude, frequency, time, phaseShift) {
46
47 return amplitude * Math.sin(frequency * time + phaseShift);
48 }
49};