diff --git a/cocos/3d/physics/cocos/built-in-body.ts b/cocos/3d/physics/cocos/built-in-body.ts new file mode 100644 index 0000000000..ae5f1ff5c5 --- /dev/null +++ b/cocos/3d/physics/cocos/built-in-body.ts @@ -0,0 +1,191 @@ +import { Quat, Vec3 } from '../../../core'; +import { Node } from '../../../scene-graph'; +import { intersect } from '../../geom-utils'; +import { ICollisionCallback, ICollisionEvent, PhysicsWorldBase, RigidBodyBase, ShapeBase } from '../api'; +import { ERigidBodyType } from '../physic-enum'; +import { BuiltInShape } from './built-in-shape'; +import { BuiltInWorld } from './built-in-world'; + +/** + * Built-in static collider, no physical forces involved + */ +export class BuiltInBody implements RigidBodyBase { + get collisionFilterGroup () { return this._collisionFilterGroup; } + set collisionFilterGroup (v: number) { this._collisionFilterGroup = v; } + get collisionFilterMask () { return this._collisionFilterMask; } + set collisionFilterMask (v: number) { this._collisionFilterMask = v; } + + /** 刚体类型 */ + private type: ERigidBodyType = ERigidBodyType.DYNAMIC; + /** 属于的组 */ + private _collisionFilterGroup: number = 1; + /** 检测的组 */ + private _collisionFilterMask: number = 1; + /** 碰撞回调 */ + private _collisionCallbacks: ICollisionCallback[] = []; + /** 物理世界 */ + private _world!: BuiltInWorld | null; + /** Body拥有的现状 */ + private _shapes: BuiltInShape[] = []; + /** Body对应的场景对象 */ + private userData: any; + + constructor (options) { } + + public intersects (body: BuiltInBody) { + // tslint:disable-next-line: prefer-for-of + for (let i = 0; i < this._shapes.length; i++) { + const shapeA = this._shapes[i]; + // tslint:disable-next-line:prefer-for-of + for (let j = 0; j < body._shapes.length; j++) { + const shapeB = body._shapes[j]; + if (intersect.resolve(shapeA.shape, shapeB.shape)) { + const event: ICollisionEvent = { + source: this, + target: body, + }; + for (const callback of this._collisionCallbacks) { + callback(event); + } + for (const callback of body._collisionCallbacks) { + callback(event); + } + } + } + } + } + public getType (): ERigidBodyType { + return this.type; + } + public setType (v: ERigidBodyType): void { + this.type = v; + } + public wakeUp (): void { + + } + public sleep (): void { + + } + public addShape (shape: BuiltInShape): void { + this._shapes.push(shape); + } + public removeShape (shape: BuiltInShape): void { + const i = this._shapes.indexOf(shape); + if (i >= 0) { + this._shapes.splice(i, 1); + } + } + public getMass (): number { + return 0; + } + public setMass (value: number): void { + + } + public applyForce (force: Vec3, position?: Vec3 | undefined): void { + + } + public applyImpulse (impulse: Vec3, position?: Vec3 | undefined): void { + + } + public getIsKinematic (): boolean { + return true; + } + public setIsKinematic (value: boolean): void { + + } + public getLinearDamping (): number { + return 0; + } + public setLinearDamping (value: number): void { + + } + public getAngularDamping (): number { + return 0; + } + public setAngularDamping (value: number): void { + + } + public getUseGravity (): boolean { + return false; + } + public setUseGravity (value: boolean): void { + + } + public getIsTrigger (): boolean { + return false; + } + public setIsTrigger (value: boolean): void { + + } + public getVelocity (): Vec3 { + return new Vec3(); + } + public setVelocity (value: Vec3): void { + + } + public getFreezeRotation (): boolean { + return false; + } + public setFreezeRotation (value: boolean): void { + + } + public setCollisionFilter (group: number, mask: number): void { + + } + public setWorld (world: PhysicsWorldBase | null): void { + if (this._world) { + this._world = null; + } + + const cworld = world as unknown as (BuiltInWorld | null); + if (cworld) { + cworld.addBody(this); + } + this._world = cworld; + } + public commitShapeUpdates (): void { + + } + public isPhysicsManagedTransform (): boolean { + return false; + } + public getPosition (out: Vec3): void { + + } + public setPosition (pos: Vec3): void { + /** 更新所有形状的位置 */ + for (const shape of this._shapes) { + shape.setPosition(pos); + } + } + public getRotation (out: Quat): void { + + } + public setRotation (rot: Quat): void { + /** 更新所有形状的方向 */ + for (const shape of this._shapes) { + shape.setRotation(rot); + } + } + public scaleAllShapes (scale: Vec3): void { + for (const shape of this._shapes) { + shape.setScale(scale); + } + } + public addCollisionCallback (callback: ICollisionCallback): void { + this._collisionCallbacks.push(callback); + } + public removeCollisionCllback (callback: ICollisionCallback): void { + const i = this._collisionCallbacks.indexOf(callback); + if (i >= 0) { + this._collisionCallbacks.splice(i, 1); + } + } + public getUserData () { + return this.userData; + } + public setUserData (data: any): void { + this.userData = data; + } + +} diff --git a/cocos/3d/physics/cocos/built-in-interface.ts b/cocos/3d/physics/cocos/built-in-interface.ts new file mode 100644 index 0000000000..c7b5f64dbb --- /dev/null +++ b/cocos/3d/physics/cocos/built-in-interface.ts @@ -0,0 +1,8 @@ +/** + * 声明了一系列built-in-physics相关的的接口 + */ +export interface IShapeTransform { + translate (...args: any): any; + scale (...args: any): any; + rotate (...args: any): any; +} diff --git a/cocos/3d/physics/cocos/built-in-shape.ts b/cocos/3d/physics/cocos/built-in-shape.ts new file mode 100644 index 0000000000..59317e43a4 --- /dev/null +++ b/cocos/3d/physics/cocos/built-in-shape.ts @@ -0,0 +1,87 @@ +import { Quat, Vec3 } from '../../../core'; +import { vec3 } from '../../../core/vmath'; +import { ShapeBase } from '../api'; +import { EShapeType } from '../physic-enum'; +import { BuiltInBody } from './built-in-body'; +import { IShapeTransform } from './built-in-interface'; +import { BoxShape } from './built-in-shape/BoxShape'; +import { SphereShape } from './built-in-shape/SphereShape'; + +export class BuiltInShape implements ShapeBase { + get shape () { return this._shape; } + get type () { return this._type; } + set type (v: EShapeType) { this._type = v; } + + protected _shape: IShapeTransform | undefined; + protected _type: EShapeType = EShapeType.SHAPE_AABB; + + protected _body!: BuiltInBody; + /** 相对于节点的偏移 */ + protected _center: Vec3 = new Vec3(0, 0, 0); + + public _setBody (body: BuiltInBody) { + this._body = body; + } + public setCenter (center: Vec3): void { + this._center = center; + /** TODO :更新形状在世界的位置并需要加上节点的世界位置 */ + + } + + public getUserData () { + + } + public setUserData (data: any): void { + + } + public getCollisionResponse (): boolean { + return false; + } + public setCollisionResponse (value: boolean): void { + } + + public setPosition (pos: Vec3) { + this._shape!.translate(pos, this._center); + } + + public setScale (scale: Vec3): void { + this._shape!.scale(scale); + } + + public setRotation (rotation: Quat): void { + this._shape!.rotate(rotation); + } +} + +export class BuiltInBoxShape extends BuiltInShape { + private _box: BoxShape; + + constructor (size: Vec3) { + super(); + this._box = new BoxShape( + this._center.x, + this._center.y, + this._center.z, + size.x * 0.5, + size.y * 0.5, + size.z * 0.5, + ); + this._shape = this._box as unknown as IShapeTransform; + } + public setSize (size: Vec3) { + this._box.size = size; + } +} + +export class BuiltInSphereShape extends BuiltInShape { + private _sphere: SphereShape; + public get radius () { return this._sphere.r; } + constructor (radius) { + super(); + this._sphere = new SphereShape(this._center.x, this._center.y, this._center.z, radius); + this._shape = this._sphere as unknown as IShapeTransform; + } + public setRadius (radius: number) { + this._sphere.r = radius; + } +} diff --git a/cocos/3d/physics/cocos/built-in-shape/BoxShape.ts b/cocos/3d/physics/cocos/built-in-shape/BoxShape.ts new file mode 100644 index 0000000000..52bf8ced50 --- /dev/null +++ b/cocos/3d/physics/cocos/built-in-shape/BoxShape.ts @@ -0,0 +1,37 @@ +import { mat3, quat, vec3 } from '../../../../core/vmath'; +import { obb } from '../../../geom-utils'; +import { IShapeTransform } from '../built-in-interface'; + +export class BoxShape extends obb implements IShapeTransform { + private _originHalfExtents: vec3; + public set size (size: vec3) { + vec3.scale(this._originHalfExtents, size, 0.5); + } + constructor (px = 0, py = 0, pz = 0, + hw = 1, hh = 1, hl = 1, + ox_1 = 1, ox_2 = 0, ox_3 = 0, + oy_1 = 0, oy_2 = 1, oy_3 = 0, + oz_1 = 0, oz_2 = 0, oz_3 = 1) { + super( + px, py, pz, + hw, hh, hl, + ox_1, ox_2, ox_3, + oy_1, oy_2, oy_3, + oz_1, oz_2, oz_3); + + this._originHalfExtents = vec3.create(hw, hh, hl); + } + + /** 根据世界pos和偏移offset设置obb的center */ + public translate (pos: vec3, offset: vec3) { + vec3.add(this.center, pos, offset); + } + /** 缩放obb */ + public scale (scale: vec3) { + vec3.mul(this.halfExtents, this._originHalfExtents, scale); + } + /** 根据四元数设置obb朝向 */ + public rotate (rot: quat) { + mat3.fromQuat(this.orientation, rot); + } +} diff --git a/cocos/3d/physics/cocos/built-in-shape/SphereShape.ts b/cocos/3d/physics/cocos/built-in-shape/SphereShape.ts new file mode 100644 index 0000000000..c410102e42 --- /dev/null +++ b/cocos/3d/physics/cocos/built-in-shape/SphereShape.ts @@ -0,0 +1,20 @@ +import { vec3 } from '../../../../core/vmath'; +import { sphere } from '../../../geom-utils'; +import { IShapeTransform } from '../built-in-interface'; + +export class SphereShape extends sphere implements IShapeTransform { + constructor (cx = 0, cy = 0, cz = 0, r = 1) { + super(cx, cy, cz, r); + } + + /** 根据世界pos和偏移offset设置obb的center */ + public translate (pos: vec3, offset: vec3) { + vec3.add(this.c, pos, offset); + } + /** 根据缩放向量缩放球 */ + public scale (scale: vec3) { + this.r = this.r * Math.max(Math.max(scale.x, scale.y), scale.z); + } + /** 根据四元数设置朝向 */ + public rotate (...args: any) { } +} diff --git a/cocos/3d/physics/cocos/built-in-world.ts b/cocos/3d/physics/cocos/built-in-world.ts new file mode 100644 index 0000000000..ec7ee07e0e --- /dev/null +++ b/cocos/3d/physics/cocos/built-in-world.ts @@ -0,0 +1,87 @@ +import { Vec3 } from '../../../core'; +import { AfterStepCallback, BeforeStepCallback, IRaycastOptions, PhysicsWorldBase, RigidBodyBase } from '../api'; +import { RaycastResult } from '../raycast-result'; +import { BuiltInBody } from './built-in-body'; + +/** + * Built-in collision system, intended for use as a + * efficient discrete collision detector, + * not a full physical simulator + */ +export class BuiltInWorld implements PhysicsWorldBase { + private _bodies: RigidBodyBase[] = []; + private _customBeforeStepListener: BeforeStepCallback[] = []; + private _customAfterStepListener: AfterStepCallback[] = []; + constructor () { + } + public step (deltaTime: number): void { + /** 执行 before step */ + this._customBeforeStepListener.forEach((fx) => fx()); + + /** 进行物理检测 */ + // tslint:disable-next-line: prefer-for-of + for (let i = 0; i < this._bodies.length; i++) { + const bodyA = this._bodies[i] as BuiltInBody; + // tslint:disable-next-line:prefer-for-of + for (let j = i + 1; j < this._bodies.length; j++) { + const bodyB = this._bodies[j] as BuiltInBody; + + // TODO: 判断是否碰撞 + if ((bodyA.collisionFilterGroup & bodyB.collisionFilterMask) === 0 || + (bodyB.collisionFilterGroup & bodyA.collisionFilterMask) === 0) { + continue; + } + + bodyA.intersects(bodyB); + } + + this._customAfterStepListener.forEach((fx) => fx()); + } + + /** 执行 after step */ + this._customAfterStepListener.forEach((fx) => fx()); + } + public addBody (body: RigidBodyBase) { + this._bodies.push(body); + } + public removeBody (body: RigidBodyBase) { + const i = this._bodies.indexOf(body); + if (i >= 0) { + this._bodies.splice(i, 1); + } + } + + public addBeforeStep (cb: BeforeStepCallback) { + this._customBeforeStepListener.push(cb); + } + + public removeBeforeStep (cb: BeforeStepCallback) { + const i = this._customBeforeStepListener.indexOf(cb); + if (i < 0) { + return; + } + this._customBeforeStepListener.splice(i, 1); + } + + public addAfterStep (cb: AfterStepCallback) { + this._customAfterStepListener.push(cb); + } + + public removeAfterStep (cb: AfterStepCallback) { + const i = this._customAfterStepListener.indexOf(cb); + if (i < 0) { + return; + } + this._customAfterStepListener.splice(i, 1); + } + public raycastClosest (from: Vec3, to: Vec3, options: IRaycastOptions, result: RaycastResult): boolean { + return false; + } + public raycastAny (from: Vec3, to: Vec3, options: IRaycastOptions, result: RaycastResult): boolean { + return false; + } + public raycastAll (from: Vec3, to: Vec3, options: IRaycastOptions, callback: (result: RaycastResult) => void): boolean { + return false; + } + +} diff --git a/cocos/3d/physics/impl-selector.ts b/cocos/3d/physics/impl-selector.ts index a66f447cfc..7454d19917 100644 --- a/cocos/3d/physics/impl-selector.ts +++ b/cocos/3d/physics/impl-selector.ts @@ -13,3 +13,9 @@ export { // AmmoSphereShape as SphereShape, // AmmoWorld as PhysicsWorld, // } from './ammo-impl'; + +// 增加built-in物理模块 +// export { BuiltInBody as RigidBody } from './cocos/built-in-body'; +// export { BuiltInBoxShape as BoxShape } from './cocos/built-in-shape'; +// export { BuiltInSphereShape as SphereShape } from './cocos/built-in-shape'; +// export { BuiltInWorld as PhysicsWorld } from './cocos/built-in-world'; diff --git a/cocos/3d/physics/physic-enum.ts b/cocos/3d/physics/physic-enum.ts index 2c19953b8e..33c4617afc 100644 --- a/cocos/3d/physics/physic-enum.ts +++ b/cocos/3d/physics/physic-enum.ts @@ -8,3 +8,15 @@ export enum ETransformSource { SCENE = 0, PHYSIC = 1, } + +export enum EShapeType { + SHAPE_RAY = (1 << 0), + SHAPE_LINE = (1 << 1), + SHAPE_SPHERE = (1 << 2), + SHAPE_AABB = (1 << 3), + SHAPE_OBB = (1 << 4), + SHAPE_PLANE = (1 << 5), + SHAPE_TRIANGLE = (1 << 6), + SHAPE_FRUSTUM = (1 << 7), + SHAPE_FRUSTUM_ACCURATE = (1 << 8), +}