引入模块
import * as Oimo from "oimo";
创建世界
let world = new Oimo.World({
timestep: 1 / 60,
iterations: 8,
broadphase: 2, // 1 brute force, 2 sweep and prune, 3 volume tree
worldscale: 1, // scale full world
random: true, // randomize sample
info: false, // calculate statistic or not
gravity: [0, -9.8, 0],
});
world.play();
创建物体
var body = this.world.add({
type: "box", // type of shape : sphere, box, cylinder
size: [1, 1, 1], // size of shape
pos: [0, 86, 0], // start position in degree
rot: [0, 0, 0], // start rotation in degree
move: true, // dynamic or statique
collision: false, //物理碰撞
density: 1,
friction: 0.2,
restitution: 0.2,
belongsTo: 1, // The bits of the collision groups to which the shape belongs.
collidesWith: 0xffffffff, // The bits of the collision groups with which the shape collides.
});
body.connectMesh(obj); // obj 就是物体 mesh
调用动画
animation() {
this.frame = requestAnimationFrame(this.animation);
this.world.step();
},
结束动画
end() {
this.world.clear();
if (this.frame) {
cancelAnimationFrame(this.frame);
}
},