Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions cocos/physics-2d/box2d-jsb/physics-contact-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright (c) 2024 Xiamen Yaji Software Co., Ltd.

https://www.cocos.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { PhysicsContact, b2ContactExtends } from './physics-contact';

const contactMap = new Map<b2ContactExtends, PhysicsContact>();
const pools: PhysicsContact[] = [];

export class PhysicsContactManager {
static get (b2contact: b2ContactExtends): PhysicsContact {
let c = pools.pop();
if (!c) {
c = new PhysicsContact();
}
c.init(b2contact);
contactMap.set(b2contact, c);
return c;
}

static find (b2contact: b2ContactExtends): PhysicsContact | null {
return contactMap.get(b2contact) ?? null;
}

static put (b2contact: b2ContactExtends): void {
const c = contactMap.get(b2contact);
if (!c) return;
pools.push(c);
c.reset();
contactMap.delete(b2contact);
}

static clear (): void {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the clear API called?

Copy link
Copy Markdown
Contributor Author

@tangkaikk tangkaikk Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear function left, but there is no better way to call it. because the box2d system follow the engine utils it destroy.

contactMap.clear();
pools.length = 0;
}
}
27 changes: 1 addition & 26 deletions cocos/physics-2d/box2d-jsb/physics-contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export type b2ContactExtends = b2jsb.Contact & {
m_userData: any
}

const pools: PhysicsContact[] = [];

// temp world manifold
const pointCache = [new Vec2(), new Vec2()];

Expand Down Expand Up @@ -74,25 +72,6 @@ const impulse: IPhysics2DImpulse = {
};

export class PhysicsContact implements IPhysics2DContact {
static get (b2contact: b2ContactExtends): PhysicsContact {
let c = pools.pop();

if (!c) {
c = new PhysicsContact();
}

c.init(b2contact);
return c;
}

static put (b2contact: b2ContactExtends): void {
const c: PhysicsContact = b2contact.m_userData as PhysicsContact;
if (!c) return;

pools.push(c);
c.reset();
}

colliderA: Collider2D | null = null;
colliderB: Collider2D | null = null;

Expand All @@ -108,16 +87,13 @@ export class PhysicsContact implements IPhysics2DContact {
}

init (b2contact: b2ContactExtends): void {
this._b2contact = b2contact;
this.colliderA = (b2contact.GetFixtureA().m_userData as b2Shape2D).collider;
this.colliderB = (b2contact.GetFixtureB().m_userData as b2Shape2D).collider;
this.disabled = false;
this.disabledOnce = false;
this._impulse = null!;

this._inverted = false;

this._b2contact = b2contact;
b2contact.m_userData = this;
}

reset (): void {
Expand All @@ -130,7 +106,6 @@ export class PhysicsContact implements IPhysics2DContact {
this.disabled = false;
this._impulse = null!;

this._b2contact.m_userData = null;
this._b2contact = null!;
}

Expand Down
11 changes: 6 additions & 5 deletions cocos/physics-2d/box2d-jsb/physics-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { PhysicsContactListener } from './platform/physics-contact-listener';
import { PhysicsAABBQueryCallback } from './platform/physics-aabb-query-callback';
import { PhysicsRayCastCallback } from './platform/physics-ray-cast-callback';
import { PhysicsContact, b2ContactExtends } from './physics-contact';
import { PhysicsContactManager } from './physics-contact-manager';
import { Contact2DType, Collider2D, RaycastResult2D } from '../framework';
import { b2Shape2D } from './shapes/shape-2d';
import { PhysicsDebugDraw } from './platform/physics-debug-draw';
Expand Down Expand Up @@ -444,22 +445,22 @@ export class b2PhysicsWorld implements IPhysicsWorld {
}

_onBeginContact (b2contact: b2ContactExtends): void {
const c = PhysicsContact.get(b2contact);
const c = PhysicsContactManager.get(b2contact);
c.emit(Contact2DType.BEGIN_CONTACT);
}

_onEndContact (b2contact: b2ContactExtends): void {
const c = b2contact.m_userData as PhysicsContact;
const c = PhysicsContactManager.find(b2contact);
if (!c) {
return;
}
c.emit(Contact2DType.END_CONTACT);

PhysicsContact.put(b2contact);
PhysicsContactManager.put(b2contact);
Copy link
Copy Markdown
Contributor

@bofeng-song bofeng-song Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When to clear b2contact?

}

_onPreSolve (b2contact: b2ContactExtends): void {
const c = b2contact.m_userData as PhysicsContact;
const c = PhysicsContactManager.find(b2contact);
if (!c) {
return;
}
Expand All @@ -468,7 +469,7 @@ export class b2PhysicsWorld implements IPhysicsWorld {
}

_onPostSolve (b2contact: b2ContactExtends, impulse: b2jsb.ContactImpulse): void {
const c: PhysicsContact = b2contact.m_userData as PhysicsContact;
const c = PhysicsContactManager.find(b2contact);
if (!c) {
return;
}
Expand Down
Loading