Files
esengine/packages/rapier2d/src/geometry/narrow_phase.ts

193 lines
5.7 KiB
TypeScript
Raw Normal View History

feat: 添加跨平台运行时、资产系统和UI适配功能 (#256) * feat(platform-common): 添加WASM加载器和环境检测API * feat(rapier2d): 新增Rapier2D WASM绑定包 * feat(physics-rapier2d): 添加跨平台WASM加载器 * feat(asset-system): 添加运行时资产目录和bundle格式 * feat(asset-system-editor): 新增编辑器资产管理包 * feat(editor-core): 添加构建系统和模块管理 * feat(editor-app): 重构浏览器预览使用import maps * feat(platform-web): 添加BrowserRuntime和资产读取 * feat(engine): 添加材质系统和着色器管理 * feat(material): 新增材质系统和着色器编辑器 * feat(tilemap): 增强tilemap编辑器和动画系统 * feat(modules): 添加module.json配置 * feat(core): 添加module.json和类型定义更新 * chore: 更新依赖和构建配置 * refactor(plugins): 更新插件模板使用ModuleManifest * chore: 添加第三方依赖库 * chore: 移除BehaviourTree-ai和ecs-astar子模块 * docs: 更新README和文档主题样式 * fix: 修复Rust文档测试和添加rapier2d WASM绑定 * fix(tilemap-editor): 修复画布高DPI屏幕分辨率适配问题 * feat(ui): 添加UI屏幕适配系统(CanvasScaler/SafeArea) * fix(ecs-engine-bindgen): 添加缺失的ecs-framework-math依赖 * fix: 添加缺失的包依赖修复CI构建 * fix: 修复CodeQL检测到的代码问题 * fix: 修复构建错误和缺失依赖 * fix: 修复类型检查错误 * fix(material-system): 修复tsconfig配置支持TypeScript项目引用 * fix(editor-core): 修复Rollup构建配置添加tauri external * fix: 修复CodeQL检测到的代码问题 * fix: 修复CodeQL检测到的代码问题
2025-12-03 22:15:22 +08:00
import {RawNarrowPhase, RawContactManifold} from "../raw";
import {ColliderHandle} from "./collider";
import {Vector, VectorOps} from "../math";
/**
* The narrow-phase used for precise collision-detection.
*
* To avoid leaking WASM resources, this MUST be freed manually with `narrowPhase.free()`
* once you are done using it.
*/
export class NarrowPhase {
raw: RawNarrowPhase;
tempManifold: TempContactManifold;
/**
* Release the WASM memory occupied by this narrow-phase.
*/
public free() {
if (!!this.raw) {
this.raw.free();
}
this.raw = undefined;
}
constructor(raw?: RawNarrowPhase) {
this.raw = raw || new RawNarrowPhase();
this.tempManifold = new TempContactManifold(null);
}
/**
* Enumerates all the colliders potentially in contact with the given collider.
*
* @param collider1 - The second collider involved in the contact.
* @param f - Closure that will be called on each collider that is in contact with `collider1`.
*/
public contactPairsWith(
collider1: ColliderHandle,
f: (collider2: ColliderHandle) => void,
) {
this.raw.contact_pairs_with(collider1, f);
}
/**
* Enumerates all the colliders intersecting the given colliders, assuming one of them
* is a sensor.
*/
public intersectionPairsWith(
collider1: ColliderHandle,
f: (collider2: ColliderHandle) => void,
) {
this.raw.intersection_pairs_with(collider1, f);
}
/**
* Iterates through all the contact manifolds between the given pair of colliders.
*
* @param collider1 - The first collider involved in the contact.
* @param collider2 - The second collider involved in the contact.
* @param f - Closure that will be called on each contact manifold between the two colliders. If the second argument
* passed to this closure is `true`, then the contact manifold data is flipped, i.e., methods like `localNormal1`
* actually apply to the `collider2` and fields like `localNormal2` apply to the `collider1`.
*/
public contactPair(
collider1: ColliderHandle,
collider2: ColliderHandle,
f: (manifold: TempContactManifold, flipped: boolean) => void,
) {
const rawPair = this.raw.contact_pair(collider1, collider2);
if (!!rawPair) {
const flipped = rawPair.collider1() != collider1;
let i;
for (i = 0; i < rawPair.numContactManifolds(); ++i) {
this.tempManifold.raw = rawPair.contactManifold(i);
if (!!this.tempManifold.raw) {
f(this.tempManifold, flipped);
}
// SAFETY: The RawContactManifold stores a raw pointer that will be invalidated
// at the next timestep. So we must be sure to free the pair here
// to avoid unsoundness in the Rust code.
this.tempManifold.free();
}
rawPair.free();
}
}
/**
* Returns `true` if `collider1` and `collider2` intersect and at least one of them is a sensor.
* @param collider1 The first collider involved in the intersection.
* @param collider2 The second collider involved in the intersection.
*/
public intersectionPair(
collider1: ColliderHandle,
collider2: ColliderHandle,
): boolean {
return this.raw.intersection_pair(collider1, collider2);
}
}
export class TempContactManifold {
raw: RawContactManifold;
public free() {
if (!!this.raw) {
this.raw.free();
}
this.raw = undefined;
}
constructor(raw: RawContactManifold) {
this.raw = raw;
}
public normal(): Vector {
return VectorOps.fromRaw(this.raw.normal());
}
public localNormal1(): Vector {
return VectorOps.fromRaw(this.raw.local_n1());
}
public localNormal2(): Vector {
return VectorOps.fromRaw(this.raw.local_n2());
}
public subshape1(): number {
return this.raw.subshape1();
}
public subshape2(): number {
return this.raw.subshape2();
}
public numContacts(): number {
return this.raw.num_contacts();
}
public localContactPoint1(i: number): Vector | null {
return VectorOps.fromRaw(this.raw.contact_local_p1(i));
}
public localContactPoint2(i: number): Vector | null {
return VectorOps.fromRaw(this.raw.contact_local_p2(i));
}
public contactDist(i: number): number {
return this.raw.contact_dist(i);
}
public contactFid1(i: number): number {
return this.raw.contact_fid1(i);
}
public contactFid2(i: number): number {
return this.raw.contact_fid2(i);
}
public contactImpulse(i: number): number {
return this.raw.contact_impulse(i);
}
public contactTangentImpulse(i: number): number {
return this.raw.contact_tangent_impulse(i);
}
public numSolverContacts(): number {
return this.raw.num_solver_contacts();
}
public solverContactPoint(i: number): Vector {
return VectorOps.fromRaw(this.raw.solver_contact_point(i));
}
public solverContactDist(i: number): number {
return this.raw.solver_contact_dist(i);
}
public solverContactFriction(i: number): number {
return this.raw.solver_contact_friction(i);
}
public solverContactRestitution(i: number): number {
return this.raw.solver_contact_restitution(i);
}
public solverContactTangentVelocity(i: number): Vector {
return VectorOps.fromRaw(this.raw.solver_contact_tangent_velocity(i));
}
}