bullet - Ammo.js 自定义网格与球体碰撞
问题描述
我正在尝试为每个对象/网格生成碰撞。它们都是静态的,应该与动态的球/球体碰撞。
我的代码如下所示:
const transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin(new Ammo.btVector3(-1.1726552248001099, 2.6692488193511963, 0));
transform.setRotation(new Ammo.btQuaternion(0.5, -0.5, 0.5, 0.4999999701976776));
const motionState = new Ammo.btDefaultMotionState(transform);
// Vertices and indices are parsed by GLTF parser by loaders.gl
const vertices = Entity.vertices;
const indices = Entity.indices;
const scale = [0.15933185815811157, 1.1706310510635376, 0.15933185815811157];
// btConvexHullShape or btBvhTriangleMeshShape, see below.
const localInertia = new Ammo.btVector3(0, 0, 0);
const rbInfo = new Ammo.btRigidBodyConstructionInfo(0, motionState, shape, localInertia);
const object = new Ammo.btRigidBody(rbInfo);
this._physicsWorld.addRigidBody(object);
我尝试了 2 种方法:btConvexHullShape 和 btBvhTriangleMeshShape 均无效。
- btConvexHullShape
const shape = new Ammo.btConvexHullShape();
for (let i = 0; i < vertices.length / 3; i++) {
shape.addPoint(new Ammo.btVector3(vertices[i * 3] * scale[0], vertices[i * 3 + 1] * scale[1], vertices[i * 3 + 2] * scale[2]));
}
使用 btConvexHullShape 的球体路径是这样的(它不进入孔):
我猜 Ammo.js 连接最高点?如何根据索引控制连接哪些点?这种方法似乎也很慢(30k个顶点需要一些时间),所以我尝试了:
- btBvh三角形网格形状
const mesh = new Ammo.btTriangleMesh(true, true);
for (let i = 0; i * 3 < indices.length; i++) {
mesh.addTriangle(
new Ammo.btVector3(vertices[indices[i * 3]] * scale[0], vertices[indices[i * 3] + 1] * scale[1], vertices[indices[i * 3] + 2] * scale[2]),
new Ammo.btVector3(vertices[indices[i * 3 + 1]] * scale[0], vertices[indices[i * 3 + 1] + 1] * scale[1], vertices[indices[i * 3 + 1] + 2] * scale[2]),
new Ammo.btVector3(vertices[indices[i * 3 + 2]] * scale[0], vertices[indices[i * 3 + 2] + 1] * scale[1], vertices[indices[i * 3 + 2] + 2] * scale[2]),
false
);
}
const shape = new Ammo.btBvhTriangleMeshShape(mesh, true, true);
解决方案
btConvexHullShape 不起作用的原因(尽管它周围的碰撞实际上起作用)是因为它不适用于凹形。
至于btBvhTriangleMeshShape,我一直以错误的方式输入顶点。我不得不将索引乘以 3(因为每个顶点有 3 个分量)。
这是完整的工作代码:
const transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin(new Ammo.btVector3(-1.1726552248001099, 2.6692488193511963, 0));
transform.setRotation(new Ammo.btQuaternion(0.5, -0.5, 0.5, 0.4999999701976776));
const motionState = new Ammo.btDefaultMotionState(transform);
// Vertices and indices are parsed by GLTF parser by loaders.gl
const vertices = Entity.vertices;
const indices = Entity.indices;
const scale = [0.15933185815811157, 1.1706310510635376, 0.15933185815811157];
const mesh = new Ammo.btTriangleMesh(true, true);
mesh.setScaling(new Ammo.btVector3(scale[0], scale[1], scale[2]));
for (let i = 0; i * 3 < indices.length; i++) {
mesh.addTriangle(
new Ammo.btVector3(vertices[indices[i * 3] * 3], vertices[indices[i * 3] * 3 + 1], vertices[indices[i * 3] * 3 + 2]),
new Ammo.btVector3(vertices[indices[i * 3 + 1] * 3], vertices[indices[i * 3 + 1] * 3 + 1], vertices[indices[i * 3 + 1] * 3 + 2]),
new Ammo.btVector3(vertices[indices[i * 3 + 2] * 3], vertices[indices[i * 3 + 2] * 3 + 1], vertices[indices[i * 3 + 2] * 3 + 2]),
false
);
}
const shape = new Ammo.btBvhTriangleMeshShape(mesh, true, true);
const localInertia = new Ammo.btVector3(0, 0, 0);
const rbInfo = new Ammo.btRigidBodyConstructionInfo(0, motionState, shape, localInertia);
const object = new Ammo.btRigidBody(rbInfo);
this._physicsWorld.addRigidBody(object);
推荐阅读
- javascript - 样式后不会单击模态关闭按钮
- sql-server - 导入 bcp 输出,按日期时间吐槽
- javascript - 是否可以从节点端点运行 mocha 测试套件?
- javascript - Javascript 不适用于 Chrome 扩展程序
- angular - Angular 6 有 angular.noop 吗?
- php - 允许使用 PHP 将大型视频文件流式传输给经过身份验证的用户
- c# - 如何使用 C# 将 Wav 文件合二为一
- macos - 如何在 Mac OS 上更改终端默认图标
- node.js - Node JS 多个服务器实例,每个核心一个?
- amazon-cloudformation - 将 Application Autoscaling Group 与 ELB 运行状况检查一起使用