gpt4 book ai didi

java - LWJGL 网格到 JBullet 碰撞器

转载 作者:行者123 更新时间:2023-11-30 07:01:27 24 4
gpt4 key购买 nike

我正在努力在 LWJGL 3 中创建体素引擎,我已经掌握了所有基础知识( block 、网格渲染等)。

现在我正在使用 JBullet 添加物理效果。这是我第一次直接使用 JBullet,但我之前在其他 3D 引擎中使用过 Bullet。

来自here我发现,要创建一个与网格形状相同的碰撞对象,只需将顶点和索引插入到 TriangleIndexVertexArray 中,并将其用于 BvhTriangleMeshShape 即可。

这是我的代码:

    float[] coords = mesh.getVertices();
int[] indices = mesh.getIndices();

if (indices.length > 0) {
IndexedMesh indexedMesh = new IndexedMesh();
indexedMesh.numTriangles = indices.length / 3;
indexedMesh.triangleIndexBase = ByteBuffer.allocateDirect(indices.length*Float.BYTES).order(ByteOrder.nativeOrder());
indexedMesh.triangleIndexBase.asIntBuffer().put(indices);
indexedMesh.triangleIndexStride = 3 * Float.BYTES;
indexedMesh.numVertices = coords.length / 3;
indexedMesh.vertexBase = ByteBuffer.allocateDirect(coords.length*Float.BYTES).order(ByteOrder.nativeOrder());
indexedMesh.vertexBase.asFloatBuffer().put(coords);
indexedMesh.vertexStride = 3 * Float.BYTES;

TriangleIndexVertexArray vertArray = new TriangleIndexVertexArray();
vertArray.addIndexedMesh(indexedMesh);

boolean useQuantizedAabbCompression = false;
BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(vertArray, useQuantizedAabbCompression);

CollisionShape collisionShape = meshShape;

CollisionObject colObject = new CollisionObject();
colObject.setCollisionShape(collisionShape);
colObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(position.x, position.y, position.z), 1f)));
dynamicsWorld.addCollisionObject(colObject);

} else {
System.err.println("Failed to extract geometry from model. ");
}

我知道顶点和索引是有效的,因为我在绘制网格后将它们放在这里。

这似乎有点工作,但是当我尝试将立方体刚体放到地形上时,它似乎在地形上方发生碰撞! (我知道立方体设置正确,因为如果我移除网格碰撞器,它会在 y=0 处撞击基础地平面)。

enter image description here

我认为这可能是一个缩放问题(尽管我不明白这是怎么回事),所以我尝试更改:

colObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(position.x,position.y,position.z), 1f))); 至:

colObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(position.x,position.y,position.z), 0.5f)));

但是在将比例从 1 更改后,它的表现就像网格碰撞器不存在一样。

很难找到关于网格碰撞的 JBullet 的任何资源或代码,我已经为此工作了近 2 天,所以我希望你们中的一些以前做过这件事的人可以帮助我: )

更新1:

我创建了 IDebugDrawer 的实现,以便可以在场景中绘制调试信息。

为了测试它,我只使用一个基本的地平面和一个下落的立方体来运行它。我注意到,当立方体下落时,aabb 与立方体大小相匹配,但是当它撞到地板时,aabb 变得明显比原来大。

enter image description here

我将假设这是由于碰撞弹跳而导致的正常 Bullet 行为,稍后再查看,因为它不会影响我当前的问题。

我重新启用了 block 网格物体的碰撞器生成,并看到了这一点:

enter image description here

看起来 block 的 aabb 可视化比实际 block 高很多(我知道我对整个碰撞对象的 y 定位是正确的)。

enter image description here

我将尝试弄清楚是否可以绘制实际的碰撞网格。

更新2:

据我所见,查看源代码,碰撞器的网格应该在调试中绘制,所以我不确定为什么不是。

我尝试将 Box 刚体更改为球体,它实际上滚过地形碰撞器的可视化 aabb 的顶部。不过它只是平地滚动,并没有撞到或跌落到山丘或地形的斜坡上,所以它显然只是滚过 aabb 的平坦顶部。

最佳答案

因此,在添加“调试抽屉”后,我很困惑为什么 aabb 比应有的大两倍。

花了几个小时尝试一些小调整后,我注意到一些奇怪的事情 - 对撞机和 block 边缘之间有 0.25 的间隙。我继续缩小并惊讶地注意到这一点:

enter image description here

有额外的行和列的碰撞器吗?不,这没有意义,应该有 5x5 碰撞器来匹配 5x5 block 。

然后我计算了 block 数,发现碰撞器跨越了64 个 block (我的 block 是 32x32!)。

我很快意识到这是一个缩放问题,添加后

BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(vertArray, useQuantizedAabbCompression);
meshShape.setLocalScaling(new Vector3f(0.5f, 0.5f, 0.5f));

为了将碰撞器缩小一半,一切都合适并且有效!我的“球体”滚动并停在地形上有一座小山的地方,就像它应该的那样。

enter image description here

我将 LWJGL 网格覆盖到 JBullet 网格冷却器的完整代码是:

public void addMesh(org.joml.Vector3f position, Mesh mesh){
float[] coords = mesh.getVertices();
int[] indices = mesh.getIndices();

if (indices.length > 0) {

IndexedMesh indexedMesh = new IndexedMesh();
indexedMesh.numTriangles = indices.length / 3;
indexedMesh.triangleIndexBase = ByteBuffer.allocateDirect(indices.length*Integer.BYTES).order(ByteOrder.nativeOrder());
indexedMesh.triangleIndexBase.rewind();
indexedMesh.triangleIndexBase.asIntBuffer().put(indices);
indexedMesh.triangleIndexStride = 3 * Integer.BYTES;
indexedMesh.numVertices = coords.length / 3;
indexedMesh.vertexBase = ByteBuffer.allocateDirect(coords.length*Float.BYTES).order(ByteOrder.nativeOrder());
indexedMesh.vertexBase.rewind();
indexedMesh.vertexBase.asFloatBuffer().put(coords);
indexedMesh.vertexStride = 3 * Float.BYTES;

TriangleIndexVertexArray vertArray = new TriangleIndexVertexArray();
vertArray.addIndexedMesh(indexedMesh);

boolean useQuantizedAabbCompression = false;
BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(vertArray, useQuantizedAabbCompression);
meshShape.setLocalScaling(new Vector3f(0.5f, 0.5f, 0.5f));

CollisionShape collisionShape = meshShape;

CollisionObject colObject = new CollisionObject();
colObject.setCollisionShape(collisionShape);
colObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(position.x, position.y, position.z), 1f)));
dynamicsWorld.addCollisionObject(colObject);

} else {
System.err.println("Failed to extract geometry from model. ");
}

}

更新1:

尽管缩放是解决上述问题的方法,但它让我更深入地研究并意识到我错误地在 GridView 中使用 block 大小(0.5f)作为网格缩放因子矩阵。将比例更改为 1 就像应该修复它一样。

关于java - LWJGL 网格到 JBullet 碰撞器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40855945/

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