diff --git a/cocos/physics-2d/box2d/platform/physics-contact-listener.ts b/cocos/physics-2d/box2d/platform/physics-contact-listener.ts index 4c64093095..55a6f57348 100644 --- a/cocos/physics-2d/box2d/platform/physics-contact-listener.ts +++ b/cocos/physics-2d/box2d/platform/physics-contact-listener.ts @@ -29,6 +29,14 @@ import { Contact2DType, PhysicsSystem2D } from '../../framework'; import { PhysicsContact } from '../physics-contact'; import { b2Shape2D } from '../shapes/shape-2d'; +/** + * @en + * The contact listener responsible for all contact events between colliders. + * @zh + * 负责处理所有碰撞体之间的触发事件。 + * @class PhysicsContactListener + * @extends b2.ContactListener + */ export class PhysicsContactListener extends b2.ContactListener { static readonly _contactMap = new Map(); @@ -87,17 +95,26 @@ export class PhysicsContactListener extends b2.ContactListener { public finalizeContactEvent () { PhysicsContactListener._contactMap.forEach((contact: PhysicsContact, key: string) => { //console.log('forEach', key, collision); + + // emit collision event + if (!contact.disabled || contact.status === Contact2DType.BEGIN_CONTACT) { //BEGIN_CONTACT always emits + if (contact.status === Contact2DType.END_CONTACT) { + //console.log(' report end collision', key, 'current ref is:', contact.ref); + this.emit(Contact2DType.END_CONTACT, contact); + } else if (contact.status === Contact2DType.BEGIN_CONTACT) { + //console.log(' report enter collision', key, 'current ref is:', contact.ref); + this.emit(Contact2DType.BEGIN_CONTACT, contact); + } else if (contact.status === Contact2DType.STAY_CONTACT) { + //console.log(' report stay collision', key, 'current ref is:', contact.ref); + this.emit(Contact2DType.STAY_CONTACT, contact); + } + } + + // extra processing if (contact.status === Contact2DType.END_CONTACT) { PhysicsContactListener._contactMap.delete(key); - //console.log(' report end collision', key, 'current ref is:', contact.ref); - this.emit(Contact2DType.END_CONTACT, contact); } else if (contact.status === Contact2DType.BEGIN_CONTACT) { contact.status = Contact2DType.STAY_CONTACT; - //console.log(' report enter collision', key, 'current ref is:', contact.ref); - this.emit(Contact2DType.BEGIN_CONTACT, contact); - } else if (contact.status === Contact2DType.STAY_CONTACT) { - //console.log(' report stay collision', key, 'current ref is:', contact.ref); - this.emit(Contact2DType.STAY_CONTACT, contact); } }); } @@ -130,10 +147,5 @@ export class PhysicsContactListener extends b2.ContactListener { if ((bodyA && bodyA.enabledContactListener) || (bodyB && bodyB.enabledContactListener) || !bodyA || !bodyB) { PhysicsSystem2D.instance.emit(contactType, colliderA, colliderB, contact); } - - if (contact.disabled || contact.disabledOnce) { - contact.setEnabled(false); - contact.disabledOnce = false; - } } } diff --git a/cocos/physics-2d/framework/physics-types.ts b/cocos/physics-2d/framework/physics-types.ts index 426d121565..f73a0884da 100644 --- a/cocos/physics-2d/framework/physics-types.ts +++ b/cocos/physics-2d/framework/physics-types.ts @@ -127,10 +127,43 @@ export enum ERaycast2DType { All } +/** + * @en + * Physics2D contact event types. + * @zh + * 2D物理接触事件类型。 + */ export const Contact2DType = { + /** + * @en + * The event type of non contact. + * @zh + * 无接触的事件类型。 + */ None: 'none-contact', + + /** + * @en + * The event type of the contact start. + * @zh + * 开始接触的事件类型。 + */ BEGIN_CONTACT: 'begin-contact', + + /** + * @en + * The event type of the contact stay. + * @zh + * 保持接触的事件类型。 + */ STAY_CONTACT: 'stay-contact', + + /** + * @en + * The event type of the contact end. + * @zh + * 结束接触的事件类型。 + */ END_CONTACT: 'end-contact', /** diff --git a/cocos/physics-2d/spec/i-physics-contact.ts b/cocos/physics-2d/spec/i-physics-contact.ts index e661ff180f..220ed46962 100644 --- a/cocos/physics-2d/spec/i-physics-contact.ts +++ b/cocos/physics-2d/spec/i-physics-contact.ts @@ -204,17 +204,17 @@ export interface IPhysics2DContact { /** * @en * If set disabled to true, the contact will be ignored until contact end. - * If you just want to disabled contact for current time step or sub-step, please use disabledOnce. * @zh * 如果 disabled 被设置为 true,那么直到接触结束此接触都将被忽略。 - * 如果只是希望在当前时间步或子步中忽略此接触,请使用 disabledOnce 。 */ disabled: boolean; + /** * @en * Disabled contact for current time step or sub-step. * @zh * 在当前时间步或子步中忽略此接触。 + * @deprecated Since v3.8.0, disabledOnce is no longer supported. */ disabledOnce: boolean;