From 765ac770f090dcca743abb99dcd8636daf5ea982 Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Fri, 12 Jul 2019 10:03:47 +0800 Subject: [PATCH] add physiscs system interface (#4866) --- .../physics/detail/physics-based-component.ts | 6 +- cocos/3d/framework/physics/physics-system.ts | 85 ++++++++++++++++--- cocos/3d/physics/api.ts | 13 ++- cocos/3d/physics/cannon/cannon-world.ts | 25 ++++-- cocos/3d/physics/cocos/builtin-world.ts | 6 +- cocos/3d/physics/instance.ts | 2 +- 6 files changed, 109 insertions(+), 28 deletions(-) diff --git a/cocos/3d/framework/physics/detail/physics-based-component.ts b/cocos/3d/framework/physics/detail/physics-based-component.ts index 738fedf45a..78b15f17de 100644 --- a/cocos/3d/framework/physics/detail/physics-based-component.ts +++ b/cocos/3d/framework/physics/detail/physics-based-component.ts @@ -149,15 +149,15 @@ export class PhysicsBasedComponent extends Component { // if (rigidbody.sharedBody != null) { // sharedBody = rigidbody.sharedBody; // } else { - // sharedBody = new SharedRigidBody(rigidbody.node, rigidbody, cc.director._physicsSystem.world); + // sharedBody = new SharedRigidBody(rigidbody.node, rigidbody, cc.director._physicsSystem._world); // } // } else { - // sharedBody = new SharedRigidBody(this.node, null, cc.director._physicsSystem.world); + // sharedBody = new SharedRigidBody(this.node, null, cc.director._physicsSystem._world); // } // binding collider to self rigidybody, if it exist. const rigidbody = this.getComponent(cc.RigidBodyComponent); - sharedBody = new SharedRigidBody(this.node, rigidbody, cc.director._physicsSystem.world); + sharedBody = new SharedRigidBody(this.node, rigidbody, cc.director._physicsSystem._world); } sharedBody!.ref(); this._sharedBody = sharedBody!; diff --git a/cocos/3d/framework/physics/physics-system.ts b/cocos/3d/framework/physics/physics-system.ts index 349a56052e..b088ea17e5 100644 --- a/cocos/3d/framework/physics/physics-system.ts +++ b/cocos/3d/framework/physics/physics-system.ts @@ -2,6 +2,7 @@ * @category physics */ +import { Vec3 } from '../../../core/value-types'; import { PhysicsWorldBase } from '../../physics/api'; import { createPhysicsWorld } from '../../physics/instance'; @@ -10,16 +11,6 @@ import { createPhysicsWorld } from '../../physics/instance'; * 物理系统。 */ export class PhysicsSystem { - private _world: PhysicsWorldBase; - private _enable = true; - private _deltaTime = 1.0 / 60.0; - private _maxSubStep = 2; - // private _frameRate = 60; - // private _singleStep = false; - - constructor () { - this._world = createPhysicsWorld(); - } /** * @zh @@ -33,6 +24,23 @@ export class PhysicsSystem { this._enable = value; } + /** + * @zh + * 获取或设置物理系统是否允许自动休眠,默认为 true + */ + public get allowSleep (): boolean { + return this._allowSleep; + } + + public set allowSleep (v: boolean) { + this._allowSleep = v; + if (!CC_EDITOR) { + if (!CC_PHYSICS_BUILT_IN) { + this._world.setAllowSleep(this._allowSleep); + } + } + } + // shielding for alpha version // /** // * @zh @@ -71,6 +79,34 @@ export class PhysicsSystem { this._deltaTime = value; } + /** + * @zh + * 获取或设置物理世界的重力数值,默认为 (0, -10, 0) + */ + public get gravity (): Vec3 { + if (!CC_EDITOR) { + if (!CC_PHYSICS_BUILT_IN) { + this._world.getGravity(this._gravity); + } + } + return this._gravity; + } + + public set gravity (gravity: Vec3) { + this._gravity.x = gravity.x; + this._gravity.y = gravity.y; + this._gravity.z = gravity.z; + if (!CC_EDITOR) { + if (!CC_PHYSICS_BUILT_IN) { + this._world.setGravity(gravity); + } + } + } + + // get world () { + // return this._world; + // } + // shielding for alpha version // /** // * @zh @@ -85,6 +121,30 @@ export class PhysicsSystem { // this._deltaTime = 1 / this._frameRate; // } + public static get ins (): PhysicsSystem { + return cc.director._physicsSystem; + } + + private _world: PhysicsWorldBase; + private _enable = true; + private _deltaTime = 1.0 / 60.0; + private _maxSubStep = 2; + // private _frameRate = 60; + // private _singleStep = false; + private _allowSleep = true; + private _gravity = new Vec3(0, -10, 0); + + constructor () { + this._world = createPhysicsWorld(); + this.gravity = this._gravity; + this.allowSleep = this._allowSleep; + } + + /** + * @zh + * 执行一次物理系统的模拟,目前将在每帧自动执行一次。 + * @param deltaTime 与上一次执行相差的时间,目前为每帧消耗时间 + */ public update (deltaTime: number) { if (CC_EDITOR) { return; @@ -97,8 +157,5 @@ export class PhysicsSystem { // this._enable = false; // } } - - get world () { - return this._world; - } } +cc.PhysicsSystem = PhysicsSystem; diff --git a/cocos/3d/physics/api.ts b/cocos/3d/physics/api.ts index 69f7a80c83..3740d0a925 100644 --- a/cocos/3d/physics/api.ts +++ b/cocos/3d/physics/api.ts @@ -49,7 +49,7 @@ export type BeforeStepCallback = () => void; export type AfterStepCallback = () => void; -export interface PhysicsWorldBase { +export interface BuiltInWorldBase { step (deltaTime: number, ...args: any): void; @@ -80,6 +80,17 @@ export interface PhysicsWorldBase { raycastAll (from: Vec3, to: Vec3, options: IRaycastOptions, callback: (result: RaycastResult) => void): boolean; } +export interface PhysicsWorldBase extends BuiltInWorldBase { + + setGravity (gravity: Vec3): void; + + getGravity (out: Vec3): void; + + getAllowSleep (): boolean; + + setAllowSleep (v: boolean): void; +} + export interface BuiltInRigidBodyBase { getGroup (): number; diff --git a/cocos/3d/physics/cannon/cannon-world.ts b/cocos/3d/physics/cannon/cannon-world.ts index b05fa3e116..6f62eb2fbe 100644 --- a/cocos/3d/physics/cannon/cannon-world.ts +++ b/cocos/3d/physics/cannon/cannon-world.ts @@ -4,10 +4,14 @@ import { RaycastResult } from '../raycast-result'; import { setWrap } from '../util'; import { AfterStepCallback, BeforeStepCallback, IRaycastOptions, PhysicsWorldBase } from './../api'; import { defaultCannonContactMaterial, defaultCannonMaterial } from './cannon-const'; -import { fillRaycastResult, toCannonRaycastOptions } from './cannon-util'; +import { fillRaycastResult, toCannonRaycastOptions, toCannonVec3, toCocosVec3 } from './cannon-util'; import { CannonConstraint } from './constraint/cannon-constraint'; export class CannonWorld implements PhysicsWorldBase { + + get impl () { + return this._world; + } private _world: CANNON.World; private _customBeforeStepListener: BeforeStepCallback[] = []; private _customAfterStepListener: AfterStepCallback[] = []; @@ -16,18 +20,27 @@ export class CannonWorld implements PhysicsWorldBase { constructor () { this._world = new CANNON.World(); setWrap(this._world, this); - this._world.allowSleep = true; - this._world.gravity.set(0, -9.81, 0); this._world.broadphase = new CANNON.NaiveBroadphase(); this._world.defaultMaterial = defaultCannonMaterial; this._world.defaultContactMaterial = defaultCannonContactMaterial; } - public destroy () { + public getAllowSleep (): boolean { + return this._world.allowSleep; + } + public setAllowSleep (v: boolean): void { + this._world.allowSleep = v; } - get impl () { - return this._world; + public setGravity (gravity: Vec3): void { + this._world.gravity = toCannonVec3(gravity); + } + + public getGravity (out: Vec3): void { + toCocosVec3(this._world.gravity, out); + } + + public destroy () { } // get defaultContactMaterial () { diff --git a/cocos/3d/physics/cocos/builtin-world.ts b/cocos/3d/physics/cocos/builtin-world.ts index 4a07e953a9..e09a8bf7c9 100644 --- a/cocos/3d/physics/cocos/builtin-world.ts +++ b/cocos/3d/physics/cocos/builtin-world.ts @@ -3,8 +3,8 @@ */ import { Vec3 } from '../../../core/value-types'; -import { AfterStepCallback, BeforeStepCallback, BuiltInRigidBodyBase, IRaycastOptions, - ITriggerEventType, PhysicsWorldBase, RigidBodyBase, ShapeBase } from '../api'; +import { AfterStepCallback, BeforeStepCallback, BuiltInRigidBodyBase, BuiltInWorldBase, + IRaycastOptions, ITriggerEventType, ShapeBase } from '../api'; import { RaycastResult } from '../raycast-result'; import { getWrap } from '../util'; import { BuiltInBody } from './builtin-body'; @@ -16,7 +16,7 @@ import { ArrayCollisionMatrix } from './utils/array-collision-matrix'; * efficient discrete collision detector, * not a full physical simulator */ -export class BuiltInWorld implements PhysicsWorldBase { +export class BuiltInWorld implements BuiltInWorldBase { private _bodies: BuiltInRigidBodyBase[] = []; diff --git a/cocos/3d/physics/instance.ts b/cocos/3d/physics/instance.ts index 5d1be3aff9..3927fd913a 100644 --- a/cocos/3d/physics/instance.ts +++ b/cocos/3d/physics/instance.ts @@ -8,7 +8,7 @@ import { BoxShape, PhysicsWorld, RigidBody, SphereShape } from './impl-selector' import { RaycastResult } from './raycast-result'; export function createPhysicsWorld (): PhysicsWorldBase { - return new PhysicsWorld(); + return new PhysicsWorld() as PhysicsWorldBase; } export function createRigidBody (options?: ICreateBodyOptions): RigidBodyBase {