Skip to content

Commit 0686c4a

Browse files
tangkaikkstar-e
authored andcommitted
the logic is consistent with box2d-jsb & box2d (#93)
* the logic is consistent with box2d-jsb & box2d * Add return type to register/unregisterContactFixture, fix lint
1 parent 31ef98a commit 0686c4a

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

cocos/physics-2d/box2d-wasm/physics-world.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ export class B2PhysicsWorld implements IPhysicsWorld {
8282
PhysicsContactListener._PreSolve = this._onPreSolve;
8383
PhysicsContactListener._PostSolve = this._onPostSolve;
8484
this._contactListener = B2.ContactListener.implement(PhysicsContactListener.callback);
85+
const orgRegisterContactFixture = this._contactListener.registerContactFixture.bind(this._contactListener);
86+
this._contactListener.registerContactFixture = (contactFixture: number): void => {
87+
PhysicsContactListener.registerContactFixture(contactFixture);
88+
orgRegisterContactFixture(contactFixture);
89+
};
90+
const orgUnregisterContactFixture = this._contactListener.unregisterContactFixture.bind(this._contactListener);
91+
this._contactListener.unregisterContactFixture = (contactFixture: number): void => {
92+
PhysicsContactListener.unregisterContactFixture(contactFixture);
93+
orgUnregisterContactFixture(contactFixture);
94+
};
8595
this._world.SetContactListener(this._contactListener);
8696

8797
this._aabbQueryCallback = B2.QueryCallback.implement(PhysicsAABBQueryCallback.callback);

cocos/physics-2d/box2d-wasm/platform/physics-contact-listener.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,56 @@
2222
THE SOFTWARE.
2323
*/
2424

25+
import { B2 } from '../instantiated';
26+
2527
type BeginContactCallback = (contact: number) => void;
2628
type EndContactCallback = (contact: number) => void;
2729
type PreSolveCallback = (contact: number, oldManifold: number) => void;
2830
type PostSolveCallback = (contact: number, impulse: number) => void;
2931

3032
export class PhysicsContactListener {
33+
static _contactFixtures: Set<number> = new Set<number>();
3134
static _BeginContact: BeginContactCallback | null = null;
3235
static _EndContact: EndContactCallback | null = null;
3336
static _PreSolve: PreSolveCallback | null = null;
3437
static _PostSolve: PostSolveCallback | null = null;
38+
static _shouldReportContacts: Set<number> = new Set<number>();
3539

3640
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);
3849
this._BeginContact(contact);
3950
}
4051
}
4152

4253
static EndContact (contact: number): void {
43-
if (this._EndContact) {
54+
if (this._EndContact && this._shouldReportContacts.has(contact)) {
55+
this._shouldReportContacts.delete(contact);
4456
this._EndContact(contact);
4557
}
4658
}
4759

4860
static PreSolve (contact: number, oldManifold: number): void {
49-
if (this._PreSolve) {
61+
if (this._PreSolve && this._shouldReportContacts.has(contact)) {
5062
this._PreSolve(contact, oldManifold);
5163
}
5264
}
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+
}
5372

5473
static PostSolve (contact: number, impulse: number): void {
55-
if (this._PostSolve) {
74+
if (this._PostSolve && this._shouldReportContacts.has(contact)) {
5675
this._PostSolve(contact, impulse);
5776
}
5877
}

0 commit comments

Comments
 (0)