From c682c7c809a025bc37da60cd6af4c04da720eabb Mon Sep 17 00:00:00 2001 From: Ling Zhan Date: Tue, 26 Jul 2022 20:18:44 +0800 Subject: [PATCH] fix bullet contact data error (#13210) (#12195) --- cocos/physics/bullet/bullet-contact-data.ts | 13 ++++++++----- cocos/physics/bullet/bullet-world.ts | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/cocos/physics/bullet/bullet-contact-data.ts b/cocos/physics/bullet/bullet-contact-data.ts index b371def21f..8e4602e117 100644 --- a/cocos/physics/bullet/bullet-contact-data.ts +++ b/cocos/physics/bullet/bullet-contact-data.ts @@ -33,12 +33,15 @@ import { bullet2CocosQuat, bullet2CocosVec3 } from './bullet-utils'; export class BulletContactData implements IContactEquation { get isBodyA (): boolean { const sb = (this.event.selfCollider.shape as BulletShape).sharedBody.body; - return sb === bt.PersistentManifold_getBody0(this.impl); + return sb === bt.PersistentManifold_getBody0(this.event.impl); } - impl: Bullet.ptr = 0; + impl: Bullet.ptr = 0; //btManifoldPoint + event: ICollisionEvent; - constructor (public event: ICollisionEvent) { } + constructor (event: ICollisionEvent) { + this.event = event; + } getLocalPointOnA (out: IVec3Like): void { if (this.impl) bullet2CocosVec3(out, bt.ManifoldPoint_get_m_localPointA(this.impl)); @@ -59,7 +62,7 @@ export class BulletContactData implements IContactEquation { getLocalNormalOnA (out: IVec3Like): void { if (this.impl) { const bt_rot = BulletCache.instance.BT_QUAT_0; - const body = bt.PersistentManifold_getBody0(this.impl); + const body = bt.PersistentManifold_getBody0(this.event.impl); const trans = bt.CollisionObject_getWorldTransform(body); bt.Transform_getRotation(trans, bt_rot); const inv_rot = CC_QUAT_0; @@ -74,7 +77,7 @@ export class BulletContactData implements IContactEquation { getLocalNormalOnB (out: IVec3Like): void { if (this.impl) { const bt_rot = BulletCache.instance.BT_QUAT_0; - const body = bt.PersistentManifold_getBody1(this.impl); + const body = bt.PersistentManifold_getBody1(this.event.impl); const trans = bt.CollisionObject_getWorldTransform(body); bt.Transform_getRotation(trans, bt_rot); const inv_rot = CC_QUAT_0; diff --git a/cocos/physics/bullet/bullet-world.ts b/cocos/physics/bullet/bullet-world.ts index fc7faff2d2..7466543c2b 100644 --- a/cocos/physics/bullet/bullet-world.ts +++ b/cocos/physics/bullet/bullet-world.ts @@ -276,7 +276,7 @@ export class BulletWorld implements IPhysicsWorld { TriggerEventObject.type = 'onTriggerEnter'; this.triggerArrayMat.set(shape0.id, shape1.id, true); } - TriggerEventObject.impl = data.impl; + TriggerEventObject.impl = data.impl; //btPersistentManifold TriggerEventObject.selfCollider = collider0; TriggerEventObject.otherCollider = collider1; collider0.emit(TriggerEventObject.type, TriggerEventObject); @@ -303,16 +303,18 @@ export class BulletWorld implements IPhysicsWorld { } for (let i = 0; i < data.contacts.length; i++) { - const cq = data.contacts[i]; + const cq = data.contacts[i]; //btManifoldPoint if (contactsPool.length > 0) { - const c = contactsPool.pop(); c!.impl = cq; + const c = contactsPool.pop(); + c!.impl = cq; //btManifoldPoint CollisionEventObject.contacts.push(c!); } else { - const c = new BulletContactData(CollisionEventObject); c.impl = cq; + const c = new BulletContactData(CollisionEventObject); + c.impl = cq; //btManifoldPoint CollisionEventObject.contacts.push(c); } } - CollisionEventObject.impl = data.impl; + CollisionEventObject.impl = data.impl; //btPersistentManifold CollisionEventObject.selfCollider = collider0; CollisionEventObject.otherCollider = collider1; collider0.emit(CollisionEventObject.type, CollisionEventObject); @@ -383,10 +385,10 @@ export class BulletWorld implements IPhysicsWorld { gatherConatactData () { const numManifolds = bt.Dispatcher_getNumManifolds(this._dispatcher); for (let i = 0; i < numManifolds; i++) { - const manifold = bt.Dispatcher_getManifoldByIndexInternal(this._dispatcher, i); + const manifold = bt.Dispatcher_getManifoldByIndexInternal(this._dispatcher, i);//btPersistentManifold const numContacts = bt.PersistentManifold_getNumContacts(manifold); for (let j = 0; j < numContacts; j++) { - const manifoldPoint = bt.PersistentManifold_getContactPoint(manifold, j); + const manifoldPoint = bt.PersistentManifold_getContactPoint(manifold, j);//btManifoldPoint const s0 = bt.ManifoldPoint_getShape0(manifoldPoint); const s1 = bt.ManifoldPoint_getShape1(manifoldPoint); const shape0: BulletShape = BulletCache.getWrapper(s0, BulletShape.TYPE); @@ -400,7 +402,7 @@ export class BulletWorld implements IPhysicsWorld { item = this.contactsDic.set(shape0.id, shape1.id, { shape0, shape1, contacts: [], impl: manifold }); } - item.contacts.push(manifoldPoint); + item.contacts.push(manifoldPoint);//btManifoldPoint } } }