@cocos/cannon

中文文档
1.2.8发布于 2021/07/20CommonJS零依赖近 30 天下载 2052

A lightweight 3D physics engine written in JavaScript.

原始使用文档
文档目录7 节
  1. Lightweight 3D physics for the web
  2. Browser install
  3. Node.js install
  4. Example
  5. Features
  6. Todo
  7. Help

cannon.js for cocos creator

Lightweight 3D physics for the web

Inspired by three.js and ammo.js, and driven by the fact that the web lacks a physics engine, here comes cannon.js. The rigid body physics engine includes simple collision detection, various body shapes, contacts, friction and constraints.

Demos - Documentation - Rendering hints - NPM package - CDN

Browser install

Just include cannon.js or cannon.min.js in your html and you're done:

<script src="cannon.min.js"></script>

Node.js install

Install via npm for cocos creator:

usage : npm install @cocos/cannon --save。

Example

The sample code below creates a sphere on a plane, steps the simulation, and prints the sphere simulation to the console. Note that Cannon.js uses SI units (metre, kilogram, second, etc.).

// Setup our world
var world = new CANNON.World();
world.gravity.set(0, 0, -9.82); // m/s²

// Create a sphere
var radius = 1; // m
var sphereBody = new CANNON.Body({
   mass: 5, // kg
   position: new CANNON.Vec3(0, 0, 10), // m
   shape: new CANNON.Sphere(radius)
});
world.addBody(sphereBody);

// Create a plane
var groundBody = new CANNON.Body({
    mass: 0 // mass == 0 makes the body static
});
var groundShape = new CANNON.Plane();
groundBody.addShape(groundShape);
world.addBody(groundBody);

var fixedTimeStep = 1.0 / 60.0; // seconds
var maxSubSteps = 3;

// Start the simulation loop
var lastTime;
(function simloop(time){
  requestAnimationFrame(simloop);
  if(lastTime !== undefined){
     var dt = (time - lastTime) / 1000;
     world.step(fixedTimeStep, dt, maxSubSteps);
  }
  console.log("Sphere z position: " + sphereBody.position.z);
  lastTime = time;
})();

If you want to know how to use cannon.js with a rendering engine, for example Three.js, see the Examples.

Features

  • Rigid body dynamics
  • Discrete collision detection
  • Contacts, friction and restitution
  • Constraints
    • PointToPoint (a.k.a. ball/socket joint)
    • Distance
    • Hinge (with optional motor)
    • Lock
    • ConeTwist
  • Gauss-Seidel constraint solver and an island split algorithm
  • Collision filters
  • Body sleeping
  • Experimental SPH / fluid support
  • Various shapes and collision algorithms (see table below)
SpherePlaneBoxConvexParticleHeightfieldTrimesh
SphereYesYesYesYesYesYesYes
Plane--YesYesYes-Yes
Box--YesYesYesYes(todo)
Cylinder--YesYesYesYes(todo)
Convex---YesYesYes(todo)
Particle-----(todo)(todo)
Heightfield------(todo)
Trimesh-------

Todo

The simpler todos are marked with @todo in the code. Github Issues can and should also be used for todos.

Help

Create an issue if you need help.

版本与发布记录

@cocos/cannon 共发布 29 个版本,首次发布于 2019/10/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.8latest2021/07/205 年前
1.2.7-exp.1beta2021/06/285 年前
1.2.72021/06/285 年前
1.2.6-exp.22021/06/095 年前
1.2.6-exp.12021/06/095 年前
1.2.62021/06/095 年前

关于 @cocos/cannon 的常见问题

@cocos/cannon 是什么?

A lightweight 3D physics engine written in JavaScript.

如何安装 @cocos/cannon?

在项目目录运行 npm install @cocos/cannon,或使用 pnpm add / yarn add / bun add。

@cocos/cannon 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cocos__cannon 是否存在。

@cocos/cannon 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cocos/cannon 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js *。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cocos/cannon 可以商用吗?

@cocos/cannon 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@cocos/cannon 的下载量有多少?

根据 npm 官方下载统计,@cocos/cannon 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,052 次,日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cocos/cannon 还在维护吗?

最新版本 1.2.8 发布于 2021/07/20(5 年前);至今共发布 29 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/10/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。