mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 03:22:39 +08:00
0dc393b884
* support physx physics engine * tweaks * stash contact event * update @cocos/physx * basic event impl * stash * support BYTEDANCE * stash * stash * fix inertia * PhysX mesh collider * PhysX static trimesh geometry * PhysX terrain collider * add kinematic * bytedance plane collider * bytedance v2 * support trigger * bytedance triangle mesh support * raycast filter * raycast support * stash all * stash * center of mass * stash * stash fixed joint * stash * stash * physx hinge joint * physx distance joint * tweaks * tweaks * bytedance raycast * tweaks * update mass * tweaks * tweaks * joint * stash * bytedance adapters * tweaks * stash * apply force/impulse * add updateEventListener * Fixed some loose usage * add getShapeFlags/getShapeMaterials * tweaks * optimize bytedance adapter * fix isStaticsOrKinematic * support cylinder & cone * contact normal * update @cocos/physx to 1.0.0-beta.5 * rigid body tweaks * add syncAfterEvents * hinge constraint * filter static - static * ammo refactor * cannon refactor * ammo p2p constraint refactor * update @cocos/ammo to 1.0.8 * lint ammo * more lint * docs * tweaks * avoid direct use globalThis
492 lines
16 KiB
TypeScript
492 lines
16 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-namespace */
|
|
/*
|
|
Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
|
|
|
|
https://www.cocos.com/
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
|
|
/**
|
|
* @packageDocumentation
|
|
* @module physics
|
|
*/
|
|
|
|
import { ccclass, tooltip, displayOrder, displayName, readOnly, type, serializable } from 'cc.decorator';
|
|
import { EDITOR } from 'internal:constants';
|
|
import { Eventify } from '../../../../core/event';
|
|
import { Vec3 } from '../../../../core/math';
|
|
import { CollisionEventType, TriggerEventType } from '../../physics-interface';
|
|
import { RigidBody } from '../rigid-body';
|
|
import { PhysicsMaterial } from '../../assets/physics-material';
|
|
import { PhysicsSystem } from '../../physics-system';
|
|
import { Component, error, Node } from '../../../../core';
|
|
import { IBaseShape } from '../../../spec/i-physics-shape';
|
|
import { AABB, Sphere } from '../../../../core/geometry';
|
|
import { EColliderType, EAxisDirection } from '../../physics-enum';
|
|
import { createShape } from '../../instance';
|
|
|
|
/**
|
|
* @en
|
|
* Base class of collider.
|
|
* @zh
|
|
* 碰撞器的基类。
|
|
*/
|
|
@ccclass('cc.Collider')
|
|
export class Collider extends Eventify(Component) {
|
|
/**
|
|
* @en
|
|
* Enumeration of collider types.
|
|
* @zh
|
|
* 碰撞体类型的枚举。
|
|
*/
|
|
static readonly Type = EColliderType;
|
|
|
|
/**
|
|
* @en
|
|
* Enumeration of axes.
|
|
* @zh
|
|
* 坐标轴方向的枚举。
|
|
*/
|
|
static readonly Axis = EAxisDirection;
|
|
|
|
/// PUBLIC PROPERTY GETTER\SETTER ///
|
|
|
|
/**
|
|
* @en
|
|
* Gets the collider attached rigid-body, this may be null.
|
|
* @zh
|
|
* 获取碰撞器所绑定的刚体组件,可能为 null 。
|
|
*/
|
|
@type(RigidBody)
|
|
@readOnly
|
|
@displayName('Attached')
|
|
@displayOrder(-2)
|
|
public get attachedRigidBody (): RigidBody | null {
|
|
return findAttachedBody(this.node);
|
|
// return this._attachedRigidBody;
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Gets or sets the physical material for this collider.
|
|
* @zh
|
|
* 获取或设置此碰撞器的物理材质。
|
|
*/
|
|
@type(PhysicsMaterial)
|
|
@displayName('Material')
|
|
@displayOrder(-1)
|
|
@tooltip('源材质')
|
|
public get sharedMaterial () {
|
|
return this._material;
|
|
}
|
|
|
|
public set sharedMaterial (value) {
|
|
if (EDITOR) {
|
|
this._material = value;
|
|
} else {
|
|
this.material = value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Gets or sets the physics material for this collider, which in Shared state will generate a new instance.
|
|
* @zh
|
|
* 获取或设置此碰撞器的物理材质,共享状态下获取将会生成新的实例。
|
|
*/
|
|
public get material () {
|
|
if (this._isSharedMaterial && this._material != null) {
|
|
this._material.off('physics_material_update', this._updateMaterial, this);
|
|
this._material = this._material.clone();
|
|
this._material.on('physics_material_update', this._updateMaterial, this);
|
|
this._isSharedMaterial = false;
|
|
}
|
|
return this._material;
|
|
}
|
|
|
|
public set material (value) {
|
|
if (this._shape) {
|
|
if (value != null && this._material != null) {
|
|
if (this._material._uuid !== value._uuid) {
|
|
this._material.off('physics_material_update', this._updateMaterial, this);
|
|
value.on('physics_material_update', this._updateMaterial, this);
|
|
this._isSharedMaterial = false;
|
|
this._material = value;
|
|
}
|
|
} else if (value != null && this._material == null) {
|
|
value.on('physics_material_update', this._updateMaterial, this);
|
|
this._material = value;
|
|
} else if (value == null && this._material != null) {
|
|
this._material.off('physics_material_update', this._updateMaterial, this);
|
|
this._material = value;
|
|
}
|
|
this._updateMaterial();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Gets or sets the collider is trigger, this will be always trigger if using builtin.
|
|
* @zh
|
|
* 获取或设置碰撞器是否为触发器。(builtin中无论真假都为触发器)
|
|
*/
|
|
@displayOrder(0)
|
|
@tooltip('是否为触发器,触发器不会产生物理反馈')
|
|
public get isTrigger () {
|
|
return this._isTrigger;
|
|
}
|
|
|
|
public set isTrigger (value) {
|
|
this._isTrigger = value;
|
|
if (this._shape) {
|
|
this._shape.setAsTrigger(this._isTrigger);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Gets or sets the center of the collider, in local space.
|
|
* @zh
|
|
* 获取或设置碰撞器的中心点。
|
|
*/
|
|
@type(Vec3)
|
|
@displayOrder(1)
|
|
@tooltip('形状的中心点(与所在 Node 中心点的相对位置)')
|
|
public get center () {
|
|
return this._center;
|
|
}
|
|
|
|
public set center (value: Vec3) {
|
|
Vec3.copy(this._center, value);
|
|
if (this._shape) {
|
|
this._shape.setCenter(this._center);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Gets the wrapper object, through which the lowLevel instance can be accessed.
|
|
* @zh
|
|
* 获取封装对象,通过此对象可以访问到底层实例。
|
|
*/
|
|
public get shape () {
|
|
return this._shape;
|
|
}
|
|
|
|
public get worldBounds (): Readonly<AABB> {
|
|
if (this._aabb == null) this._aabb = new AABB();
|
|
if (this._shape) this._shape.getAABB(this._aabb);
|
|
return this._aabb;
|
|
}
|
|
|
|
public get boundingSphere (): Readonly<Sphere> {
|
|
if (this._boundingSphere == null) this._boundingSphere = new Sphere();
|
|
if (this._shape) this._shape.getBoundingSphere(this._boundingSphere);
|
|
return this._boundingSphere;
|
|
}
|
|
|
|
public get needTriggerEvent () {
|
|
return this._needTriggerEvent;
|
|
}
|
|
|
|
public get needCollisionEvent () {
|
|
return this._needCollisionEvent;
|
|
}
|
|
|
|
readonly TYPE: EColliderType;
|
|
|
|
/// PROTECTED PROPERTY ///
|
|
|
|
protected _shape: IBaseShape | null = null;
|
|
protected _aabb: AABB | null = null;
|
|
protected _boundingSphere: Sphere | null = null;
|
|
protected _isSharedMaterial = true;
|
|
protected _needTriggerEvent = false;
|
|
protected _needCollisionEvent = false;
|
|
// protected _attachedRigidBody: RigidBody | null = null;
|
|
|
|
@type(PhysicsMaterial)
|
|
protected _material: PhysicsMaterial | null = null;
|
|
|
|
@serializable
|
|
protected _isTrigger = false;
|
|
|
|
@serializable
|
|
protected readonly _center: Vec3 = new Vec3();
|
|
|
|
protected get _assertOnLoadCalled (): boolean {
|
|
const r = this._isOnLoadCalled === 0;
|
|
if (r) { error('[Physics]: Please make sure that the node has been added to the scene'); }
|
|
return !r;
|
|
}
|
|
|
|
constructor (type: EColliderType) {
|
|
super();
|
|
this.TYPE = type;
|
|
}
|
|
|
|
/// EVENT INTERFACE ///
|
|
|
|
/**
|
|
* @en
|
|
* Registers callbacks associated with triggered or collision events.
|
|
* @zh
|
|
* 注册触发或碰撞事件相关的回调。
|
|
* @param type - The event type, onTriggerEnter|onTriggerStay|onTriggerExit|onCollisionEnter|onCollisionStay|onCollisionExit;
|
|
* @param callback - The event callback, signature:`(event?:ICollisionEvent|ITriggerEvent)=>void`.
|
|
* @param target - The event callback target.
|
|
*/
|
|
public on<TFunction extends (...any) => void> (type: TriggerEventType | CollisionEventType, callback: TFunction, target?, once?: boolean): any {
|
|
const ret = super.on(type, callback, target, once);
|
|
this._updateNeedEvent(type);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Unregisters callbacks associated with trigger or collision events that have been registered.
|
|
* @zh
|
|
* 取消已经注册的触发或碰撞事件相关的回调。
|
|
* @param type - The event type, onTriggerEnter|onTriggerStay|onTriggerExit|onCollisionEnter|onCollisionStay|onCollisionExit;
|
|
* @param callback - The event callback, signature:`(event?:ICollisionEvent|ITriggerEvent)=>void`.
|
|
* @param target - The event callback target.
|
|
*/
|
|
public off (type: TriggerEventType | CollisionEventType, callback?: (...any) => void, target?) {
|
|
super.off(type, callback, target);
|
|
this._updateNeedEvent();
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Registers a callback associated with a trigger or collision event, which is automatically unregistered once executed.
|
|
* @zh
|
|
* 注册触发或碰撞事件相关的回调,执行一次后会自动取消注册。
|
|
* @param type - The event type, onTriggerEnter|onTriggerStay|onTriggerExit|onCollisionEnter|onCollisionStay|onCollisionExit;
|
|
* @param callback - The event callback, signature:`(event?:ICollisionEvent|ITriggerEvent)=>void`.
|
|
* @param target - The event callback target.
|
|
*/
|
|
public once<TFunction extends (...any) => void> (type: TriggerEventType | CollisionEventType, callback: TFunction, target?): any {
|
|
// TODO: callback invoker now is a entity, after `once` will not calling the upper `off`.
|
|
const ret = super.once(type, callback, target);
|
|
this._updateNeedEvent(type);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Removes all registered events of the specified target or type.
|
|
* @zh
|
|
* 移除所有指定目标或类型的注册事件。
|
|
* @param typeOrTarget - The event type or target.
|
|
*/
|
|
public removeAll (typeOrTarget: TriggerEventType | CollisionEventType | Record<string, unknown>) {
|
|
super.removeAll(typeOrTarget);
|
|
this._updateNeedEvent();
|
|
}
|
|
|
|
/// GROUP MASK ///
|
|
|
|
/**
|
|
* @en
|
|
* Gets the group value.
|
|
* @zh
|
|
* 获取分组值。
|
|
* @returns 整数,范围为 2 的 0 次方 到 2 的 31 次方
|
|
*/
|
|
public getGroup (): number {
|
|
if (this._assertOnLoadCalled) {
|
|
return this._shape!.getGroup();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Sets the group value.
|
|
* @zh
|
|
* 设置分组值。
|
|
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
|
|
*/
|
|
public setGroup (v: number): void {
|
|
if (this._assertOnLoadCalled) {
|
|
this._shape!.setGroup(v);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Add a grouping value to fill in the group you want to join.
|
|
* @zh
|
|
* 添加分组值,可填要加入的 group。
|
|
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
|
|
*/
|
|
public addGroup (v: number) {
|
|
if (this._assertOnLoadCalled) {
|
|
this._shape!.addGroup(v);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Subtract the grouping value to fill in the group to be removed.
|
|
* @zh
|
|
* 减去分组值,可填要移除的 group。
|
|
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
|
|
*/
|
|
public removeGroup (v: number) {
|
|
if (this._assertOnLoadCalled) {
|
|
this._shape!.removeGroup(v);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Gets the mask value.
|
|
* @zh
|
|
* 获取掩码值。
|
|
* @returns 整数,范围为 2 的 0 次方 到 2 的 31 次方
|
|
*/
|
|
public getMask (): number {
|
|
if (this._assertOnLoadCalled) {
|
|
return this._shape!.getMask();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Sets the mask value.
|
|
* @zh
|
|
* 设置掩码值。
|
|
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
|
|
*/
|
|
public setMask (v: number) {
|
|
if (this._assertOnLoadCalled) {
|
|
this._shape!.setMask(v);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Add mask values to fill in groups that need to be checked.
|
|
* @zh
|
|
* 添加掩码值,可填入需要检查的 group。
|
|
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
|
|
*/
|
|
public addMask (v: number) {
|
|
if (this._assertOnLoadCalled) {
|
|
this._shape!.addMask(v);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Subtract the mask value to fill in the group that does not need to be checked.
|
|
* @zh
|
|
* 减去掩码值,可填入不需要检查的 group。
|
|
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
|
|
*/
|
|
public removeMask (v: number) {
|
|
if (this._assertOnLoadCalled) {
|
|
this._shape!.removeMask(v);
|
|
}
|
|
}
|
|
|
|
/// COMPONENT LIFECYCLE ///
|
|
|
|
protected onLoad () {
|
|
if (!EDITOR) {
|
|
this._shape = createShape(this.TYPE);
|
|
this.sharedMaterial = this._material == null ? PhysicsSystem.instance.defaultMaterial : this._material;
|
|
this._shape.initialize(this);
|
|
this._shape.onLoad!();
|
|
}
|
|
}
|
|
|
|
protected onEnable () {
|
|
if (this._shape) {
|
|
this._shape.onEnable!();
|
|
}
|
|
}
|
|
|
|
protected onDisable () {
|
|
if (this._shape) {
|
|
this._shape.onDisable!();
|
|
}
|
|
}
|
|
|
|
protected onDestroy () {
|
|
if (this._shape) {
|
|
if (this._material) {
|
|
this._material.off('physics_material_update', this._updateMaterial, this);
|
|
}
|
|
this._shape.onDestroy!();
|
|
}
|
|
if (this._boundingSphere) this._boundingSphere.destroy();
|
|
}
|
|
|
|
private _updateMaterial () {
|
|
if (this._shape) {
|
|
this._shape.setMaterial(this._material);
|
|
}
|
|
}
|
|
|
|
private _updateNeedEvent (type?: string) {
|
|
if (this.isValid) {
|
|
if (type !== undefined) {
|
|
if (type === 'onCollisionEnter' || type === 'onCollisionStay' || type === 'onCollisionExit') {
|
|
this._needCollisionEvent = true;
|
|
}
|
|
if (type === 'onTriggerEnter' || type === 'onTriggerStay' || type === 'onTriggerExit') {
|
|
this._needTriggerEvent = true;
|
|
}
|
|
} else {
|
|
if (!(this.hasEventListener('onTriggerEnter')
|
|
|| this.hasEventListener('onTriggerStay')
|
|
|| this.hasEventListener('onTriggerExit'))) {
|
|
this._needTriggerEvent = false;
|
|
}
|
|
if (!(this.hasEventListener('onCollisionEnter')
|
|
|| this.hasEventListener('onCollisionStay')
|
|
|| this.hasEventListener('onCollisionExit'))) {
|
|
this._needCollisionEvent = false;
|
|
}
|
|
}
|
|
if (this._shape) this._shape.updateEventListener();
|
|
}
|
|
}
|
|
}
|
|
|
|
export namespace Collider {
|
|
export type Type = EnumAlias<typeof EColliderType>;
|
|
export type Axis = EnumAlias<typeof EAxisDirection>;
|
|
}
|
|
|
|
function findAttachedBody (node: Node): RigidBody | null {
|
|
const rb = node.getComponent(RigidBody);
|
|
if (rb && rb.isValid) {
|
|
return rb;
|
|
}
|
|
return null;
|
|
// if (node.parent == null || node.parent == node.scene) return null;
|
|
// return findAttachedBody(node.parent);
|
|
}
|