@keyframe-engine/physics 1.0.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/LICENSE +21 -0
- package/dist/RealTimeSpring.d.ts +36 -0
- package/dist/RealTimeSpring.js +69 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/js/physics/RealTimeSpring.d.ts +36 -0
- package/dist/js/physics/RealTimeSpring.js +69 -0
- package/dist/packages/physics/src/RealTimeSpring.d.ts +1 -0
- package/dist/packages/physics/src/RealTimeSpring.js +1 -0
- package/dist/packages/physics/src/index.d.ts +1 -0
- package/dist/packages/physics/src/index.js +1 -0
- package/package.json +18 -0
- package/src/RealTimeSpring.d.ts +36 -0
- package/src/RealTimeSpring.js +69 -0
- package/src/RealTimeSpring.ts +94 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 InitialXKO
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface RealTimeSpringConfig {
|
|
2
|
+
mass?: number;
|
|
3
|
+
damping?: number;
|
|
4
|
+
stiffness?: number;
|
|
5
|
+
initialValue?: number;
|
|
6
|
+
initialVelocity?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class RealTimeSpring {
|
|
9
|
+
private static activeInstanceCount;
|
|
10
|
+
private mass;
|
|
11
|
+
private damping;
|
|
12
|
+
private stiffness;
|
|
13
|
+
private value;
|
|
14
|
+
private velocity;
|
|
15
|
+
constructor(config?: RealTimeSpringConfig);
|
|
16
|
+
static getActiveInstanceCount(): number;
|
|
17
|
+
static resetInstanceCount(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Advances the spring simulation state towards target by deltaTime.
|
|
20
|
+
* Automatically handles deltaTime passed in seconds (e.g. 0.016) or milliseconds (e.g. 16.66).
|
|
21
|
+
* @returns current value after step
|
|
22
|
+
*/
|
|
23
|
+
step(target: number, deltaTime: number): number;
|
|
24
|
+
/**
|
|
25
|
+
* Resets spring position and velocity state.
|
|
26
|
+
*/
|
|
27
|
+
reset(value?: number, velocity?: number): void;
|
|
28
|
+
/**
|
|
29
|
+
* Returns current spring velocity.
|
|
30
|
+
*/
|
|
31
|
+
getVelocity(): number;
|
|
32
|
+
/**
|
|
33
|
+
* Returns current spring value.
|
|
34
|
+
*/
|
|
35
|
+
getValue(): number;
|
|
36
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export class RealTimeSpring {
|
|
2
|
+
static activeInstanceCount = 0;
|
|
3
|
+
mass;
|
|
4
|
+
damping;
|
|
5
|
+
stiffness;
|
|
6
|
+
value;
|
|
7
|
+
velocity;
|
|
8
|
+
constructor(config = {}) {
|
|
9
|
+
this.mass = config.mass ?? 1.0;
|
|
10
|
+
this.damping = config.damping ?? 10;
|
|
11
|
+
this.stiffness = config.stiffness ?? 100;
|
|
12
|
+
this.value = config.initialValue ?? 0;
|
|
13
|
+
this.velocity = config.initialVelocity ?? 0;
|
|
14
|
+
RealTimeSpring.activeInstanceCount++;
|
|
15
|
+
if (RealTimeSpring.activeInstanceCount > 200) {
|
|
16
|
+
console.warn("[@keyframe-engine/physics] Large number of live springs (>200). Consider using baked physics or reducing update frequency.");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
static getActiveInstanceCount() {
|
|
20
|
+
return RealTimeSpring.activeInstanceCount;
|
|
21
|
+
}
|
|
22
|
+
static resetInstanceCount() {
|
|
23
|
+
RealTimeSpring.activeInstanceCount = 0;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Advances the spring simulation state towards target by deltaTime.
|
|
27
|
+
* Automatically handles deltaTime passed in seconds (e.g. 0.016) or milliseconds (e.g. 16.66).
|
|
28
|
+
* @returns current value after step
|
|
29
|
+
*/
|
|
30
|
+
step(target, deltaTime) {
|
|
31
|
+
if (deltaTime <= 0) {
|
|
32
|
+
return this.value;
|
|
33
|
+
}
|
|
34
|
+
// Convert milliseconds to seconds if deltaTime is > 1.0
|
|
35
|
+
let dt = deltaTime > 1.0 ? deltaTime / 1000 : deltaTime;
|
|
36
|
+
// Sub-stepping for numerical stability (max sub-step = 1ms = 0.001s)
|
|
37
|
+
const subStep = 0.001;
|
|
38
|
+
while (dt > 0) {
|
|
39
|
+
const currentDt = Math.min(dt, subStep);
|
|
40
|
+
// Spring force equation: F = -stiffness * (x - target) - damping * v
|
|
41
|
+
const force = -this.stiffness * (this.value - target) - this.damping * this.velocity;
|
|
42
|
+
const accel = force / (this.mass || 1.0);
|
|
43
|
+
// Semi-implicit Euler step
|
|
44
|
+
this.velocity += accel * currentDt;
|
|
45
|
+
this.value += this.velocity * currentDt;
|
|
46
|
+
dt -= currentDt;
|
|
47
|
+
}
|
|
48
|
+
return this.value;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Resets spring position and velocity state.
|
|
52
|
+
*/
|
|
53
|
+
reset(value, velocity) {
|
|
54
|
+
this.value = value ?? 0;
|
|
55
|
+
this.velocity = velocity ?? 0;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns current spring velocity.
|
|
59
|
+
*/
|
|
60
|
+
getVelocity() {
|
|
61
|
+
return this.velocity;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns current spring value.
|
|
65
|
+
*/
|
|
66
|
+
getValue() {
|
|
67
|
+
return this.value;
|
|
68
|
+
}
|
|
69
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './RealTimeSpring.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './RealTimeSpring.js';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface RealTimeSpringConfig {
|
|
2
|
+
mass?: number;
|
|
3
|
+
damping?: number;
|
|
4
|
+
stiffness?: number;
|
|
5
|
+
initialValue?: number;
|
|
6
|
+
initialVelocity?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class RealTimeSpring {
|
|
9
|
+
private static activeInstanceCount;
|
|
10
|
+
private mass;
|
|
11
|
+
private damping;
|
|
12
|
+
private stiffness;
|
|
13
|
+
private value;
|
|
14
|
+
private velocity;
|
|
15
|
+
constructor(config?: RealTimeSpringConfig);
|
|
16
|
+
static getActiveInstanceCount(): number;
|
|
17
|
+
static resetInstanceCount(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Advances the spring simulation state towards target by deltaTime.
|
|
20
|
+
* Automatically handles deltaTime passed in seconds (e.g. 0.016) or milliseconds (e.g. 16.66).
|
|
21
|
+
* @returns current value after step
|
|
22
|
+
*/
|
|
23
|
+
step(target: number, deltaTime: number): number;
|
|
24
|
+
/**
|
|
25
|
+
* Resets spring position and velocity state.
|
|
26
|
+
*/
|
|
27
|
+
reset(value?: number, velocity?: number): void;
|
|
28
|
+
/**
|
|
29
|
+
* Returns current spring velocity.
|
|
30
|
+
*/
|
|
31
|
+
getVelocity(): number;
|
|
32
|
+
/**
|
|
33
|
+
* Returns current spring value.
|
|
34
|
+
*/
|
|
35
|
+
getValue(): number;
|
|
36
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export class RealTimeSpring {
|
|
2
|
+
static activeInstanceCount = 0;
|
|
3
|
+
mass;
|
|
4
|
+
damping;
|
|
5
|
+
stiffness;
|
|
6
|
+
value;
|
|
7
|
+
velocity;
|
|
8
|
+
constructor(config = {}) {
|
|
9
|
+
this.mass = config.mass ?? 1.0;
|
|
10
|
+
this.damping = config.damping ?? 10;
|
|
11
|
+
this.stiffness = config.stiffness ?? 100;
|
|
12
|
+
this.value = config.initialValue ?? 0;
|
|
13
|
+
this.velocity = config.initialVelocity ?? 0;
|
|
14
|
+
RealTimeSpring.activeInstanceCount++;
|
|
15
|
+
if (RealTimeSpring.activeInstanceCount > 200) {
|
|
16
|
+
console.warn("[@keyframe-engine/physics] Large number of live springs (>200). Consider using baked physics or reducing update frequency.");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
static getActiveInstanceCount() {
|
|
20
|
+
return RealTimeSpring.activeInstanceCount;
|
|
21
|
+
}
|
|
22
|
+
static resetInstanceCount() {
|
|
23
|
+
RealTimeSpring.activeInstanceCount = 0;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Advances the spring simulation state towards target by deltaTime.
|
|
27
|
+
* Automatically handles deltaTime passed in seconds (e.g. 0.016) or milliseconds (e.g. 16.66).
|
|
28
|
+
* @returns current value after step
|
|
29
|
+
*/
|
|
30
|
+
step(target, deltaTime) {
|
|
31
|
+
if (deltaTime <= 0) {
|
|
32
|
+
return this.value;
|
|
33
|
+
}
|
|
34
|
+
// Convert milliseconds to seconds if deltaTime is > 1.0
|
|
35
|
+
let dt = deltaTime > 1.0 ? deltaTime / 1000 : deltaTime;
|
|
36
|
+
// Sub-stepping for numerical stability (max sub-step = 1ms = 0.001s)
|
|
37
|
+
const subStep = 0.001;
|
|
38
|
+
while (dt > 0) {
|
|
39
|
+
const currentDt = Math.min(dt, subStep);
|
|
40
|
+
// Spring force equation: F = -stiffness * (x - target) - damping * v
|
|
41
|
+
const force = -this.stiffness * (this.value - target) - this.damping * this.velocity;
|
|
42
|
+
const accel = force / (this.mass || 1.0);
|
|
43
|
+
// Semi-implicit Euler step
|
|
44
|
+
this.velocity += accel * currentDt;
|
|
45
|
+
this.value += this.velocity * currentDt;
|
|
46
|
+
dt -= currentDt;
|
|
47
|
+
}
|
|
48
|
+
return this.value;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Resets spring position and velocity state.
|
|
52
|
+
*/
|
|
53
|
+
reset(value, velocity) {
|
|
54
|
+
this.value = value ?? 0;
|
|
55
|
+
this.velocity = velocity ?? 0;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns current spring velocity.
|
|
59
|
+
*/
|
|
60
|
+
getVelocity() {
|
|
61
|
+
return this.velocity;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns current spring value.
|
|
65
|
+
*/
|
|
66
|
+
getValue() {
|
|
67
|
+
return this.value;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../../js/physics/RealTimeSpring.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../../js/physics/RealTimeSpring.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../../js/physics/RealTimeSpring.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../../js/physics/RealTimeSpring.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@keyframe-engine/physics",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "@keyframe-engine/physics — Real-time Physics & Spring Engine Adapter",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"@keyframe-engine/core": "1.0.0"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface RealTimeSpringConfig {
|
|
2
|
+
mass?: number;
|
|
3
|
+
damping?: number;
|
|
4
|
+
stiffness?: number;
|
|
5
|
+
initialValue?: number;
|
|
6
|
+
initialVelocity?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class RealTimeSpring {
|
|
9
|
+
private static activeInstanceCount;
|
|
10
|
+
private mass;
|
|
11
|
+
private damping;
|
|
12
|
+
private stiffness;
|
|
13
|
+
private value;
|
|
14
|
+
private velocity;
|
|
15
|
+
constructor(config?: RealTimeSpringConfig);
|
|
16
|
+
static getActiveInstanceCount(): number;
|
|
17
|
+
static resetInstanceCount(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Advances the spring simulation state towards target by deltaTime.
|
|
20
|
+
* Automatically handles deltaTime passed in seconds (e.g. 0.016) or milliseconds (e.g. 16.66).
|
|
21
|
+
* @returns current value after step
|
|
22
|
+
*/
|
|
23
|
+
step(target: number, deltaTime: number): number;
|
|
24
|
+
/**
|
|
25
|
+
* Resets spring position and velocity state.
|
|
26
|
+
*/
|
|
27
|
+
reset(value?: number, velocity?: number): void;
|
|
28
|
+
/**
|
|
29
|
+
* Returns current spring velocity.
|
|
30
|
+
*/
|
|
31
|
+
getVelocity(): number;
|
|
32
|
+
/**
|
|
33
|
+
* Returns current spring value.
|
|
34
|
+
*/
|
|
35
|
+
getValue(): number;
|
|
36
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export class RealTimeSpring {
|
|
2
|
+
static activeInstanceCount = 0;
|
|
3
|
+
mass;
|
|
4
|
+
damping;
|
|
5
|
+
stiffness;
|
|
6
|
+
value;
|
|
7
|
+
velocity;
|
|
8
|
+
constructor(config = {}) {
|
|
9
|
+
this.mass = config.mass ?? 1.0;
|
|
10
|
+
this.damping = config.damping ?? 10;
|
|
11
|
+
this.stiffness = config.stiffness ?? 100;
|
|
12
|
+
this.value = config.initialValue ?? 0;
|
|
13
|
+
this.velocity = config.initialVelocity ?? 0;
|
|
14
|
+
RealTimeSpring.activeInstanceCount++;
|
|
15
|
+
if (RealTimeSpring.activeInstanceCount > 200) {
|
|
16
|
+
console.warn("[@keyframe-engine/physics] Large number of live springs (>200). Consider using baked physics or reducing update frequency.");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
static getActiveInstanceCount() {
|
|
20
|
+
return RealTimeSpring.activeInstanceCount;
|
|
21
|
+
}
|
|
22
|
+
static resetInstanceCount() {
|
|
23
|
+
RealTimeSpring.activeInstanceCount = 0;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Advances the spring simulation state towards target by deltaTime.
|
|
27
|
+
* Automatically handles deltaTime passed in seconds (e.g. 0.016) or milliseconds (e.g. 16.66).
|
|
28
|
+
* @returns current value after step
|
|
29
|
+
*/
|
|
30
|
+
step(target, deltaTime) {
|
|
31
|
+
if (deltaTime <= 0) {
|
|
32
|
+
return this.value;
|
|
33
|
+
}
|
|
34
|
+
// Convert milliseconds to seconds if deltaTime is > 1.0
|
|
35
|
+
let dt = deltaTime > 1.0 ? deltaTime / 1000 : deltaTime;
|
|
36
|
+
// Sub-stepping for numerical stability (max sub-step = 1ms = 0.001s)
|
|
37
|
+
const subStep = 0.001;
|
|
38
|
+
while (dt > 0) {
|
|
39
|
+
const currentDt = Math.min(dt, subStep);
|
|
40
|
+
// Spring force equation: F = -stiffness * (x - target) - damping * v
|
|
41
|
+
const force = -this.stiffness * (this.value - target) - this.damping * this.velocity;
|
|
42
|
+
const accel = force / (this.mass || 1.0);
|
|
43
|
+
// Semi-implicit Euler step
|
|
44
|
+
this.velocity += accel * currentDt;
|
|
45
|
+
this.value += this.velocity * currentDt;
|
|
46
|
+
dt -= currentDt;
|
|
47
|
+
}
|
|
48
|
+
return this.value;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Resets spring position and velocity state.
|
|
52
|
+
*/
|
|
53
|
+
reset(value, velocity) {
|
|
54
|
+
this.value = value ?? 0;
|
|
55
|
+
this.velocity = velocity ?? 0;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns current spring velocity.
|
|
59
|
+
*/
|
|
60
|
+
getVelocity() {
|
|
61
|
+
return this.velocity;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns current spring value.
|
|
65
|
+
*/
|
|
66
|
+
getValue() {
|
|
67
|
+
return this.value;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
export interface RealTimeSpringConfig {
|
|
2
|
+
mass?: number; // Default 1.0
|
|
3
|
+
damping?: number; // Default 10
|
|
4
|
+
stiffness?: number; // Default 100
|
|
5
|
+
initialValue?: number; // Default 0
|
|
6
|
+
initialVelocity?: number; // Default 0
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class RealTimeSpring {
|
|
10
|
+
private static activeInstanceCount = 0;
|
|
11
|
+
|
|
12
|
+
private mass: number;
|
|
13
|
+
private damping: number;
|
|
14
|
+
private stiffness: number;
|
|
15
|
+
private value: number;
|
|
16
|
+
private velocity: number;
|
|
17
|
+
|
|
18
|
+
constructor(config: RealTimeSpringConfig = {}) {
|
|
19
|
+
this.mass = config.mass ?? 1.0;
|
|
20
|
+
this.damping = config.damping ?? 10;
|
|
21
|
+
this.stiffness = config.stiffness ?? 100;
|
|
22
|
+
this.value = config.initialValue ?? 0;
|
|
23
|
+
this.velocity = config.initialVelocity ?? 0;
|
|
24
|
+
|
|
25
|
+
RealTimeSpring.activeInstanceCount++;
|
|
26
|
+
if (RealTimeSpring.activeInstanceCount > 200) {
|
|
27
|
+
console.warn(
|
|
28
|
+
"[@keyframe-engine/physics] Large number of live springs (>200). Consider using baked physics or reducing update frequency."
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public static getActiveInstanceCount(): number {
|
|
34
|
+
return RealTimeSpring.activeInstanceCount;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public static resetInstanceCount(): void {
|
|
38
|
+
RealTimeSpring.activeInstanceCount = 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Advances the spring simulation state towards target by deltaTime.
|
|
43
|
+
* Automatically handles deltaTime passed in seconds (e.g. 0.016) or milliseconds (e.g. 16.66).
|
|
44
|
+
* @returns current value after step
|
|
45
|
+
*/
|
|
46
|
+
public step(target: number, deltaTime: number): number {
|
|
47
|
+
if (deltaTime <= 0) {
|
|
48
|
+
return this.value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Convert milliseconds to seconds if deltaTime is > 1.0
|
|
52
|
+
let dt = deltaTime > 1.0 ? deltaTime / 1000 : deltaTime;
|
|
53
|
+
|
|
54
|
+
// Sub-stepping for numerical stability (max sub-step = 1ms = 0.001s)
|
|
55
|
+
const subStep = 0.001;
|
|
56
|
+
while (dt > 0) {
|
|
57
|
+
const currentDt = Math.min(dt, subStep);
|
|
58
|
+
|
|
59
|
+
// Spring force equation: F = -stiffness * (x - target) - damping * v
|
|
60
|
+
const force = -this.stiffness * (this.value - target) - this.damping * this.velocity;
|
|
61
|
+
const accel = force / (this.mass || 1.0);
|
|
62
|
+
|
|
63
|
+
// Semi-implicit Euler step
|
|
64
|
+
this.velocity += accel * currentDt;
|
|
65
|
+
this.value += this.velocity * currentDt;
|
|
66
|
+
|
|
67
|
+
dt -= currentDt;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return this.value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Resets spring position and velocity state.
|
|
75
|
+
*/
|
|
76
|
+
public reset(value?: number, velocity?: number): void {
|
|
77
|
+
this.value = value ?? 0;
|
|
78
|
+
this.velocity = velocity ?? 0;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns current spring velocity.
|
|
83
|
+
*/
|
|
84
|
+
public getVelocity(): number {
|
|
85
|
+
return this.velocity;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns current spring value.
|
|
90
|
+
*/
|
|
91
|
+
public getValue(): number {
|
|
92
|
+
return this.value;
|
|
93
|
+
}
|
|
94
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './RealTimeSpring.js';
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true
|
|
13
|
+
},
|
|
14
|
+
"include": ["src/**/*"]
|
|
15
|
+
}
|