Files
cocos4/cocos/physics/framework/instance.ts
T
jiaxin.lai 65b0478c69 v1.2 new physics features (#7037)
* 3d v1.1.1-exp physics expriment (#6787)

* activate when not awake (#6781)

* activate when not awake

* cannon too

* off event listener when onDestroy

* add clearVelocity & clearForces

* add CC_CONFIG to ammo\cannon

* add convex mesh & reuse mesh

* cache enable false by default

* terrain collider

* cone collider

* fix scale of ammo box & sphere

* simple collider

* add worldBounds & boundingSphere to collider

* add clear*** to rigid body

* enhance physics material

* enhance event

* plane collider

* stash

* tweaks

* update @cocos/ammo to v1.0.7-beta.1

* typing

* enchance update shape dynamic

* absolute scale cannon-box

* expose allowsleep

* fix warning of generate declaration

* remove physics_builtin

* DO NOT MERGE

* Better reuse

* Correct construct and deconstruct

* avoid ammo memory leaks

* minor refactor contact data

* fix cannon terrain tileSize

* stash

* optimize auto wakeup

* [Ammo] fix update by re add

* 400~

* overwrite ArrayCollisionMatrix.prototype.reset

* Export enum by namespace

* stash

* fix remove child shape of btCompound

* fix event with CCD

* dynamic instantiate

* STASH

* add group to rigid body

* update attachedRigidBody of collider in editor

* stash physics config

* tweaks of ammo config

* stash

* stash

* collision matrix

* stash

* rename deltaTime to fixedTimeStep

* group mask collider

* allow get mask

* set default group to Layers.Enum.DEFAULT

* add group change fallback

* cannon collision matrix

* fix

* update @cocos/ammo to 1.0.7-beta.2

* more prop

* fix

* disable on 1.1.1-exp

* tweaks

* fix circular dependency

* tweaks

* rename simple to simplex

* rename collider -> colliders

* stash

* ammo p2p

* hinge constraint

* adapt cannon p2p pivotB

* ammo p2p setPivotB

* constraint

* tweaks

* remove macro of physics

* refactor physics selector

* import ammo from ammo-instantiated

* update @cocos/ammo

* update @cocos/cannon
2020-08-03 11:00:07 +08:00

227 lines
8.5 KiB
TypeScript

/**
* @hidden
*/
import { IVec3Like } from '../../core/math';
import { IRigidBody } from '../spec/i-rigid-body';
import { WRAPPER } from './physics-selector';
import { IBoxShape, ISphereShape, ICapsuleShape, ITrimeshShape, ICylinderShape, IConeShape, ITerrainShape, ISimplexShape, IPlaneShape, IBaseShape } from '../spec/i-physics-shape';
import { IPhysicsWorld } from '../spec/i-physics-world';
import { errorID, warnID, warn } from '../../core';
import { EDITOR, DEBUG, TEST } from 'internal:constants';
import { EColliderType, EConstraintType } from './physics-enum';
import { IBaseConstraint, IPointToPointConstraint, IHingeConstraint, IConeTwistConstraint } from '../spec/i-physics-constraint';
export function checkPhysicsModule (obj: any) {
if (DEBUG && !TEST && !EDITOR && obj == null) {
errorID(9600);
return true;
}
return false;
}
export function createPhysicsWorld (): IPhysicsWorld {
if (DEBUG && checkPhysicsModule(WRAPPER.PhysicsWorld)) { return null as any; }
return new WRAPPER.PhysicsWorld() as IPhysicsWorld;
}
export function createRigidBody (): IRigidBody {
if (DEBUG && checkPhysicsModule(WRAPPER.RigidBody)) { return null as any; }
return new WRAPPER.RigidBody() as IRigidBody;
}
const CREATE_COLLIDER_PROXY = { INITED: false };
const FUNC = (...v: any) => { return 0 as any; };
interface IEntireShape extends IBoxShape, ISphereShape, ICapsuleShape, ITrimeshShape, ICylinderShape, IConeShape, ITerrainShape, ISimplexShape, IPlaneShape { }
const ENTIRE_SHAPE: IEntireShape = {
impl: null,
collider: null as unknown as any,
attachedRigidBody: null,
initialize: FUNC,
onLoad: FUNC,
onEnable: FUNC,
onDisable: FUNC,
onDestroy: FUNC,
setGroup: FUNC,
getGroup: FUNC,
addGroup: FUNC,
removeGroup: FUNC,
setMask: FUNC,
getMask: FUNC,
addMask: FUNC,
removeMask: FUNC,
setMaterial: FUNC,
setAsTrigger: FUNC,
setCenter: FUNC,
getAABB: FUNC,
getBoundingSphere: FUNC,
setSize: FUNC,
setRadius: FUNC,
setCylinderHeight: FUNC,
setDirection: FUNC,
setHeight: FUNC,
setShapeType: FUNC,
setVertices: FUNC,
setMesh: FUNC,
setTerrain: FUNC,
setNormal: FUNC,
setConstant: FUNC,
}
export function createShape (type: EColliderType): IBaseShape {
initColliderProxy();
return CREATE_COLLIDER_PROXY[type]();
}
function initColliderProxy () {
if (CREATE_COLLIDER_PROXY.INITED) return;
CREATE_COLLIDER_PROXY.INITED = true;
const PHYSICS_BUILTIN = globalThis['CC_PHYSICS_BUILTIN'];
const PHYSICS_CANNON = globalThis['CC_PHYSICS_CANNON'];
const PHYSICS_AMMO = globalThis['CC_PHYSICS_AMMO'];
CREATE_COLLIDER_PROXY[EColliderType.BOX] = function createBoxShape (size: IVec3Like): IBoxShape {
if (DEBUG && checkPhysicsModule(WRAPPER.BoxShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.BoxShape(size) as IBoxShape;
}
CREATE_COLLIDER_PROXY[EColliderType.SPHERE] = function createSphereShape (radius: number): ISphereShape {
if (DEBUG && checkPhysicsModule(WRAPPER.SphereShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.SphereShape(radius) as ISphereShape;
}
CREATE_COLLIDER_PROXY[EColliderType.CAPSULE] = function createCapsuleShape (radius = 0.5, height = 2, dir = 1): ICapsuleShape {
if (PHYSICS_BUILTIN || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.CapsuleShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.CapsuleShape(radius, height, dir) as ICapsuleShape;
} else {
warnID(9610);
return ENTIRE_SHAPE;
}
}
CREATE_COLLIDER_PROXY[EColliderType.CYLINDER] = function createCylinderShape (radius = 0.5, height = 2, dir = 1): ICylinderShape {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.CylinderShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.CylinderShape(radius, height, dir) as ICylinderShape;
} else {
warnID(9612);
return ENTIRE_SHAPE;
}
}
CREATE_COLLIDER_PROXY[EColliderType.CONE] = function createConeShape (radius = 0.5, height = 1, dir = 1): IConeShape {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.ConeShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.ConeShape(radius, height, dir) as IConeShape;
} else {
warnID(9612);
return ENTIRE_SHAPE;
}
}
CREATE_COLLIDER_PROXY[EColliderType.MESH] = function createTrimeshShape (): ITrimeshShape {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.TrimeshShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.TrimeshShape() as ITrimeshShape;
} else {
warnID(9611);
return ENTIRE_SHAPE;
}
}
CREATE_COLLIDER_PROXY[EColliderType.TERRAIN] = function createTerrainShape (): ITerrainShape {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.TerrainShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.TerrainShape() as ITerrainShape;
} else {
warn("[Physics]: builtin physics system doesn't support cylinder collider");
return ENTIRE_SHAPE;
}
}
CREATE_COLLIDER_PROXY[EColliderType.SIMPLEX] = function createSimplexShape (): ISimplexShape {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.SimplexShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.SimplexShape() as ISimplexShape;
} else {
warn("[Physics]: builtin physics system doesn't support simple collider");
return ENTIRE_SHAPE;
}
}
CREATE_COLLIDER_PROXY[EColliderType.PLANE] = function createPlaneShape (): IPlaneShape {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.PlaneShape)) { return ENTIRE_SHAPE; }
return new WRAPPER.PlaneShape() as IPlaneShape;
} else {
warn("[Physics]: builtin physics system doesn't support plane collider");
return ENTIRE_SHAPE;
}
}
}
/// CREATE CONSTRAINT ///
const CREATE_CONSTRAINT_PROXY = { INITED: false };
interface IEntireConstraint extends IPointToPointConstraint, IHingeConstraint, IConeTwistConstraint { }
const ENTIRE_CONSTRAINT: IEntireConstraint = {
'impl': null,
'initialize': FUNC,
'onLoad': FUNC,
'onEnable': FUNC,
'onDisable': FUNC,
'onDestroy': FUNC,
'setEnableCollision': FUNC,
'setConnectedBody': FUNC,
'setPivotA': FUNC,
'setPivotB': FUNC,
}
export function createConstraint (type: EConstraintType): IBaseConstraint {
initConstraintProxy();
return CREATE_CONSTRAINT_PROXY[type]();
}
function initConstraintProxy () {
if (CREATE_CONSTRAINT_PROXY.INITED) return;
CREATE_CONSTRAINT_PROXY.INITED = true;
const PHYSICS_BUILTIN = globalThis['CC_PHYSICS_BUILTIN'];
const PHYSICS_CANNON = globalThis['CC_PHYSICS_CANNON'];
const PHYSICS_AMMO = globalThis['CC_PHYSICS_AMMO'];
CREATE_CONSTRAINT_PROXY[EConstraintType.POINT_TO_POINT] = function createPointToPointConstraint (): IPointToPointConstraint {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.PointToPointConstraint)) { return ENTIRE_CONSTRAINT; }
return new WRAPPER.PointToPointConstraint() as IPointToPointConstraint;
} else {
warn("[Physics]: builtin physics system doesn't support point to point constraint");
return ENTIRE_CONSTRAINT;
}
}
CREATE_CONSTRAINT_PROXY[EConstraintType.HINGE] = function createHingeConstraint (): IHingeConstraint {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.HingeConstraint)) { return ENTIRE_CONSTRAINT; }
return new WRAPPER.HingeConstraint() as IHingeConstraint;
} else {
warn("[Physics]: builtin physics system doesn't support hinge constraint");
return ENTIRE_CONSTRAINT;
}
}
CREATE_CONSTRAINT_PROXY[EConstraintType.CONE_TWIST] = function createConeTwistConstraint (): IConeTwistConstraint {
if (PHYSICS_CANNON || PHYSICS_AMMO) {
if (DEBUG && checkPhysicsModule(WRAPPER.ConeTwistConstraint)) { return null as any; }
return new WRAPPER.ConeTwistConstraint() as IConeTwistConstraint;
} else {
warn("[Physics]: builtin physics system doesn't support cone twist constraint");
return ENTIRE_CONSTRAINT;
}
}
}