|
22 | 22 | THE SOFTWARE. |
23 | 23 | */ |
24 | 24 |
|
| 25 | +import { B2 } from '../instantiated'; |
| 26 | + |
25 | 27 | type BeginContactCallback = (contact: number) => void; |
26 | 28 | type EndContactCallback = (contact: number) => void; |
27 | 29 | type PreSolveCallback = (contact: number, oldManifold: number) => void; |
28 | 30 | type PostSolveCallback = (contact: number, impulse: number) => void; |
29 | 31 |
|
30 | 32 | export class PhysicsContactListener { |
| 33 | + static _contactFixtures: Set<number> = new Set<number>(); |
31 | 34 | static _BeginContact: BeginContactCallback | null = null; |
32 | 35 | static _EndContact: EndContactCallback | null = null; |
33 | 36 | static _PreSolve: PreSolveCallback | null = null; |
34 | 37 | static _PostSolve: PostSolveCallback | null = null; |
| 38 | + static _shouldReportContacts: Set<number> = new Set<number>(); |
35 | 39 |
|
36 | 40 | static BeginContact (contact: number): void { |
37 | | - if (this._BeginContact) { |
| 41 | + if (!this._BeginContact) return; |
| 42 | + const fixtureA = B2.ContactGetFixtureA(contact) as number; |
| 43 | + const fixtureB = B2.ContactGetFixtureB(contact) as number; |
| 44 | + const fixtures = this._contactFixtures; |
| 45 | + this._shouldReportContacts.delete(contact); |
| 46 | + |
| 47 | + if (fixtures.has(fixtureA) || fixtures.has(fixtureB)) { |
| 48 | + this._shouldReportContacts.add(contact); |
38 | 49 | this._BeginContact(contact); |
39 | 50 | } |
40 | 51 | } |
41 | 52 |
|
42 | 53 | static EndContact (contact: number): void { |
43 | | - if (this._EndContact) { |
| 54 | + if (this._EndContact && this._shouldReportContacts.has(contact)) { |
| 55 | + this._shouldReportContacts.delete(contact); |
44 | 56 | this._EndContact(contact); |
45 | 57 | } |
46 | 58 | } |
47 | 59 |
|
48 | 60 | static PreSolve (contact: number, oldManifold: number): void { |
49 | | - if (this._PreSolve) { |
| 61 | + if (this._PreSolve && this._shouldReportContacts.has(contact)) { |
50 | 62 | this._PreSolve(contact, oldManifold); |
51 | 63 | } |
52 | 64 | } |
| 65 | + static registerContactFixture (fixture: number): void { |
| 66 | + this._contactFixtures.add(fixture); |
| 67 | + } |
| 68 | + |
| 69 | + static unregisterContactFixture (fixture: number): void { |
| 70 | + this._contactFixtures.delete(fixture); |
| 71 | + } |
53 | 72 |
|
54 | 73 | static PostSolve (contact: number, impulse: number): void { |
55 | | - if (this._PostSolve) { |
| 74 | + if (this._PostSolve && this._shouldReportContacts.has(contact)) { |
56 | 75 | this._PostSolve(contact, impulse); |
57 | 76 | } |
58 | 77 | } |
|
0 commit comments