three.js使用cannon.js 进行不规则物体碰撞检测

优化点
使用形状类似 但点更少的物体进行拆解 如右侧模型
在这里插入图片描述

使用vhacd-js将负责的模型拆分成多个小模型
在这里插入图片描述

import { ConvexMeshDecomposition } from “vhacd-js”;

const rocketBody = new CANNON.Body({ mass: 10 });
const decomposer = await ConvexMeshDecomposition.create();
const options = { maxHulls: 64 };

const data = decomposer.computeConvexHulls({
         positions: mesh.geometry.attributes.position.array,
         indices: mesh.geometry.index.array},
         options)

data.forEach(({ positions, indices }) => {
     const vertices: CANNON.Vec3[] = [];
     const faces: number[][] = [];

     for (let index = 0; index < Object.keys(positions).length; index += 3) {
         vertices.push(new CANNON.Vec3(positions[index], positions[index + 1], positions[index + 2]));
     }

     for (let index = 0; index < Object.keys(indices).length; index += 3) {
         faces.push([indices[index], indices[index + 1], indices[index + 2]]);
     }

     const part = new CANNON.ConvexPolyhedron({ vertices, faces });

     rocketBody.addShape(part);
 });

this.world.addBody(rocketBody);

如此就添加了可用的形状

控制台可能会出现 警告和报错 但不影响使用
.faceNormals[${i}] = Vec3(${n.toString()}) looks like it points into the shape? The vertices follow. Make sure they are ordered CCW around the normal, using the right hand rule.

优化 可将拆分的数据保存 缓存使用 拆包过程开销很大会卡顿阻塞 网页会卡顿数秒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值