From 3bb398a31f5a3e8a689a330800f8558149d19597 Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Mon, 8 Apr 2019 14:35:23 +0800 Subject: [PATCH] Fix a problem where the physical system collision event does not trigger because of automatic sleep, adding a trigger property to the collider component (#4158) --- .../framework/physics/collider-component.ts | 20 ++++++++++++++++--- cocos/3d/physics/api.d.ts | 9 +++++++-- cocos/3d/physics/cannon-impl.ts | 19 ++++++++++++++---- cocos/3d/physics/instance.ts | 6 ++++++ 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/cocos/3d/framework/physics/collider-component.ts b/cocos/3d/framework/physics/collider-component.ts index e52c72ee1f..705dc3699e 100644 --- a/cocos/3d/framework/physics/collider-component.ts +++ b/cocos/3d/framework/physics/collider-component.ts @@ -8,16 +8,29 @@ import { import Vec3 from '../../../core/value-types/vec3'; import { vec3 } from '../../../core/vmath'; import { BoxShapeBase, ShapeBase, SphereShapeBase } from '../../physics/api'; -import { createBoxShape, createSphereShape } from '../../physics/instance'; +import { createBoxShape, createSphereShape, ERigidBodyType } from '../../physics/instance'; import { PhysicsBasedComponent } from './detail/physics-based-component'; export class ColliderComponentBase extends PhysicsBasedComponent { protected _shapeBase: ShapeBase | null = null; + @property + private _triggered: boolean = false; + @property + get isTrigger () { return this._triggered; } + set isTrigger (value) { + this._triggered = value; + if (this._body) { + let type = this._triggered ? ERigidBodyType.DYNAMIC : ERigidBodyType.STATIC; + this._body.setType(type); + this._body.setIsTrigger(value); + } + } + /** * The center of the collider, in local space. */ - @property({type: Vec3}) + @property({ type: Vec3 }) get center () { return this._center; } @@ -44,6 +57,7 @@ export class ColliderComponentBase extends PhysicsBasedComponent { if (this._enabled) { this.onEnable(); } + this.isTrigger = this._triggered; } public onEnable () { @@ -92,7 +106,7 @@ export class BoxColliderComponent extends ColliderComponentBase { * The size of the box, in local space. * @note Shall not specify size with component 0. */ - @property({type: Vec3}) + @property({ type: Vec3 }) get size () { return this._size; } diff --git a/cocos/3d/physics/api.d.ts b/cocos/3d/physics/api.d.ts index ba83bf531b..e95f1555da 100644 --- a/cocos/3d/physics/api.d.ts +++ b/cocos/3d/physics/api.d.ts @@ -1,6 +1,7 @@ import { Vec3, Quat } from '../../core/value-types'; import { Node } from '../../scene-graph'; import { RaycastResult } from './raycast-result'; +import { ERigidBodyType } from './instance'; export interface IRaycastOptions { collisionFilterMask?: number; @@ -59,6 +60,10 @@ export class PhysicsWorldBase { export class RigidBodyBase { constructor (options?: ICreateBodyOptions); + /**获取/设置刚体类型 : ERigidBodyType*/ + getType (): ERigidBodyType; + setType (v: ERigidBodyType): void; + addShape (shape: ShapeBase): void; removeShape (shape: ShapeBase): void; @@ -128,7 +133,7 @@ export class RigidBodyBase { getUserData (): any; - setUserData(data: any): void; + setUserData (data: any): void; } export class ShapeBase { @@ -140,7 +145,7 @@ export class ShapeBase { getUserData (): any; - setUserData(data: any): void; + setUserData (data: any): void; } export class SphereShapeBase extends ShapeBase { diff --git a/cocos/3d/physics/cannon-impl.ts b/cocos/3d/physics/cannon-impl.ts index 39c1a2d55b..e8ef421c58 100644 --- a/cocos/3d/physics/cannon-impl.ts +++ b/cocos/3d/physics/cannon-impl.ts @@ -1,7 +1,8 @@ import CANNON from 'cannon'; import { Quat, Vec3 } from '../../core/value-types'; import { quat, vec3 } from '../../core/vmath'; -import { AfterStepCallback, BeforeStepCallback, +import { + AfterStepCallback, BeforeStepCallback, BoxShapeBase, ConstraintBase, DistanceConstraintBase, ICollisionCallback, ICollisionEvent, @@ -9,9 +10,11 @@ import { AfterStepCallback, BeforeStepCallback, ILockConstraintOptions, IPointToPointConstraintOptions, IRaycastOptions, LockConstraintBase, PhysicsWorldBase, - PointToPointConstraintBase, RigidBodyBase, ShapeBase, SphereShapeBase } from './api'; + PointToPointConstraintBase, RigidBodyBase, ShapeBase, SphereShapeBase +} from './api'; import { RaycastResult } from './raycast-result'; import { stringfyQuat, stringfyVec3 } from './util'; +import { ERigidBodyType } from './instance'; const defaultCannonMaterial = new CANNON.Material(''); const defaultCannonContactMaterial = new CANNON.ContactMaterial( @@ -32,7 +35,7 @@ export class CannonWorld implements PhysicsWorldBase { constructor () { this._cannonWorld = new CANNON.World(); setWrap(this._cannonWorld, this); - this._cannonWorld.allowSleep = true; + // this._cannonWorld.allowSleep = true; this._cannonWorld.gravity.set(0, -9.81, 0); this._cannonWorld.broadphase = new CANNON.NaiveBroadphase(); this._cannonWorld.defaultMaterial = defaultCannonMaterial; @@ -216,6 +219,14 @@ export class CannonRigidBody implements RigidBodyBase { return this._name; } + public getType (): ERigidBodyType { + return this._cannonBody.type; + } + + public setType (v: ERigidBodyType): void { + this._cannonBody.type = v; + } + public addShape (shape: CannonShape) { const index = this._shapes.length; this._shapes.push(shape); @@ -296,7 +307,7 @@ export class CannonRigidBody implements RigidBodyBase { } public setIsTrigger (value: boolean): void { - this._cannonBody.collisionResponse = value; + this._cannonBody.collisionResponse = !value; } public getVelocity (): Vec3 { diff --git a/cocos/3d/physics/instance.ts b/cocos/3d/physics/instance.ts index fa4d8f93fb..acdbdeeb68 100644 --- a/cocos/3d/physics/instance.ts +++ b/cocos/3d/physics/instance.ts @@ -3,6 +3,12 @@ import { BoxShapeBase, ICreateBodyOptions, PhysicsWorldBase, RigidBodyBase, Sphe import { BoxShape, PhysicsWorld, RigidBody, SphereShape } from './impl-selector'; import { RaycastResult } from './raycast-result'; +export enum ERigidBodyType { + DYNAMIC = 1, + STATIC = 2, + KINEMATIC = 4 +} + export function createPhysicsWorld (): PhysicsWorldBase { return new PhysicsWorld(); }