Torque
2d 纯计算高性能刚体物理引擎
Install / Use
/learn @phenomLi/TorqueREADME
Torque
2D 刚体高性能物理引擎,不包含渲染。
<br />Usage
初始化一个 torque 实例
const torque = new Torque(width, height);
创建并添加一个矩形
const rect = Torque.body.Rect(100, 100, 100, 200);
torque.append(rect);
此时一个宽 100,高 200 的矩形被创建在物理世界中的(100,100)位置上。但是此时我们看不到任何画面。
Torque 仅包含物理计算,不包含渲染器,因此你需要选取一个渲染器进行图形绘制。以 PIXI 为例,给这个矩形添加渲染器:
const app = new PIXI.Application({
width: 800,
height: 600,
antialias: true,
});
let rectangle = new PIXI.Graphics();
shape.lineStyle(0.8, 0x000000, 1);
shape.beginFill();
rectangle.position.x = 100 + 100 / 2;
rectangle.position.y = 100 + 100 / 2;
rectangle.drawRect(-100 / 2, -100 / 2, 100, 100);
rectangle.endFill();
rect.setRender(function(body) {
rectangle.position.x = body.position.x;
rectangle.position.y = body.position.y;
rectangle.rotation = body.rotation;
});
app.stage.add(rectangle);
至此,由PIXI创建的矩形rectangle与Torque世界的矩形rect通过setRender函数绑定在了一起。刷新浏览器,可以看见一个黑色边框的,长宽都为100的矩形出现在(100, 100)的位置。
Feature
- SATBoost技术
- 休眠 / 唤醒技术
- Warm Start
- Sequential Impulses
- 基于SATBoost的快速碰撞缓存 / 复用技术
- 碰撞过滤
- 基于SATBoost的快速V-clip碰撞点求解方法
- 动态 dt
- 凹多边形
- 复合刚体
- 静态 / 运动 / 动态的刚体
- 摩擦力,静摩擦力,空气摩擦,恢复系数
- 旋转关节,扭转关节,布料等
- 事件(collisionStart/collisionEnd/sleepStart/sleepEnd...)
Demo
- 最新的DEMO已将渲染器从zrender替换至PIXI,因PIXI是基于WebGL渲染,具有更好的性能
- 已增加关节约束功能!
关于SATBoost
SATBoost技术是本人研究得到的针对SAT(分离轴测试算法)的一个优化算法,能大幅提高碰撞检测的效率。在给定7 * 17个正16边形的静止碰撞(rest collision)条件下,与未经过优化的常规SAT对比结果如下(关闭碰撞复用和休眠功能):

SATBoost主要针对SAT进行改进,但同时,SATBoost也优化了碰撞复用和碰撞点求解的性能。
<br />A.D.
想了解制作物理引擎相关技术细节,可以关注我的博客(不定时更新)
Related Skills
node-connect
327.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
80.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
327.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
80.7kCommit, push, and open a PR
