@thi.ng/timestep 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/LICENSE +201 -0
- package/README.md +182 -0
- package/api.d.ts +91 -0
- package/api.js +1 -0
- package/index.d.ts +4 -0
- package/index.js +3 -0
- package/package.json +92 -0
- package/state.d.ts +43 -0
- package/state.js +52 -0
- package/timestep.d.ts +30 -0
- package/timestep.js +56 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
- **Last updated**: 2023-04-19T09:28:07Z
|
|
4
|
+
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
|
+
|
|
6
|
+
All notable changes to this project will be documented in this file.
|
|
7
|
+
See [Conventional Commits](https://conventionalcommits.org/) for commit guidelines.
|
|
8
|
+
|
|
9
|
+
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
|
+
and/or version bumps of transitive dependencies.
|
|
11
|
+
|
|
12
|
+
## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/timestep@0.1.0) (2023-04-19)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- import as new package ([1398282](https://github.com/thi-ng/umbrella/commit/1398282))
|
|
17
|
+
- add start time and time scale opts ([1cdc3d3](https://github.com/thi-ng/umbrella/commit/1cdc3d3))
|
|
18
|
+
- update IUpdatable/AState to receive context arg ([1249283](https://github.com/thi-ng/umbrella/commit/1249283))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
<!-- This file is generated - DO NOT EDIT! -->
|
|
2
|
+
|
|
3
|
+
# 
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@thi.ng/timestep)
|
|
6
|
+

|
|
7
|
+
[](https://mastodon.thi.ng/@toxi)
|
|
8
|
+
|
|
9
|
+
This project is part of the
|
|
10
|
+
[@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo.
|
|
11
|
+
|
|
12
|
+
- [About](#about)
|
|
13
|
+
- [Managing fixed timestep updates](#managing-fixed-timestep-updates)
|
|
14
|
+
- [Status](#status)
|
|
15
|
+
- [Installation](#installation)
|
|
16
|
+
- [Dependencies](#dependencies)
|
|
17
|
+
- [API](#api)
|
|
18
|
+
- [Authors](#authors)
|
|
19
|
+
- [License](#license)
|
|
20
|
+
|
|
21
|
+
## About
|
|
22
|
+
|
|
23
|
+
Deterministic fixed timestep simulation updates with state interpolation.
|
|
24
|
+
|
|
25
|
+
For a more thorough description of both the problem and solution(s), please see
|
|
26
|
+
[Glenn Fiedler's article](https://www.gafferongames.com/post/fix_your_timestep/)
|
|
27
|
+
upon which this implementation is principally based on.
|
|
28
|
+
|
|
29
|
+
### Managing fixed timestep updates
|
|
30
|
+
|
|
31
|
+
The package provides a configurable [`TimeStep`
|
|
32
|
+
class](https://docs.thi.ng/umbrella/timestep/classes/TimeStep.html) to manage
|
|
33
|
+
the update logic. To participate in these managed updates, the user must provide
|
|
34
|
+
state values/wrappers implementing the [`IUpdatable`
|
|
35
|
+
interface](https://docs.thi.ng/umbrella/timestep/interfaces/IUpdatable.html) for
|
|
36
|
+
the two main phases of the frame update:
|
|
37
|
+
|
|
38
|
+
- `integrate(dt: number, ctx: ReadonlyTimeStep): void`: Depending on actual
|
|
39
|
+
elapsed time and the configured timestep, this function might be called
|
|
40
|
+
several times per update cycle to update the sim to its next desired state
|
|
41
|
+
to:
|
|
42
|
+
1. Backup the current state of the sim (at the beginning of that function
|
|
43
|
+
call). This backup is required for the second phase of the update.
|
|
44
|
+
2. Update the state to the desired next state using the provided timestep
|
|
45
|
+
`dt` (`ctx` is only given for information).
|
|
46
|
+
- `interpolate(alpha: number, ctx: ReadonlyTimeStep): void`: In this phase a
|
|
47
|
+
previously backed up state should be linearly interpolated to the current
|
|
48
|
+
state using the provided `alpha` tween factor and the result stored separately
|
|
49
|
+
such that it can be used for subsequent rendering/display purposes.
|
|
50
|
+
|
|
51
|
+
In other words, this means any updatable state value will require 3 versions:
|
|
52
|
+
previous, current (next), interpolated. **Only the interpolated version is to be
|
|
53
|
+
used for rendering (or other outside purposes).** For that reason, the package
|
|
54
|
+
also provides wrappers for
|
|
55
|
+
[numeric](https://docs.thi.ng/umbrella/timestep/functions/defNumeric.html) and
|
|
56
|
+
[vector-based](https://docs.thi.ng/umbrella/timestep/functions/defVector.html)
|
|
57
|
+
state variables, illustrated in the following short example:
|
|
58
|
+
|
|
59
|
+
```ts tangle:export/readme.ts
|
|
60
|
+
import { defTimeStep, defNumeric, defVector } from "@thi.ng/timestep";
|
|
61
|
+
|
|
62
|
+
// initialize with default options (i.e. dt = 1/60 = 60 fps)
|
|
63
|
+
// start time is given in milliseconds but will be converted to seconds
|
|
64
|
+
// see: https://docs.thi.ng/umbrella/timestep/interfaces/TimestepOpts.html
|
|
65
|
+
const sim = defTimeStep({ dt: 1 / 60, startTime: Date.now() });
|
|
66
|
+
|
|
67
|
+
// define numeric state variable, increase using @ 10 units per second
|
|
68
|
+
const a = defNumeric(0, (x, dt) => x + dt * 10);
|
|
69
|
+
|
|
70
|
+
// define vector state variable, update using velocity of [-10, 20] (per second)
|
|
71
|
+
// also see thi.ng/vectors for hundreds of useful vector operations to simplify...
|
|
72
|
+
const b = defVector([0, 0], (x, dt) => [x[0] - 10 * dt, x[1] + 20 * dt]);
|
|
73
|
+
|
|
74
|
+
// even though the sim will update at a fixed (theoretical) 60 fps,
|
|
75
|
+
// the simulated render frame rate here is only 25 fps...
|
|
76
|
+
setInterval(() => {
|
|
77
|
+
// provide current time and an array of state values to update
|
|
78
|
+
// (any IUpdatable impl can be given here, incl. custom types)
|
|
79
|
+
sim.update(Date.now(), [a, b]);
|
|
80
|
+
// show current frame, num updates, time (relative to start) and interpolated state values
|
|
81
|
+
console.log(sim.frame, sim.updates, sim.current, a.value, b.value);
|
|
82
|
+
}, 1000 / 25);
|
|
83
|
+
|
|
84
|
+
// 1 2 0.042 0.253 [ -0.253, 0.506 ]
|
|
85
|
+
// 2 4 0.082 0.663 [ -0.663, 1.326 ]
|
|
86
|
+
// 3 7 0.124 1.073 [ -1.073, 2.146 ]
|
|
87
|
+
// 4 9 0.164 1.483 [ -1.483, 2.966 ]
|
|
88
|
+
// 5 12 0.206 1.893 [ -1.893, 3.786 ]
|
|
89
|
+
// 6 14 0.246 2.293 [ -2.293, 4.586 ]
|
|
90
|
+
// 7 17 0.288 2.713 [ -2.713, 5.426 ]
|
|
91
|
+
// 8 19 0.328 3.123 [ -3.123, 6.246 ]
|
|
92
|
+
// 9 22 0.371 3.543 [ -3.543, 7.086 ]
|
|
93
|
+
// 10 24 0.411 3.953 [ -3.953, 7.906 ]
|
|
94
|
+
// 11 27 0.452 4.353 [ -4.353, 8.706 ]
|
|
95
|
+
// 12 29 0.493 4.773 [ -4.773, 9.546 ]
|
|
96
|
+
// 13 32 0.534 5.173 [ -5.173, 10.346 ]
|
|
97
|
+
// 14 34 0.575 5.593 [ -5.593, 11.186 ]
|
|
98
|
+
// 15 37 0.617 6.003 [ -6.003, 12.006 ]
|
|
99
|
+
// 16 39 0.659 6.423 [ -6.423, 12.846 ]
|
|
100
|
+
// 17 42 0.700 6.833 [ -6.833, 13.666 ]
|
|
101
|
+
// 18 44 0.740 7.233 [ -7.233, 14.466 ]
|
|
102
|
+
// 19 46 0.781 7.643 [ -7.643, 15.286 ]
|
|
103
|
+
// 20 49 0.822 8.053 [ -8.053, 16.106 ]
|
|
104
|
+
// 21 51 0.861 8.453 [ -8.453, 16.906 ]
|
|
105
|
+
// 22 54 0.904 8.873 [ -8.873, 17.746 ]
|
|
106
|
+
// 23 56 0.944 9.283 [ -9.283, 18.566 ]
|
|
107
|
+
// 24 59 0.986 9.703 [ -9.703, 19.406 ]
|
|
108
|
+
// 25 61 1.028 10.113 [ -10.113, 20.226 ]
|
|
109
|
+
// ...
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Since the 25 fps render framerate is not a multiple of the 60 fps used by the
|
|
113
|
+
sim, the output shows that for each render frame (1st column), there're either 2
|
|
114
|
+
or 3 sim updates performed (2nd column) to accommodate the 60 fps.
|
|
115
|
+
|
|
116
|
+
This should then also clarify _why_ state interpolation is the final step of
|
|
117
|
+
each update cycle: When multiple updates are performed in a single frame, the
|
|
118
|
+
sim's state is technically _ahead_ of the render time line and therefore we need
|
|
119
|
+
to compute (interpolate) the correct state between previous and current, then
|
|
120
|
+
use these interpolated values for rendering (in the abstract sense)...
|
|
121
|
+
|
|
122
|
+
The last row also shows that both `a` and `b` arrive at their expected values
|
|
123
|
+
after 1 second (25 frames)...
|
|
124
|
+
|
|
125
|
+
## Status
|
|
126
|
+
|
|
127
|
+
**ALPHA** - bleeding edge / work-in-progress
|
|
128
|
+
|
|
129
|
+
[Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Btimestep%5D+in%3Atitle)
|
|
130
|
+
|
|
131
|
+
## Installation
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
yarn add @thi.ng/timestep
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
ES module import:
|
|
138
|
+
|
|
139
|
+
```html
|
|
140
|
+
<script type="module" src="https://cdn.skypack.dev/@thi.ng/timestep"></script>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
[Skypack documentation](https://docs.skypack.dev/)
|
|
144
|
+
|
|
145
|
+
For Node.js REPL:
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
const timestep = await import("@thi.ng/timestep");
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 518 bytes
|
|
152
|
+
|
|
153
|
+
## Dependencies
|
|
154
|
+
|
|
155
|
+
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
156
|
+
- [@thi.ng/math](https://github.com/thi-ng/umbrella/tree/develop/packages/math)
|
|
157
|
+
- [@thi.ng/vectors](https://github.com/thi-ng/umbrella/tree/develop/packages/vectors)
|
|
158
|
+
|
|
159
|
+
## API
|
|
160
|
+
|
|
161
|
+
[Generated API docs](https://docs.thi.ng/umbrella/timestep/)
|
|
162
|
+
|
|
163
|
+
TODO
|
|
164
|
+
|
|
165
|
+
## Authors
|
|
166
|
+
|
|
167
|
+
- [Karsten Schmidt](https://thi.ng)
|
|
168
|
+
|
|
169
|
+
If this project contributes to an academic publication, please cite it as:
|
|
170
|
+
|
|
171
|
+
```bibtex
|
|
172
|
+
@misc{thing-timestep,
|
|
173
|
+
title = "@thi.ng/timestep",
|
|
174
|
+
author = "Karsten Schmidt",
|
|
175
|
+
note = "https://thi.ng/timestep",
|
|
176
|
+
year = 2023
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## License
|
|
181
|
+
|
|
182
|
+
© 2023 Karsten Schmidt // Apache License 2.0
|
package/api.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for participating in the {@link TimeStep.update} logic.
|
|
3
|
+
*/
|
|
4
|
+
export interface IUpdatable {
|
|
5
|
+
/**
|
|
6
|
+
* 1st phase of the update cycle and main update function for this
|
|
7
|
+
* simulation state. Receives timestep (in seconds) and a `ctx` object.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* Implementations must perform the following tasks:
|
|
11
|
+
*
|
|
12
|
+
* - backup current state (e.g copy to a `previous` field/var)
|
|
13
|
+
* - compute new state based on current state (using `dt`)
|
|
14
|
+
* - set current state to newly computed state
|
|
15
|
+
*
|
|
16
|
+
* The `ctx` object is only provided for information. Most update logic will
|
|
17
|
+
* (and should) only require the timestep arg `dt`.
|
|
18
|
+
*
|
|
19
|
+
* @param dt
|
|
20
|
+
* @param ctx
|
|
21
|
+
*/
|
|
22
|
+
integrate(dt: number, ctx: ReadonlyTimeStep): void;
|
|
23
|
+
/**
|
|
24
|
+
* Final stage of the update cycle. The `alpha` arg is the tween factor to
|
|
25
|
+
* use for interpolating between the backed up `previous` state and the
|
|
26
|
+
* current state.
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* The aforementioned `previous` / `current` state values are never to be
|
|
30
|
+
* used directly. Only the interpolated value(s) computed by this function
|
|
31
|
+
* should be used for rendering (or other external) purposes.
|
|
32
|
+
*
|
|
33
|
+
* @param alpha
|
|
34
|
+
* @param ctx
|
|
35
|
+
*/
|
|
36
|
+
interpolate(alpha: number, ctx: ReadonlyTimeStep): void;
|
|
37
|
+
}
|
|
38
|
+
export interface ReadonlyTimeStep {
|
|
39
|
+
/**
|
|
40
|
+
* Configured timestep (in seconds)
|
|
41
|
+
*/
|
|
42
|
+
readonly dt: number;
|
|
43
|
+
/**
|
|
44
|
+
* Current time relative to configured start time (in seconds)
|
|
45
|
+
*/
|
|
46
|
+
readonly current: number;
|
|
47
|
+
/**
|
|
48
|
+
* Start time (in seconds)
|
|
49
|
+
*/
|
|
50
|
+
readonly start: number;
|
|
51
|
+
/**
|
|
52
|
+
* Current render frame
|
|
53
|
+
*/
|
|
54
|
+
readonly frame: number;
|
|
55
|
+
/**
|
|
56
|
+
* Number of sim updates performed in total so far
|
|
57
|
+
*/
|
|
58
|
+
readonly updates: number;
|
|
59
|
+
}
|
|
60
|
+
export interface TimeStepOpts {
|
|
61
|
+
/**
|
|
62
|
+
* Timestep (in seconds)
|
|
63
|
+
*
|
|
64
|
+
* @defaultValue 1/60
|
|
65
|
+
*/
|
|
66
|
+
dt: number;
|
|
67
|
+
/**
|
|
68
|
+
* Start time (see {@link TimeStepOpts.scale}).
|
|
69
|
+
*
|
|
70
|
+
* @defaultValue 0
|
|
71
|
+
*/
|
|
72
|
+
startTime: number;
|
|
73
|
+
/**
|
|
74
|
+
* Max. frame duration (in seconds)
|
|
75
|
+
*
|
|
76
|
+
* @defaultValue 1/4
|
|
77
|
+
*/
|
|
78
|
+
maxFrameTime: number;
|
|
79
|
+
/**
|
|
80
|
+
* Scale factor used to convert timestamp values to seconds, i.e. those
|
|
81
|
+
* timestamps passed via {@link TimeStepOpts.startTime} and those as
|
|
82
|
+
* argument given to {@link TimeStep.update}. The default assumes these
|
|
83
|
+
* value are in milliseconds.
|
|
84
|
+
*
|
|
85
|
+
* @defaultValue 0.001
|
|
86
|
+
*/
|
|
87
|
+
scale: number;
|
|
88
|
+
}
|
|
89
|
+
export type StateUpdate<T> = (curr: T, dt: number, ctx: ReadonlyTimeStep) => T;
|
|
90
|
+
export type StateInterpolation<T> = (prev: T, curr: T, alpha: number, ctx: ReadonlyTimeStep) => T;
|
|
91
|
+
//# sourceMappingURL=api.d.ts.map
|
package/api.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thi.ng/timestep",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Deterministic fixed timestep simulation updates with state interpolation",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "./index.js",
|
|
7
|
+
"typings": "./index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/thi-ng/umbrella/tree/develop/packages/timestep#readme",
|
|
14
|
+
"funding": [
|
|
15
|
+
{
|
|
16
|
+
"type": "github",
|
|
17
|
+
"url": "https://github.com/sponsors/postspectacular"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"type": "patreon",
|
|
21
|
+
"url": "https://patreon.com/thing_umbrella"
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "yarn clean && tsc --declaration",
|
|
28
|
+
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
|
+
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
|
+
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
31
|
+
"doc:readme": "yarn doc:stats && tools:readme",
|
|
32
|
+
"doc:stats": "tools:module-stats",
|
|
33
|
+
"pub": "yarn npm publish --access public",
|
|
34
|
+
"test": "testament test"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@thi.ng/api": "^8.8.0",
|
|
38
|
+
"@thi.ng/math": "^5.4.8",
|
|
39
|
+
"@thi.ng/vectors": "^7.6.12"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@microsoft/api-extractor": "^7.34.4",
|
|
43
|
+
"@thi.ng/testament": "^0.3.15",
|
|
44
|
+
"rimraf": "^4.4.1",
|
|
45
|
+
"tools": "^0.0.1",
|
|
46
|
+
"typedoc": "^0.23.28",
|
|
47
|
+
"typescript": "^5.0.4"
|
|
48
|
+
},
|
|
49
|
+
"keywords": [
|
|
50
|
+
"deterministic",
|
|
51
|
+
"fractional",
|
|
52
|
+
"interpolation",
|
|
53
|
+
"physics",
|
|
54
|
+
"simulation",
|
|
55
|
+
"time",
|
|
56
|
+
"typescript",
|
|
57
|
+
"vector"
|
|
58
|
+
],
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
},
|
|
62
|
+
"browser": {
|
|
63
|
+
"process": false,
|
|
64
|
+
"setTimeout": false
|
|
65
|
+
},
|
|
66
|
+
"engines": {
|
|
67
|
+
"node": ">=14"
|
|
68
|
+
},
|
|
69
|
+
"files": [
|
|
70
|
+
"./*.js",
|
|
71
|
+
"./*.d.ts"
|
|
72
|
+
],
|
|
73
|
+
"exports": {
|
|
74
|
+
".": {
|
|
75
|
+
"default": "./index.js"
|
|
76
|
+
},
|
|
77
|
+
"./api": {
|
|
78
|
+
"default": "./api.js"
|
|
79
|
+
},
|
|
80
|
+
"./state": {
|
|
81
|
+
"default": "./state.js"
|
|
82
|
+
},
|
|
83
|
+
"./timestep": {
|
|
84
|
+
"default": "./timestep.js"
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"thi.ng": {
|
|
88
|
+
"status": "alpha",
|
|
89
|
+
"year": 2023
|
|
90
|
+
},
|
|
91
|
+
"gitHead": "3a56bc490f1e68754762a503d06327b5b34ff7eb\n"
|
|
92
|
+
}
|
package/state.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { IDeref } from "@thi.ng/api";
|
|
2
|
+
import type { ReadonlyVec } from "@thi.ng/vectors";
|
|
3
|
+
import type { IUpdatable, ReadonlyTimeStep, StateInterpolation, StateUpdate } from "./api.js";
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base class for simulation state wrappers.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* See {@link defNumeric} and {@link defVector}.
|
|
9
|
+
*/
|
|
10
|
+
export declare abstract class AState<T> implements IDeref<T>, IUpdatable {
|
|
11
|
+
value: T;
|
|
12
|
+
update: StateUpdate<T>;
|
|
13
|
+
mix: StateInterpolation<T>;
|
|
14
|
+
protected prev: T;
|
|
15
|
+
protected curr: T;
|
|
16
|
+
constructor(value: T, update: StateUpdate<T>, mix: StateInterpolation<T>);
|
|
17
|
+
deref(): T;
|
|
18
|
+
integrate(dt: number, ctx: ReadonlyTimeStep): void;
|
|
19
|
+
interpolate(alpha: number, ctx: ReadonlyTimeStep): void;
|
|
20
|
+
}
|
|
21
|
+
export declare class NumericState extends AState<number> {
|
|
22
|
+
constructor(x: number, update: StateUpdate<number>);
|
|
23
|
+
}
|
|
24
|
+
export declare class VectorState extends AState<ReadonlyVec> {
|
|
25
|
+
constructor(x: ReadonlyVec, update: StateUpdate<ReadonlyVec>);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns a new {@link NumericState} wrapper for given value `x` and its update
|
|
29
|
+
* function for use with {@link TimeStep.update}.
|
|
30
|
+
*
|
|
31
|
+
* @param x
|
|
32
|
+
* @param update
|
|
33
|
+
*/
|
|
34
|
+
export declare const defNumeric: (x: number, update: StateUpdate<number>) => NumericState;
|
|
35
|
+
/**
|
|
36
|
+
* Returns a new {@link VectorState} wrapper for given vector `v` and its update
|
|
37
|
+
* function for use with {@link TimeStep.update}.
|
|
38
|
+
*
|
|
39
|
+
* @param x
|
|
40
|
+
* @param update
|
|
41
|
+
*/
|
|
42
|
+
export declare const defVector: (v: ReadonlyVec, update: StateUpdate<ReadonlyVec>) => VectorState;
|
|
43
|
+
//# sourceMappingURL=state.d.ts.map
|
package/state.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { mix } from "@thi.ng/math/mix";
|
|
2
|
+
import { mixN } from "@thi.ng/vectors/mixn";
|
|
3
|
+
/**
|
|
4
|
+
* Abstract base class for simulation state wrappers.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* See {@link defNumeric} and {@link defVector}.
|
|
8
|
+
*/
|
|
9
|
+
export class AState {
|
|
10
|
+
constructor(value, update, mix) {
|
|
11
|
+
this.value = value;
|
|
12
|
+
this.update = update;
|
|
13
|
+
this.mix = mix;
|
|
14
|
+
this.prev = this.curr = value;
|
|
15
|
+
}
|
|
16
|
+
deref() {
|
|
17
|
+
return this.value;
|
|
18
|
+
}
|
|
19
|
+
integrate(dt, ctx) {
|
|
20
|
+
this.prev = this.curr;
|
|
21
|
+
this.curr = this.update(this.curr, dt, ctx);
|
|
22
|
+
}
|
|
23
|
+
interpolate(alpha, ctx) {
|
|
24
|
+
this.value = this.mix(this.prev, this.curr, alpha, ctx);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class NumericState extends AState {
|
|
28
|
+
constructor(x, update) {
|
|
29
|
+
super(x, update, mix);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class VectorState extends AState {
|
|
33
|
+
constructor(x, update) {
|
|
34
|
+
super(x, update, (a, b, t) => mixN([], a, b, t));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns a new {@link NumericState} wrapper for given value `x` and its update
|
|
39
|
+
* function for use with {@link TimeStep.update}.
|
|
40
|
+
*
|
|
41
|
+
* @param x
|
|
42
|
+
* @param update
|
|
43
|
+
*/
|
|
44
|
+
export const defNumeric = (x, update) => new NumericState(x, update);
|
|
45
|
+
/**
|
|
46
|
+
* Returns a new {@link VectorState} wrapper for given vector `v` and its update
|
|
47
|
+
* function for use with {@link TimeStep.update}.
|
|
48
|
+
*
|
|
49
|
+
* @param x
|
|
50
|
+
* @param update
|
|
51
|
+
*/
|
|
52
|
+
export const defVector = (v, update) => new VectorState(v, update);
|
package/timestep.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { IUpdatable, TimeStepOpts } from "./api.js";
|
|
2
|
+
export declare class TimeStep {
|
|
3
|
+
current: number;
|
|
4
|
+
start: number;
|
|
5
|
+
dt: number;
|
|
6
|
+
maxFrameTime: number;
|
|
7
|
+
scale: number;
|
|
8
|
+
accumulator: number;
|
|
9
|
+
frame: number;
|
|
10
|
+
updates: number;
|
|
11
|
+
constructor(opts?: Partial<TimeStepOpts>);
|
|
12
|
+
/**
|
|
13
|
+
* Updates internal time to given new time `now` (in seconds) and performs
|
|
14
|
+
* the required number of fixed timesteps to integrate and interpolate the
|
|
15
|
+
* given `state` values.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* If `interpolate` is false, the {@link IUpdatable.interpolate} phase of
|
|
19
|
+
* the update cycle is skipped. This is useful when using this setup to
|
|
20
|
+
* simulate sub-steps (e.g. in XPBD) and only requiring the interpolation
|
|
21
|
+
* stage for the last step.
|
|
22
|
+
*
|
|
23
|
+
* @param now
|
|
24
|
+
* @param items
|
|
25
|
+
* @param interpolate
|
|
26
|
+
*/
|
|
27
|
+
update(now: number, items: IUpdatable[], interpolate?: boolean): void;
|
|
28
|
+
}
|
|
29
|
+
export declare const defTimeStep: (opts?: Partial<TimeStepOpts>) => TimeStep;
|
|
30
|
+
//# sourceMappingURL=timestep.d.ts.map
|
package/timestep.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export class TimeStep {
|
|
2
|
+
constructor(opts) {
|
|
3
|
+
this.current = 0;
|
|
4
|
+
this.accumulator = 0;
|
|
5
|
+
this.frame = 0;
|
|
6
|
+
this.updates = 0;
|
|
7
|
+
const $opts = {
|
|
8
|
+
dt: 1 / 60,
|
|
9
|
+
maxFrameTime: 1 / 4,
|
|
10
|
+
startTime: 0,
|
|
11
|
+
scale: 1e-3,
|
|
12
|
+
...opts,
|
|
13
|
+
};
|
|
14
|
+
this.dt = $opts.dt;
|
|
15
|
+
this.maxFrameTime = $opts.maxFrameTime;
|
|
16
|
+
this.scale = $opts.scale;
|
|
17
|
+
this.start = $opts.startTime * this.scale;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Updates internal time to given new time `now` (in seconds) and performs
|
|
21
|
+
* the required number of fixed timesteps to integrate and interpolate the
|
|
22
|
+
* given `state` values.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* If `interpolate` is false, the {@link IUpdatable.interpolate} phase of
|
|
26
|
+
* the update cycle is skipped. This is useful when using this setup to
|
|
27
|
+
* simulate sub-steps (e.g. in XPBD) and only requiring the interpolation
|
|
28
|
+
* stage for the last step.
|
|
29
|
+
*
|
|
30
|
+
* @param now
|
|
31
|
+
* @param items
|
|
32
|
+
* @param interpolate
|
|
33
|
+
*/
|
|
34
|
+
update(now, items, interpolate = true) {
|
|
35
|
+
now = now * this.scale - this.start;
|
|
36
|
+
if (this.current < 0)
|
|
37
|
+
this.current = now;
|
|
38
|
+
this.accumulator += Math.min(now - this.current, this.maxFrameTime);
|
|
39
|
+
this.current = now;
|
|
40
|
+
const n = items.length;
|
|
41
|
+
const dt = this.dt;
|
|
42
|
+
while (this.accumulator >= dt) {
|
|
43
|
+
for (let i = 0; i < n; i++)
|
|
44
|
+
items[i].integrate(dt, this);
|
|
45
|
+
this.accumulator -= dt;
|
|
46
|
+
this.updates++;
|
|
47
|
+
}
|
|
48
|
+
if (interpolate) {
|
|
49
|
+
const alpha = this.accumulator / dt;
|
|
50
|
+
for (let i = 0; i < n; i++)
|
|
51
|
+
items[i].interpolate(alpha, this);
|
|
52
|
+
}
|
|
53
|
+
this.frame++;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export const defTimeStep = (opts) => new TimeStep(opts);
|