Skip to content

Commit c622f66

Browse files
committed
Convert Material, ContactMaterial to TS
1 parent ff4495e commit c622f66

10 files changed

Lines changed: 73 additions & 102 deletions

File tree

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ import { World } from 'cannon-es'
1212

1313
- Finish TS conversion
1414
- Revisit math/Transform.ts types
15+
- Revisit material/ContactMaterial.ts constructor assertions
1516
- Correct & standardize JSDoc comments
16-
- Test possible performance improvements by converting matrices to Maps (instead of Arrays)
17+
- Test possible performance improvements by converting matrices to Maps (instead of Arrays)

src/collision/Broadphase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ const Broadphase_collisionPairs_relpos = new Vec3()
185185
* @param {Array} pairs1
186186
* @param {Array} pairs2
187187
*/
188-
const Broadphase_makePairsUnique_temp: { [key: string]: any } = { keys: [] }
188+
const Broadphase_makePairsUnique_temp: Record<string, any> = { keys: [] }
189189

190190
const Broadphase_makePairsUnique_p1: Body[] = []
191191
const Broadphase_makePairsUnique_p2: Body[] = []

src/collision/ObjectCollisionMatrix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Body } from '../objects/Body'
66
* @constructor
77
*/
88
export class ObjectCollisionMatrix {
9-
matrix: { [key: string]: boolean } // The matrix storage.
9+
matrix: Record<string, boolean> // The matrix storage.
1010

1111
constructor() {
1212
this.matrix = {}
Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
import { Material } from '../material/Material'
12
import { Utils } from '../utils/Utils'
23

4+
type ContactMaterialOptions = {
5+
friction?: number
6+
restitution?: number
7+
contactEquationStiffness?: number
8+
contactEquationRelaxation?: number
9+
frictionEquationStiffness?: number
10+
frictionEquationRelaxation?: number
11+
}
12+
313
/**
414
* Defines what happens when two materials meet.
515
* @class ContactMaterial
@@ -15,7 +25,18 @@ import { Utils } from '../utils/Utils'
1525
* @param {Number} [options.frictionEquationRelaxation=3]
1626
*/
1727
export class ContactMaterial {
18-
constructor(m1, m2, options) {
28+
id: number // Identifier of this material.
29+
materials: [Material, Material] // Participating materials.
30+
friction: number // Friction coefficient.
31+
restitution: number // Restitution coefficient.
32+
contactEquationStiffness: number // Stiffness of the produced contact equations.
33+
contactEquationRelaxation: number // Relaxation time of the produced contact equations.
34+
frictionEquationStiffness: number // Stiffness of the produced friction equations.
35+
frictionEquationRelaxation: number // Relaxation time of the produced friction equations
36+
37+
static idCounter: number;
38+
39+
constructor(m1: Material, m2: Material, options: ContactMaterialOptions) {
1940
options = Utils.defaults(options, {
2041
friction: 0.3,
2142
restitution: 0.3,
@@ -25,54 +46,14 @@ export class ContactMaterial {
2546
frictionEquationRelaxation: 3,
2647
})
2748

28-
/**
29-
* Identifier of this material
30-
* @property {Number} id
31-
*/
3249
this.id = ContactMaterial.idCounter++
33-
34-
/**
35-
* Participating materials
36-
* @property {Array} materials
37-
* @todo Should be .materialA and .materialB instead
38-
*/
3950
this.materials = [m1, m2]
40-
41-
/**
42-
* Friction coefficient
43-
* @property {Number} friction
44-
*/
45-
this.friction = options.friction
46-
47-
/**
48-
* Restitution coefficient
49-
* @property {Number} restitution
50-
*/
51-
this.restitution = options.restitution
52-
53-
/**
54-
* Stiffness of the produced contact equations
55-
* @property {Number} contactEquationStiffness
56-
*/
57-
this.contactEquationStiffness = options.contactEquationStiffness
58-
59-
/**
60-
* Relaxation time of the produced contact equations
61-
* @property {Number} contactEquationRelaxation
62-
*/
63-
this.contactEquationRelaxation = options.contactEquationRelaxation
64-
65-
/**
66-
* Stiffness of the produced friction equations
67-
* @property {Number} frictionEquationStiffness
68-
*/
69-
this.frictionEquationStiffness = options.frictionEquationStiffness
70-
71-
/**
72-
* Relaxation time of the produced friction equations
73-
* @property {Number} frictionEquationRelaxation
74-
*/
75-
this.frictionEquationRelaxation = options.frictionEquationRelaxation
51+
this.friction = options.friction!
52+
this.restitution = options.restitution!
53+
this.contactEquationStiffness = options.contactEquationStiffness!
54+
this.contactEquationRelaxation = options.contactEquationRelaxation!
55+
this.frictionEquationStiffness = options.frictionEquationStiffness!
56+
this.frictionEquationRelaxation = options.frictionEquationRelaxation!
7657
}
7758
}
7859

src/material/Material.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/material/Material.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
type MaterialOptions = {
2+
friction?: number
3+
restitution?: number
4+
}
5+
6+
/**
7+
* Defines a physics material.
8+
* @class Material
9+
* @constructor
10+
* @param {object} [options]
11+
* @author schteppe
12+
*/
13+
export class Material {
14+
name: string // Material name.
15+
id: number // Material id.
16+
friction: number // Friction for this material. If non-negative, it will be used instead of the friction given by ContactMaterials. If there's no matching ContactMaterial, the value from .defaultContactMaterial in the World will be used.
17+
restitution: number // Restitution for this material. If non-negative, it will be used instead of the restitution given by ContactMaterials. If there's no matching ContactMaterial, the value from .defaultContactMaterial in the World will be used.
18+
19+
static idCounter: number
20+
21+
constructor(options: MaterialOptions | string = {}) {
22+
let name = ''
23+
24+
// Backwards compatibility fix
25+
if (typeof options === 'string') {
26+
name = options
27+
options = {}
28+
}
29+
30+
this.name = name
31+
this.id = Material.idCounter++
32+
this.friction = typeof options.friction !== 'undefined' ? options.friction : -1
33+
this.restitution = typeof options.restitution !== 'undefined' ? options.restitution : -1
34+
}
35+
}
36+
37+
Material.idCounter = 0

src/shapes/Shape.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class Shape {
4040
collisionResponse: boolean // Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled.
4141
collisionFilterGroup: number
4242
collisionFilterMask: number
43-
material: Material
43+
material: Material | null
4444
body: Body | null
4545

4646
static idCounter: number

src/utils/EventTarget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @constructor
55
*/
66
export class EventTarget {
7-
private _listeners: { [key: string]: Function[] } | undefined
7+
private _listeners: Record<string, Function[]> | undefined
88

99
constructor() {}
1010

src/utils/Octree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export class Octree extends OctreeNode {
199199

200200
constructor(aabb: AABB, options: { maxDepth?: number } = {}) {
201201
super({ root: null, aabb })
202-
this.maxDepth = options.maxDepth || 8
202+
this.maxDepth = typeof options.maxDepth !== 'undefined' ? options.maxDepth : 8
203203
}
204204
}
205205

src/utils/Utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function Utils() {}
88
* @param {object} defaults An object containing default values.
99
* @return {object} The modified options object.
1010
*/
11-
Utils.defaults = (options: any = {}, defaults: { [key: string]: any }): { [key: string]: any } => {
11+
Utils.defaults = (options: Record<string, any> = {}, defaults: Record<string, any>): Record<string, any> => {
1212
for (let key in defaults) {
1313
if (!(key in options)) {
1414
options[key] = defaults[key]

0 commit comments

Comments
 (0)