Skip to content

Commit 0cb7adc

Browse files
committed
Convert ContactEquation, FrictionEquation, RotationalEquation to TS
1 parent a119e1f commit 0cb7adc

6 files changed

Lines changed: 36 additions & 32 deletions

File tree

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { World } from 'cannon-es'
1111
#### TO DO:
1212

1313
- Finish TS conversion
14+
- Only import types where possible (don't impot unused class as value)
1415
- Revisit math/Transform.ts types
1516
- Revisit material/ContactMaterial.ts constructor assertions
1617
- Remove use of defined assertion (!) where possible

src/equations/ConeEquation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Vec3 } from '../math/Vec3'
22
import { Equation } from './Equation'
3-
import { Body } from '../objects/Body'
3+
import type { Body } from '../objects/Body'
44

55
type ConeEquationOptions = {
66
maxForce?: number
Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Equation } from './Equation'
22
import { Vec3 } from '../math/Vec3'
3+
import type { Body } from '../objects/Body'
34

45
/**
56
* Contact/non-penetration constraint equation
@@ -11,35 +12,21 @@ import { Vec3 } from '../math/Vec3'
1112
* @extends Equation
1213
*/
1314
export class ContactEquation extends Equation {
14-
constructor(bodyA, bodyB, maxForce = 1e6) {
15-
super(bodyA, bodyB, 0, maxForce)
15+
restitution: number // "bounciness": u1 = -e*u0
16+
ri: Vec3 // World-oriented vector that goes from the center of bi to the contact point.
17+
rj: Vec3 // World-oriented vector that starts in body j position and goes to the contact point.
18+
ni: Vec3 // Contact normal, pointing out of body i.
1619

17-
/**
18-
* @property restitution
19-
* @type {Number}
20-
*/
21-
this.restitution = 0.0 // "bounciness": u1 = -e*u0
20+
constructor(bodyA: Body, bodyB: Body, maxForce = 1e6) {
21+
super(bodyA, bodyB, 0, maxForce)
2222

23-
/**
24-
* World-oriented vector that goes from the center of bi to the contact point.
25-
* @property {Vec3} ri
26-
*/
23+
this.restitution = 0.0
2724
this.ri = new Vec3()
28-
29-
/**
30-
* World-oriented vector that starts in body j position and goes to the contact point.
31-
* @property {Vec3} rj
32-
*/
3325
this.rj = new Vec3()
34-
35-
/**
36-
* Contact normal, pointing out of body i.
37-
* @property {Vec3} ni
38-
*/
3926
this.ni = new Vec3()
4027
}
4128

42-
computeB(h) {
29+
computeB(h: number): number {
4330
const a = this.a
4431
const b = this.b
4532
const bi = this.bi
@@ -95,7 +82,7 @@ export class ContactEquation extends Equation {
9582
* @method getImpactVelocityAlongNormal
9683
* @return {number}
9784
*/
98-
getImpactVelocityAlongNormal() {
85+
getImpactVelocityAlongNormal(): number {
9986
const vi = ContactEquation_getImpactVelocityAlongNormal_vi
10087
const vj = ContactEquation_getImpactVelocityAlongNormal_vj
10188
const xi = ContactEquation_getImpactVelocityAlongNormal_xi

src/equations/Equation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JacobianElement } from '../math/JacobianElement'
22
import { Vec3 } from '../math/Vec3'
3-
import { Body } from '../objects/Body'
3+
import type { Body } from '../objects/Body'
44

55
/**
66
* Equation base class
@@ -41,7 +41,7 @@ export class Equation {
4141
this.jacobianElementB = new JacobianElement()
4242
this.enabled = true
4343
this.multiplier = 0
44-
44+
4545
this.setSpookParams(1e7, 4, 1 / 60) // Set typical spook params
4646
}
4747

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Equation } from './Equation'
22
import { Vec3 } from '../math/Vec3'
3+
import type { Body } from '../objects/Body'
34

45
/**
56
* Constrains the slipping in a contact along a tangent
@@ -12,14 +13,18 @@ import { Vec3 } from '../math/Vec3'
1213
* @extends Equation
1314
*/
1415
export class FrictionEquation extends Equation {
15-
constructor(bodyA, bodyB, slipForce) {
16+
ri: Vec3
17+
rj: Vec3
18+
t: Vec3 // Tangent.
19+
20+
constructor(bodyA: Body, bodyB: Body, slipForce: number) {
1621
super(bodyA, bodyB, -slipForce, slipForce)
1722
this.ri = new Vec3()
1823
this.rj = new Vec3()
19-
this.t = new Vec3() // tangent
24+
this.t = new Vec3()
2025
}
2126

22-
computeB(h) {
27+
computeB(h: number): number {
2328
const a = this.a
2429
const b = this.b
2530
const bi = this.bi
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { Vec3 } from '../math/Vec3'
22
import { Equation } from './Equation'
3+
import type { Body } from '../objects/Body'
4+
5+
type RotationalEquationOptions = {
6+
maxForce?: number
7+
axisA?: Vec3
8+
axisB?: Vec3
9+
maxAngle?: number
10+
}
311

412
/**
513
* Rotational constraint. Works to keep the local vectors orthogonal to each other in world space.
@@ -14,18 +22,21 @@ import { Equation } from './Equation'
1422
* @extends Equation
1523
*/
1624
export class RotationalEquation extends Equation {
17-
constructor(bodyA, bodyB, options = {}) {
25+
axisA: Vec3
26+
axisB: Vec3
27+
maxAngle: number
28+
29+
constructor(bodyA: Body, bodyB: Body, options: RotationalEquationOptions = {}) {
1830
const maxForce = typeof options.maxForce !== 'undefined' ? options.maxForce : 1e6
1931

2032
super(bodyA, bodyB, -maxForce, maxForce)
2133

2234
this.axisA = options.axisA ? options.axisA.clone() : new Vec3(1, 0, 0)
2335
this.axisB = options.axisB ? options.axisB.clone() : new Vec3(0, 1, 0)
24-
2536
this.maxAngle = Math.PI / 2
2637
}
2738

28-
computeB(h) {
39+
computeB(h: number): number {
2940
const a = this.a
3041
const b = this.b
3142
const ni = this.axisA

0 commit comments

Comments
 (0)