mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
builtin_capsule_collider (#5970)
* add bouding capsule * add builtin capsule collider * add apater for capsule shape * fix circular dependency * fix typing * fix capsule_capsule algorithm * api doc tweaks * fix ci
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { Vec3, Quat, Mat4, absMaxComponent } from "../math";
|
||||
import enums from "./enums";
|
||||
import { IVec3Like, IQuatLike } from "../math/type-define";
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 基础几何,胶囊体
|
||||
*/
|
||||
export class capsule {
|
||||
|
||||
protected _type: number;
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 胶囊体球部半径
|
||||
*/
|
||||
radius: number;
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 胶囊体中心点和球部圆心的距离
|
||||
*/
|
||||
halfHeight: number;
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 胶囊体的朝向,映射关系 [0,1,2] => [x,y,z]
|
||||
*/
|
||||
axis: number;
|
||||
|
||||
readonly center: Vec3;
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -117,3 +117,29 @@ export function pt_point_obb (out: Vec3, point: Vec3, obb_: obb): Vec3 {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function pt_point_line (out: Vec3, point: Vec3, start: Vec3, end: Vec3) {
|
||||
Vec3.subtract(X, start, end);
|
||||
const dir = X;
|
||||
const dirSquaredLength = Vec3.lengthSqr(dir);
|
||||
|
||||
if (dirSquaredLength == 0) {
|
||||
// The point is at the segment start.
|
||||
Vec3.copy(out, start);
|
||||
} else {
|
||||
// Calculate the projection of the point onto the line extending through the segment.
|
||||
Vec3.subtract(X, point, start);
|
||||
const t = Vec3.dot(X, dir) / dirSquaredLength;
|
||||
|
||||
if (t < 0) {
|
||||
// The point projects beyond the segment start.
|
||||
Vec3.copy(out, start);
|
||||
} else if (t > 1) {
|
||||
// The point projects beyond the segment end.
|
||||
Vec3.copy(out, end);
|
||||
} else {
|
||||
// The point projects between the start and end of the segment.
|
||||
Vec3.scaleAndAdd(out, start, dir, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,4 +16,5 @@ export default {
|
||||
SHAPE_TRIANGLE: (1 << 6),
|
||||
SHAPE_FRUSTUM: (1 << 7),
|
||||
SHAPE_FRUSTUM_ACCURATE: (1 << 8),
|
||||
SHAPE_CAPSULE: (1 << 9),
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@ export { default as triangle } from './triangle';
|
||||
export { default as sphere } from './sphere';
|
||||
export { default as aabb } from './aabb';
|
||||
export { default as obb } from './obb';
|
||||
export { capsule } from './capsule';
|
||||
export { frustum } from './frustum';
|
||||
export { Keyframe, AnimationCurve } from './curve';
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* @category geometry
|
||||
*/
|
||||
|
||||
import { Mat3, Vec3 } from '../math';
|
||||
import { Mat3, Vec3, EPSILON, Vec2 } from '../math';
|
||||
import aabb from './aabb';
|
||||
import * as distance from './distance';
|
||||
import enums from './enums';
|
||||
@@ -14,6 +14,7 @@ import plane from './plane';
|
||||
import ray from './ray';
|
||||
import sphere from './sphere';
|
||||
import triangle from './triangle';
|
||||
import { capsule } from './capsule';
|
||||
|
||||
// tslint:disable:only-arrow-functions
|
||||
// tslint:disable:one-variable-per-declaration
|
||||
@@ -212,6 +213,84 @@ const ray_obb = (function () {
|
||||
};
|
||||
})();
|
||||
|
||||
const ray_capsule = (function () {
|
||||
const v3_0 = new Vec3();
|
||||
const v3_1 = new Vec3();
|
||||
const v3_2 = new Vec3();
|
||||
const v3_3 = new Vec3();
|
||||
const v3_4 = new Vec3();
|
||||
const v3_5 = new Vec3();
|
||||
const v3_6 = new Vec3();
|
||||
const sphere_0 = new sphere();
|
||||
return function (ray: ray, capsule: capsule) {
|
||||
const radiusSqr = capsule.radius * capsule.radius;
|
||||
var vRayNorm = Vec3.normalize(v3_0, ray.d);
|
||||
var A = capsule.ellipseCenter0;
|
||||
var B = capsule.ellipseCenter1;
|
||||
var BA = Vec3.subtract(v3_1, B, A);
|
||||
if (BA.equals(Vec3.ZERO)) {
|
||||
sphere_0.radius = capsule.radius;
|
||||
sphere_0.center.set(capsule.ellipseCenter0);
|
||||
return intersect.ray_sphere(ray, sphere_0);
|
||||
}
|
||||
|
||||
var O = ray.o;
|
||||
var OA = Vec3.subtract(v3_2, O, A);
|
||||
var VxBA = Vec3.cross(v3_3, vRayNorm, BA);
|
||||
var a = VxBA.lengthSqr();
|
||||
if (a == 0) {
|
||||
sphere_0.radius = capsule.radius;
|
||||
var BO = Vec3.subtract(v3_4, B, O);
|
||||
if (OA.lengthSqr() < BO.lengthSqr()) {
|
||||
sphere_0.center.set(capsule.ellipseCenter0);
|
||||
} else {
|
||||
sphere_0.center.set(capsule.ellipseCenter1);
|
||||
}
|
||||
return intersect.ray_sphere(ray, sphere_0);
|
||||
}
|
||||
|
||||
var OAxBA = Vec3.cross(v3_4, OA, BA);
|
||||
var ab2 = BA.lengthSqr();
|
||||
var b = 2 * Vec3.dot(VxBA, OAxBA);
|
||||
var c = OAxBA.lengthSqr() - (radiusSqr * ab2);
|
||||
var d = b * b - 4 * a * c;
|
||||
|
||||
if (d < 0) return 0;
|
||||
|
||||
var t = (-b - Math.sqrt(d)) / (2 * a);
|
||||
if (t < 0) {
|
||||
sphere_0.radius = capsule.radius;
|
||||
var BO = Vec3.subtract(v3_5, B, O);
|
||||
if (OA.lengthSqr() < BO.lengthSqr()) {
|
||||
sphere_0.center.set(capsule.ellipseCenter0);
|
||||
} else {
|
||||
sphere_0.center.set(capsule.ellipseCenter1);
|
||||
}
|
||||
return intersect.ray_sphere(ray, sphere_0);
|
||||
} else {
|
||||
//Limit intersection between the bounds of the cylinder's end caps.
|
||||
var iPos = Vec3.scaleAndAdd(v3_5, ray.o, vRayNorm, t);
|
||||
var iPosLen = Vec3.subtract(v3_6, iPos, A);
|
||||
var tLimit = Vec3.dot(iPosLen, BA) / ab2;
|
||||
|
||||
if (tLimit >= 0 && tLimit <= 1) {
|
||||
return t;
|
||||
} else if (tLimit < 0) {
|
||||
sphere_0.radius = capsule.radius;
|
||||
sphere_0.center.set(capsule.ellipseCenter0);
|
||||
return intersect.ray_sphere(ray, sphere_0);
|
||||
} else if (tLimit > 1) {
|
||||
sphere_0.radius = capsule.radius;
|
||||
sphere_0.center.set(capsule.ellipseCenter1);
|
||||
return intersect.ray_sphere(ray, sphere_0);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
})();
|
||||
|
||||
/**
|
||||
* line-plane intersect<br/>
|
||||
* 线段与平面的相交性检测。
|
||||
@@ -741,6 +820,68 @@ const obb_obb = (function () {
|
||||
};
|
||||
})();
|
||||
|
||||
|
||||
// https://github.com/diku-dk/bvh-tvcg18/blob/1fd3348c17bc8cf3da0b4ae60fdb8f2aa90a6ff0/FOUNDATION/GEOMETRY/GEOMETRY/include/overlap/geometry_overlap_obb_capsule.h
|
||||
/**
|
||||
* 方向包围盒和胶囊体的重叠检测
|
||||
* @param obb 方向包围盒
|
||||
* @param capsule 胶囊体
|
||||
*/
|
||||
const obb_capsule = (function () {
|
||||
const sphere_0 = new sphere();
|
||||
const v3_0 = new Vec3();
|
||||
const v3_1 = new Vec3();
|
||||
const v3_2 = new Vec3();
|
||||
const v3_verts8 = new Array<Vec3>(8);
|
||||
for (let i = 0; i < 8; i++) { v3_verts8[i] = new Vec3(); }
|
||||
const v3_axis7 = new Array<Vec3>(7);
|
||||
for (let i = 0; i < 7; i++) { v3_axis7[i] = new Vec3(); }
|
||||
return function (obb: obb, capsule: capsule) {
|
||||
const h = Vec3.squaredDistance(capsule.ellipseCenter0, capsule.ellipseCenter1);
|
||||
if (h == 0) {
|
||||
sphere_0.radius = capsule.radius;
|
||||
sphere_0.center.set(capsule.ellipseCenter0);
|
||||
return intersect.sphere_obb(sphere_0, obb);
|
||||
} else {
|
||||
v3_0.x = obb.orientation.m00;
|
||||
v3_0.y = obb.orientation.m01;
|
||||
v3_0.z = obb.orientation.m02;
|
||||
v3_1.x = obb.orientation.m03;
|
||||
v3_1.y = obb.orientation.m04;
|
||||
v3_1.z = obb.orientation.m05;
|
||||
v3_2.x = obb.orientation.m06;
|
||||
v3_2.y = obb.orientation.m07;
|
||||
v3_2.z = obb.orientation.m08;
|
||||
getOBBVertices(obb.center, obb.halfExtents, v3_0, v3_1, v3_2, v3_verts8);
|
||||
|
||||
const axes = v3_axis7;
|
||||
const a0 = Vec3.copy(axes[0], v3_0);
|
||||
const a1 = Vec3.copy(axes[1], v3_1);
|
||||
const a2 = Vec3.copy(axes[2], v3_2);
|
||||
const B = Vec3.subtract(axes[3], capsule.ellipseCenter0, capsule.ellipseCenter1);
|
||||
Vec3.cross(axes[4], a0, B);
|
||||
Vec3.cross(axes[5], a1, B);
|
||||
Vec3.cross(axes[6], a2, B);
|
||||
for (let i = 0; i < 7; ++i) {
|
||||
const a = getInterval(v3_verts8, axes[i]);
|
||||
const d0 = Vec3.dot(axes[i], capsule.ellipseCenter0);
|
||||
const d1 = Vec3.dot(axes[i], capsule.ellipseCenter1);
|
||||
const d0_min = d0 - capsule.radius;
|
||||
const d0_max = d0 + capsule.radius;
|
||||
const d1_min = d1 - capsule.radius;
|
||||
const d1_max = d1 + capsule.radius;
|
||||
const d_max = Math.max(d0_min, d1_min, d0_max, d1_max);
|
||||
const d_min = Math.min(d0_min, d1_min, d0_max, d1_max);
|
||||
|
||||
if (d_min > a[1] || a[0] > d_max) {
|
||||
return 0; // Seperating axis found
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
/**
|
||||
* sphere-plane intersect, not necessarily faster than obb-plane<br/>
|
||||
* due to the length calculation of the plane normal to factor out<br/>
|
||||
@@ -849,12 +990,123 @@ const sphere_obb = (function () {
|
||||
};
|
||||
})();
|
||||
|
||||
const sphere_capsule = (function () {
|
||||
const v3_0 = new Vec3();
|
||||
const v3_1 = new Vec3();
|
||||
return function (sphere: sphere, capsule: capsule) {
|
||||
const r = sphere.radius + capsule.radius;
|
||||
const squaredR = r * r;
|
||||
const h = Vec3.squaredDistance(capsule.ellipseCenter0, capsule.ellipseCenter1);
|
||||
if (h == 0) {
|
||||
return Vec3.squaredDistance(sphere.center, capsule.center) < squaredR;
|
||||
} else {
|
||||
Vec3.subtract(v3_0, sphere.center, capsule.ellipseCenter0);
|
||||
Vec3.subtract(v3_1, capsule.ellipseCenter1, capsule.ellipseCenter0);
|
||||
const t = Vec3.dot(v3_0, v3_1) / h;
|
||||
if (t < 0) {
|
||||
return Vec3.squaredDistance(sphere.center, capsule.ellipseCenter0) < squaredR;
|
||||
} else if (t > 1) {
|
||||
return Vec3.squaredDistance(sphere.center, capsule.ellipseCenter1) < squaredR;
|
||||
} else {
|
||||
Vec3.scaleAndAdd(v3_0, capsule.ellipseCenter0, v3_1, t);
|
||||
return Vec3.squaredDistance(sphere.center, v3_0) < squaredR;
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
// http://www.geomalgorithms.com/a07-_distance.html
|
||||
const capsule_capsule = (function () {
|
||||
const v3_0 = new Vec3();
|
||||
const v3_1 = new Vec3();
|
||||
const v3_2 = new Vec3();
|
||||
const v3_3 = new Vec3();
|
||||
const v3_4 = new Vec3();
|
||||
const v3_5 = new Vec3();
|
||||
return function capsule_capsule (capsuleA: capsule, capsuleB: capsule) {
|
||||
let u = Vec3.subtract(v3_0, capsuleA.ellipseCenter1, capsuleA.ellipseCenter0);
|
||||
let v = Vec3.subtract(v3_1, capsuleB.ellipseCenter1, capsuleB.ellipseCenter0);
|
||||
let w = Vec3.subtract(v3_2, capsuleA.ellipseCenter0, capsuleB.ellipseCenter0);
|
||||
let a = Vec3.dot(u, u); // always >= 0
|
||||
let b = Vec3.dot(u, v);
|
||||
let c = Vec3.dot(v, v); // always >= 0
|
||||
let d = Vec3.dot(u, w);
|
||||
let e = Vec3.dot(v, w);
|
||||
let D = a * c - b * b; // always >= 0
|
||||
let sc: number;
|
||||
let sN: number;
|
||||
let sD = D; // sc = sN / sD, default sD = D >= 0
|
||||
let tc: number;
|
||||
let tN: number;
|
||||
let tD = D; // tc = tN / tD, default tD = D >= 0
|
||||
|
||||
// compute the line parameters of the two closest points
|
||||
if (D < EPSILON) { // the lines are almost parallel
|
||||
sN = 0.0; // force using point P0 on segment S1
|
||||
sD = 1.0; // to prevent possible division by 0.0 later
|
||||
tN = e;
|
||||
tD = c;
|
||||
}
|
||||
else { // get the closest points on the infinite lines
|
||||
sN = (b * e - c * d);
|
||||
tN = (a * e - b * d);
|
||||
if (sN < 0.0) { // sc < 0 => the s=0 edge is visible
|
||||
sN = 0.0;
|
||||
tN = e;
|
||||
tD = c;
|
||||
}
|
||||
else if (sN > sD) { // sc > 1 => the s=1 edge is visible
|
||||
sN = sD;
|
||||
tN = e + b;
|
||||
tD = c;
|
||||
}
|
||||
}
|
||||
|
||||
if (tN < 0.0) { // tc < 0 => the t=0 edge is visible
|
||||
tN = 0.0;
|
||||
// recompute sc for this edge
|
||||
if (-d < 0.0)
|
||||
sN = 0.0;
|
||||
else if (-d > a)
|
||||
sN = sD;
|
||||
else {
|
||||
sN = -d;
|
||||
sD = a;
|
||||
}
|
||||
}
|
||||
else if (tN > tD) { // tc > 1 => the t=1 edge is visible
|
||||
tN = tD;
|
||||
// recompute sc for this edge
|
||||
if ((-d + b) < 0.0)
|
||||
sN = 0;
|
||||
else if ((-d + b) > a)
|
||||
sN = sD;
|
||||
else {
|
||||
sN = (-d + b);
|
||||
sD = a;
|
||||
}
|
||||
}
|
||||
// finally do the division to get sc and tc
|
||||
sc = (Math.abs(sN) < EPSILON ? 0.0 : sN / sD);
|
||||
tc = (Math.abs(tN) < EPSILON ? 0.0 : tN / tD);
|
||||
|
||||
// get the difference of the two closest points
|
||||
var dP = v3_3;
|
||||
dP.set(w);
|
||||
dP.add(Vec3.multiplyScalar(v3_4, u, sc));
|
||||
dP.subtract(Vec3.multiplyScalar(v3_5, v, tc));
|
||||
const radius = capsuleA.radius + capsuleB.radius;
|
||||
return dP.lengthSqr() < radius * radius;
|
||||
}
|
||||
})();
|
||||
|
||||
const intersect = {
|
||||
ray_sphere,
|
||||
ray_aabb,
|
||||
ray_obb,
|
||||
ray_plane,
|
||||
ray_triangle,
|
||||
ray_capsule,
|
||||
|
||||
line_sphere,
|
||||
line_aabb,
|
||||
@@ -868,6 +1120,7 @@ const intersect = {
|
||||
sphere_plane,
|
||||
sphere_frustum,
|
||||
sphere_frustum_accurate,
|
||||
sphere_capsule,
|
||||
|
||||
aabb_aabb,
|
||||
aabb_obb,
|
||||
@@ -880,6 +1133,9 @@ const intersect = {
|
||||
obb_frustum,
|
||||
obb_frustum_accurate,
|
||||
obb_point,
|
||||
obb_capsule,
|
||||
|
||||
capsule_capsule,
|
||||
|
||||
/**
|
||||
* @zh
|
||||
@@ -901,6 +1157,7 @@ intersect[enums.SHAPE_RAY | enums.SHAPE_AABB] = ray_aabb;
|
||||
intersect[enums.SHAPE_RAY | enums.SHAPE_OBB] = ray_obb;
|
||||
intersect[enums.SHAPE_RAY | enums.SHAPE_PLANE] = ray_plane;
|
||||
intersect[enums.SHAPE_RAY | enums.SHAPE_TRIANGLE] = ray_triangle;
|
||||
intersect[enums.SHAPE_RAY | enums.SHAPE_CAPSULE] = ray_capsule;
|
||||
|
||||
intersect[enums.SHAPE_LINE | enums.SHAPE_SPHERE] = line_sphere;
|
||||
intersect[enums.SHAPE_LINE | enums.SHAPE_AABB] = line_aabb;
|
||||
@@ -914,6 +1171,7 @@ intersect[enums.SHAPE_SPHERE | enums.SHAPE_OBB] = sphere_obb;
|
||||
intersect[enums.SHAPE_SPHERE | enums.SHAPE_PLANE] = sphere_plane;
|
||||
intersect[enums.SHAPE_SPHERE | enums.SHAPE_FRUSTUM] = sphere_frustum;
|
||||
intersect[enums.SHAPE_SPHERE | enums.SHAPE_FRUSTUM_ACCURATE] = sphere_frustum_accurate;
|
||||
intersect[enums.SHAPE_SPHERE | enums.SHAPE_CAPSULE] = sphere_capsule;
|
||||
|
||||
intersect[enums.SHAPE_AABB] = aabb_aabb;
|
||||
intersect[enums.SHAPE_AABB | enums.SHAPE_OBB] = aabb_obb;
|
||||
@@ -925,5 +1183,8 @@ intersect[enums.SHAPE_OBB] = obb_obb;
|
||||
intersect[enums.SHAPE_OBB | enums.SHAPE_PLANE] = obb_plane;
|
||||
intersect[enums.SHAPE_OBB | enums.SHAPE_FRUSTUM] = obb_frustum;
|
||||
intersect[enums.SHAPE_OBB | enums.SHAPE_FRUSTUM_ACCURATE] = obb_frustum_accurate;
|
||||
intersect[enums.SHAPE_OBB | enums.SHAPE_CAPSULE] = obb_capsule;
|
||||
|
||||
intersect[enums.SHAPE_CAPSULE] = capsule_capsule;
|
||||
|
||||
export default intersect;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { IVec3Like } from "./type-define";
|
||||
|
||||
/**
|
||||
* @category core/math
|
||||
*/
|
||||
@@ -203,3 +205,24 @@ export function pingPong (t: number, length: number) {
|
||||
export function inverseLerp (from: number, to: number, value: number) {
|
||||
return (value - from) / (to - from);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 对所有分量的绝对值进行比较大小,返回绝对值最大的分量。
|
||||
* @param v 类 Vec3 结构
|
||||
* @returns 绝对值最大的分量
|
||||
*/
|
||||
export function absMaxComponent (v: IVec3Like) {
|
||||
if (Math.abs(v.x) > Math.abs(v.y)) {
|
||||
if (Math.abs(v.x) > Math.abs(v.z)) {
|
||||
return v.x;
|
||||
} else {
|
||||
return v.z;
|
||||
}
|
||||
} else {
|
||||
if (Math.abs(v.y) > Math.abs(v.z)) {
|
||||
return v.y;
|
||||
} else {
|
||||
return v.z;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { instantiate } from '../framework/Physics-selector';
|
||||
import { BuiltInWorld } from './builtin-world';
|
||||
import { BuiltinBoxShape } from './shapes/builtin-box-shape';
|
||||
import { BuiltinSphereShape } from './shapes/builtin-sphere-shape';
|
||||
import { BuiltinCapsuleShape } from './shapes/builtin-capsule-shape';
|
||||
|
||||
if (CC_PHYSICS_BUILTIN) {
|
||||
instantiate(
|
||||
@@ -13,5 +14,6 @@ if (CC_PHYSICS_BUILTIN) {
|
||||
BuiltinSphereShape,
|
||||
null,
|
||||
BuiltInWorld,
|
||||
BuiltinCapsuleShape
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
|
||||
import { BuiltinShape } from './builtin-shape';
|
||||
import { ICapsuleShape } from '../../spec/i-physics-shape';
|
||||
import { ECapsuleDirection, CapsuleColliderComponent } from '../../framework/components/collider/capsule-collider-component';
|
||||
import { capsule } from '../../../core/geom-utils';
|
||||
|
||||
export class BuiltinCapsuleShape extends BuiltinShape implements ICapsuleShape {
|
||||
|
||||
get localCapsule () {
|
||||
return this._localShape as capsule;
|
||||
}
|
||||
|
||||
get worldCapsule () {
|
||||
return this._worldShape as capsule;
|
||||
}
|
||||
|
||||
get capsuleCollider () {
|
||||
return this.collider as CapsuleColliderComponent;
|
||||
}
|
||||
|
||||
constructor (radius: number, height: number, direction = ECapsuleDirection.Y_AXIS) {
|
||||
super();
|
||||
const halfHeight = (height - radius * 2) / 2;
|
||||
const h = halfHeight < 0 ? 0 : halfHeight;
|
||||
this._localShape = new capsule(radius, h, direction);
|
||||
this._worldShape = new capsule(radius, h, direction);
|
||||
}
|
||||
|
||||
set radius (v: number) {
|
||||
this.localCapsule.radius = v;
|
||||
|
||||
const halfTotalHeight = this.capsuleCollider.height / 2;
|
||||
let halfHeight = halfTotalHeight - v;
|
||||
if (halfHeight < 0) halfHeight = 0;
|
||||
this.localCapsule.halfHeight = halfHeight;
|
||||
this.localCapsule.updateCache();
|
||||
|
||||
this.transform(
|
||||
this._sharedBody.node.worldMatrix,
|
||||
this._sharedBody.node.worldPosition,
|
||||
this._sharedBody.node.worldRotation,
|
||||
this._sharedBody.node.worldScale
|
||||
);
|
||||
}
|
||||
|
||||
set height (v: number) {
|
||||
const hf = v / 2 - this.capsuleCollider.radius;
|
||||
this.localCapsule.halfHeight = hf;
|
||||
this.localCapsule.updateCache();
|
||||
|
||||
this.transform(
|
||||
this._sharedBody.node.worldMatrix,
|
||||
this._sharedBody.node.worldPosition,
|
||||
this._sharedBody.node.worldRotation,
|
||||
this._sharedBody.node.worldScale
|
||||
);
|
||||
}
|
||||
|
||||
set direction (v: ECapsuleDirection) {
|
||||
this.localCapsule.axis = v;
|
||||
this.localCapsule.updateCache();
|
||||
|
||||
this.worldCapsule.axis = v;
|
||||
this.worldCapsule.updateCache();
|
||||
|
||||
this.transform(
|
||||
this._sharedBody.node.worldMatrix,
|
||||
this._sharedBody.node.worldPosition,
|
||||
this._sharedBody.node.worldRotation,
|
||||
this._sharedBody.node.worldScale
|
||||
);
|
||||
}
|
||||
|
||||
onLoad () {
|
||||
super.onLoad();
|
||||
this.radius = this.capsuleCollider.radius;
|
||||
this.direction = this.capsuleCollider.direction;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import { CannonSphereShape } from '../cannon/shapes/cannon-sphere-shape';
|
||||
import { BuiltInWorld } from '../cocos/builtin-world';
|
||||
import { BuiltinBoxShape } from '../cocos/shapes/builtin-box-shape';
|
||||
import { BuiltinSphereShape } from '../cocos/shapes/builtin-sphere-shape';
|
||||
import { BuiltinCapsuleShape } from '../cocos/shapes/builtin-capsule-shape';
|
||||
|
||||
// Ammo
|
||||
import { AmmoRigidBody } from '../ammo/ammo-rigid-body';
|
||||
@@ -23,14 +24,20 @@ export let BoxShape: typeof CannonBoxShape | typeof BuiltinBoxShape | typeof Amm
|
||||
export let SphereShape: typeof CannonSphereShape | typeof BuiltinSphereShape | typeof AmmoSphereShape;
|
||||
export let RigidBody: typeof CannonRigidBody | null | typeof AmmoRigidBody;
|
||||
export let PhysicsWorld: typeof CannonWorld | typeof BuiltInWorld | typeof AmmoWorld;
|
||||
export let CapsuleShape: typeof BuiltinCapsuleShape;
|
||||
|
||||
export function instantiate (
|
||||
boxShape: typeof BoxShape,
|
||||
sphereShape: typeof SphereShape,
|
||||
body: typeof RigidBody,
|
||||
world: typeof PhysicsWorld) {
|
||||
world: typeof PhysicsWorld,
|
||||
capsuleShape?: typeof CapsuleShape
|
||||
) {
|
||||
BoxShape = boxShape;
|
||||
SphereShape = sphereShape;
|
||||
RigidBody = body;
|
||||
PhysicsWorld = world;
|
||||
if (capsuleShape) {
|
||||
CapsuleShape = capsuleShape;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* @category physics
|
||||
*/
|
||||
|
||||
import {
|
||||
ccclass,
|
||||
executeInEditMode,
|
||||
executionOrder,
|
||||
menu,
|
||||
property,
|
||||
} from '../../../../core/data/class-decorator';
|
||||
import { createCapsuleShape } from '../../instance';
|
||||
import { ColliderComponent } from './collider-component';
|
||||
import { ICapsuleShape } from '../../../spec/i-physics-shape';
|
||||
import { Enum } from '../../../../core';
|
||||
|
||||
export enum ECapsuleDirection {
|
||||
X_AXIS,
|
||||
Y_AXIS,
|
||||
Z_AXIS,
|
||||
}
|
||||
Enum(ECapsuleDirection);
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 胶囊体碰撞器
|
||||
*/
|
||||
@ccclass('cc.CapsuleColliderComponent')
|
||||
@executionOrder(98)
|
||||
@menu('Components/CapsuleCollider')
|
||||
@executeInEditMode
|
||||
export class CapsuleColliderComponent extends ColliderComponent {
|
||||
/// PUBLIC PROPERTY GETTER\SETTER ///
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Get or set the radius of the sphere on the capsule body, in local space.
|
||||
* @zh
|
||||
* 获取或设置胶囊体上的球半径。
|
||||
*/
|
||||
@property({ tooltip: '胶囊体上的球的半径' })
|
||||
public get radius () {
|
||||
return this._radius;
|
||||
}
|
||||
|
||||
public set radius (value) {
|
||||
if (value < 0) value = 0;
|
||||
|
||||
this._radius = value;
|
||||
|
||||
/** Recalculated height */
|
||||
const doubleR = this._radius * 2
|
||||
if (this._height < doubleR) {
|
||||
this._height = doubleR;
|
||||
// if (!CC_EDITOR) {
|
||||
// this.capsuleShape.height = this._height;
|
||||
// }
|
||||
}
|
||||
|
||||
if (!CC_EDITOR) {
|
||||
this.capsuleShape.radius = this._radius;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Get or set the capsule body is at the corresponding axial height, in local space.
|
||||
* @zh
|
||||
* 胶囊体在相应轴向的高度,最小值为两倍的 radius。
|
||||
*/
|
||||
@property({ tooltip: '胶囊体在相应轴向的高度' })
|
||||
public get height () {
|
||||
return this._height;
|
||||
}
|
||||
|
||||
public set height (value) {
|
||||
if (value < this._radius * 2) {
|
||||
value = this._radius * 2
|
||||
}
|
||||
|
||||
this._height = value;
|
||||
if (!CC_EDITOR) {
|
||||
this.capsuleShape.height = this._height;
|
||||
}
|
||||
}
|
||||
|
||||
@property({ type: ECapsuleDirection })
|
||||
public get direction () {
|
||||
return this._direction;
|
||||
}
|
||||
|
||||
public set direction (value: ECapsuleDirection) {
|
||||
this._direction = value;
|
||||
}
|
||||
|
||||
public get capsuleShape (): ICapsuleShape {
|
||||
return this._shape as ICapsuleShape;
|
||||
}
|
||||
|
||||
/// PRIVATE PROPERTY ///
|
||||
|
||||
@property
|
||||
private _radius = 0.5;
|
||||
|
||||
@property
|
||||
private _height = 2;
|
||||
|
||||
@property
|
||||
private _direction = ECapsuleDirection.Y_AXIS;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
if (!CC_EDITOR) {
|
||||
this._shape = createCapsuleShape();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import { PhysicsRayResult } from './physics-ray-result';
|
||||
import { BoxColliderComponent } from './components/collider/box-collider-component';
|
||||
import { ColliderComponent } from './components/collider/collider-component';
|
||||
import { SphereColliderComponent } from './components/collider/sphere-collider-component';
|
||||
import { CapsuleColliderComponent } from './components/collider/capsule-collider-component';
|
||||
import { RigidBodyComponent } from './components/rigid-body-component';
|
||||
import { ConstantForce } from './components/constant-force';
|
||||
|
||||
@@ -18,16 +19,21 @@ export {
|
||||
ColliderComponent,
|
||||
BoxColliderComponent,
|
||||
SphereColliderComponent,
|
||||
CapsuleColliderComponent,
|
||||
|
||||
RigidBodyComponent,
|
||||
|
||||
PhysicMaterial,
|
||||
};
|
||||
|
||||
cc.PhysicsSystem = PhysicsSystem;
|
||||
|
||||
cc.ColliderComponent = ColliderComponent;
|
||||
cc.BoxColliderComponent = BoxColliderComponent;
|
||||
cc.SphereColliderComponent = SphereColliderComponent;
|
||||
|
||||
cc.RigidBodyComponent = RigidBodyComponent;
|
||||
|
||||
cc.PhysicMaterial = PhysicMaterial;
|
||||
cc.PhysicsRayResult = PhysicsRayResult;
|
||||
cc.ConstantForce = ConstantForce;
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
*/
|
||||
|
||||
import { Vec3 } from '../../core/math';
|
||||
import { BoxShape, PhysicsWorld, RigidBody, SphereShape } from './Physics-selector';
|
||||
import { BoxShape, PhysicsWorld, RigidBody, SphereShape, CapsuleShape } from './Physics-selector';
|
||||
import { IRigidBody } from '../spec/i-rigid-body';
|
||||
import { IBoxShape, ISphereShape } from '../spec/i-physics-shape';
|
||||
import { IBoxShape, ISphereShape, ICapsuleShape } from '../spec/i-physics-shape';
|
||||
import { IPhysicsWorld } from '../spec/i-physics-world';
|
||||
import { warn } from '../../core';
|
||||
|
||||
export function createPhysicsWorld (): IPhysicsWorld {
|
||||
return new PhysicsWorld() as IPhysicsWorld;
|
||||
@@ -23,3 +24,23 @@ export function createBoxShape (size: Vec3): IBoxShape {
|
||||
export function createSphereShape (radius: number): ISphereShape {
|
||||
return new SphereShape(radius) as ISphereShape;
|
||||
}
|
||||
|
||||
export function createCapsuleShape (radius = 0.5, height = 2, dir = 1): ICapsuleShape {
|
||||
if (CC_PHYSICS_BUILTIN) {
|
||||
return new CapsuleShape(radius, height, dir) as ICapsuleShape;
|
||||
} else {
|
||||
warn('[v1.0.3][Physics]: Currently only builtin support capsule collider');
|
||||
/** apater */
|
||||
return {
|
||||
radius: radius, height: height, direction: dir,
|
||||
material: null,
|
||||
isTrigger: false,
|
||||
center: new Vec3(),
|
||||
__preload: () => { },
|
||||
onLoad: () => { },
|
||||
onEnable: () => { },
|
||||
onDisable: () => { },
|
||||
onDestroy: () => { }
|
||||
} as any
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import { Vec3 } from '../../core/math';
|
||||
import { IVec3Like, IQuatLike } from '../../core/math/type-define';
|
||||
|
||||
export function stringfyVec3 (value: IVec3Like): string {
|
||||
return `(x: ${value.x}, y: ${value.y}, z: ${value.z})`;
|
||||
return `(x: ${value.x}, y: ${value.y}, z: ${value.z})`;
|
||||
}
|
||||
|
||||
export function stringfyQuat (value: IQuatLike): string {
|
||||
return `(x: ${value.x}, y: ${value.y}, z: ${value.z}, w: ${value.w})`;
|
||||
return `(x: ${value.x}, y: ${value.y}, z: ${value.z}, w: ${value.w})`;
|
||||
}
|
||||
|
||||
interface IWrapped<T> {
|
||||
@@ -28,3 +28,4 @@ export function getWrap<Wrapper> (object: any) {
|
||||
export function maxComponent (v: Vec3) {
|
||||
return Math.max(v.x, Math.max(v.y, v.z));
|
||||
}
|
||||
|
||||
|
||||
@@ -19,3 +19,8 @@ export interface ISphereShape extends IBaseShape {
|
||||
radius: number;
|
||||
}
|
||||
|
||||
export interface ICapsuleShape extends IBaseShape {
|
||||
radius: number;
|
||||
height: number;
|
||||
direction: number;
|
||||
}
|
||||
Reference in New Issue
Block a user