gpt4 book ai didi

bullet - Ammo.js 自定义网格与球体碰撞

转载 作者:行者123 更新时间:2023-12-04 10:46:53 28 4
gpt4 key购买 nike

我正在尝试为每个对象/网格生成碰撞。它们都是静态的,应该与动态的球/球体碰撞。

我的代码如下所示:

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 的球体路径是这样的(它不会进入洞):
    Path of the sphere using btConvexHullShape

    我猜 Ammo.js 连接了最高点?如何根据索引控制连接哪些点?似乎这种方法也很慢(30k 个顶点需要一些时间),所以我尝试了:
  • btBvhTriangleMeshShape

  • 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);

    这种方法好一点,但是球体只是在反弹一次时穿过物体(从圆圈中反弹并穿过物体(红色圆圈)):
    Path of the sphere using btBvhTriangleMeshShape

    最佳答案

    原因 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);

    关于bullet - Ammo.js 自定义网格与球体碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59665854/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com