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)

This commit is contained in:
jiaxin.lai
2019-04-08 14:35:23 +08:00
committed by Weiliang Wang
parent ddc4189ded
commit 3bb398a31f
4 changed files with 45 additions and 9 deletions
@@ -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;
}
+7 -2
View File
@@ -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 {
+15 -4
View File
@@ -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<PhysicsWorldBase>(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 {
+6
View File
@@ -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();
}