add physiscs system interface (#4866)

This commit is contained in:
jiaxin.lai
2019-07-12 10:03:47 +08:00
committed by Weiliang Wang
parent 01272e2d10
commit 765ac770f0
6 changed files with 109 additions and 28 deletions
@@ -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!;
+71 -14
View File
@@ -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;
+12 -1
View File
@@ -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;
+19 -6
View File
@@ -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<PhysicsWorldBase>(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 () {
+3 -3
View File
@@ -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[] = [];
+1 -1
View File
@@ -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 {