mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 03:22:39 +08:00
136 lines
3.4 KiB
TypeScript
136 lines
3.4 KiB
TypeScript
/**
|
|
* @packageDocumentation
|
|
* @module geometry
|
|
*/
|
|
|
|
import { Vec3, Quat, Mat4, absMaxComponent } from "../math";
|
|
import enums from "./enums";
|
|
import { IVec3Like, IQuatLike } from "../math/type-define";
|
|
|
|
/**
|
|
* @en
|
|
* Basic Geometry: capsule.
|
|
* @zh
|
|
* 基础几何,胶囊体。
|
|
*/
|
|
export class capsule {
|
|
|
|
/**
|
|
* @en
|
|
* Gets the type of the shape.
|
|
* @zh
|
|
* 获取形状的类型。
|
|
*/
|
|
get type () {
|
|
return this._type;
|
|
}
|
|
|
|
protected readonly _type: number;
|
|
|
|
/**
|
|
* @en
|
|
* Capsule sphere radius.
|
|
* @zh
|
|
* 胶囊体球部半径。
|
|
*/
|
|
radius: number;
|
|
|
|
/**
|
|
* @en
|
|
* The distance between the center point of the capsule and the center of the sphere.
|
|
* @zh
|
|
* 胶囊体中心点和球部圆心的距离。
|
|
*/
|
|
halfHeight: number;
|
|
|
|
/**
|
|
* @en
|
|
* Local orientation of capsule [0,1,2] => [x,y,z].
|
|
* @zh
|
|
* 胶囊体的本地朝向,映射关系 [0,1,2] => [x,y,z]。
|
|
*/
|
|
axis: number;
|
|
|
|
/**
|
|
* @en
|
|
* The origin of the capsule.
|
|
* @zh
|
|
* 胶囊体的原点。
|
|
*/
|
|
readonly center: Vec3;
|
|
|
|
/**
|
|
* @en
|
|
* The rotation of the capsule.
|
|
* @zh
|
|
* 胶囊体的旋转。
|
|
*/
|
|
readonly rotation: Quat;
|
|
|
|
/** cache, local center of ellipse */
|
|
readonly ellipseCenter0: Vec3;
|
|
readonly ellipseCenter1: Vec3;
|
|
|
|
constructor (radius: number = 0.5, halfHeight: number = 0.5, axis = 1) {
|
|
this._type = enums.SHAPE_CAPSULE;
|
|
this.radius = radius;
|
|
this.halfHeight = halfHeight;
|
|
this.axis = axis;
|
|
|
|
this.center = new Vec3();
|
|
this.rotation = new Quat();
|
|
|
|
this.ellipseCenter0 = new Vec3(0, halfHeight, 0);
|
|
this.ellipseCenter1 = new Vec3(0, -halfHeight, 0);
|
|
this.updateCache();
|
|
}
|
|
|
|
/**
|
|
* @en
|
|
* Transform this capsule.
|
|
* @zh
|
|
* 变换此胶囊体。
|
|
*/
|
|
transform (m: Mat4, pos: IVec3Like, rot: IQuatLike, scale: IVec3Like, out: capsule) {
|
|
const ws = scale;
|
|
const s = absMaxComponent(ws as Vec3);
|
|
out.radius = this.radius * Math.abs(s);
|
|
|
|
const halfTotalWorldHeight = (this.halfHeight + this.radius) * Math.abs(ws.y);
|
|
let halfWorldHeight = halfTotalWorldHeight - out.radius;
|
|
if (halfWorldHeight < 0) halfWorldHeight = 0;
|
|
out.halfHeight = halfWorldHeight;
|
|
|
|
Vec3.transformMat4(out.center, this.center, m);
|
|
Quat.multiply(out.rotation, this.rotation, rot);
|
|
out.updateCache();
|
|
}
|
|
|
|
updateCache () {
|
|
this.updateLocalCenter();
|
|
Vec3.transformQuat(this.ellipseCenter0, this.ellipseCenter0, this.rotation);
|
|
Vec3.transformQuat(this.ellipseCenter1, this.ellipseCenter1, this.rotation);
|
|
this.ellipseCenter0.add(this.center);
|
|
this.ellipseCenter1.add(this.center);
|
|
}
|
|
|
|
updateLocalCenter () {
|
|
const halfHeight = this.halfHeight;
|
|
const axis = this.axis;
|
|
switch (axis) {
|
|
case 0:
|
|
this.ellipseCenter0.set(halfHeight, 0, 0);
|
|
this.ellipseCenter1.set(-halfHeight, 0, 0);
|
|
break;
|
|
case 1:
|
|
this.ellipseCenter0.set(0, halfHeight, 0);
|
|
this.ellipseCenter1.set(0, -halfHeight, 0);
|
|
break;
|
|
case 2:
|
|
this.ellipseCenter0.set(0, 0, halfHeight);
|
|
this.ellipseCenter1.set(0, 0, -halfHeight);
|
|
break;
|
|
}
|
|
}
|
|
|
|
} |