DanielChappuis / reactphysics3d

Open source C++ physics engine library in 3D
http://www.reactphysics3d.com
zlib License
1.5k stars 218 forks source link

ConcaveMeshShape/TriangleVertexArray not working? #262

Open NullJupiter opened 2 years ago

NullJupiter commented 2 years ago

So I tried to use the ConcaveMeshShape for the basic structure of my scene. But it doesn't seem to have the correct dimensions and/or vertices. I'm using Assimp to import my models. My code to create a ConcaveMeshShape looks like this:

if (entity.HasComponent<ConcaveColliderComponent>() && entity.HasComponent<MeshComponent>())
{
    auto& concaveCollider = entity.GetComponent<ConcaveColliderComponent>();
    concaveCollider.RuntimeFixture = rigidbody;

    auto& meshComponent = entity.GetComponent<MeshComponent>();

    // create triangle mesh
    concaveCollider.TriangleMesh = (void*)m_PhysicsCommon.createTriangleMesh();
    for (auto& subMesh : meshComponent.MeshRef->GetSubMeshes())
    {
        const float* vertices = subMesh.Positions.data();
        uint32_t vertexCount = subMesh.VertexCount;
        size_t vertexStride = subMesh.VertexStride;

        const uint32_t* uIndices = subMesh.Indices.data();

        int32_t* indices = new int32_t[subMesh.IndexCount];
        for (uint32_t i = 0; i < subMesh.IndexCount; i++)
            indices[i] = (int32_t)uIndices[i];

        uint32_t triangeCount = subMesh.TriangleCount;

        TriangleVertexArray* triangleArray = new TriangleVertexArray(vertexCount, vertices, vertexStride * sizeof(float),
            triangeCount, indices, 3 * sizeof(int32_t),
            TriangleVertexArray::VertexDataType::VERTEX_FLOAT_TYPE,
            TriangleVertexArray::IndexDataType::INDEX_INTEGER_TYPE);

        // add the triangle vertex array to the triangle mesh
        ((TriangleMesh*)concaveCollider.TriangleMesh)->addSubpart(triangleArray);
    }

    // create concave mesh shape
    ConcaveMeshShape* concaveMeshShape = m_PhysicsCommon.createConcaveMeshShape((TriangleMesh*)concaveCollider.TriangleMesh, Vector3(transformComponent.Scale.x, transformComponent.Scale.y, transformComponent.Scale.z));

    // create collider
    Collider* collider = rigidbody->addCollider(concaveMeshShape, Transform());
    concaveCollider.RuntimeCollider = (void*)collider;
}

I get the vertex and index data from the different sub meshes directly from Assimp. I didn't know if unsigned integer types were allowed in the TriangleVertexArray constructor so I casted all indices to integers. Would be great if someone could clarify if unsigned integers also work.

Is there a step I'm missing or something like that. I can't figure out what would be wrong with this implementation from the documentation. There seems to be some kind of collision happening but the scale and/or the vertex positions seem to be off. I checked that my transformComponent.Scale is correct and it is.

I would really appreciate help with this issue I'm having. :)

twixuss commented 2 years ago

I assume vertices is an array of 3 floats, so you need to pass sizeof(float)*3 into vertex stride when creating TriangleVertexArray.

I looked at the source code and it uses unsigned integers for indices.

NullJupiter commented 2 years ago

Okay thanks for clarifying the index data type. I changed it to be uint32_t. The variable vertexStride already is 3 so there shouldn't be a problem with that. The variable vertices is an array of 3 floats per position (no normals or texture coords). Also vertexCount = p_aiMesh->mNumVertices (from Assimp) and triangleCount = p_aiMesh->mNumFaces.

DanielChappuis commented 2 years ago

@twixuss Thanks a lot for your help.

@NullJupiter Thanks for your question. I don't really see anything wrong with your code at first.

Can you try to compare your code with the one used for ConcaveMeshes in the testbed application here? Maybe you will find what's different.

You can try to create a much more simpler ConcaveMeshShape at first like at single triangle. Maybe it will help understand what wrong.

You can also try to use the DebugRenderer to display the collision shapes of the physics engine in your own application to understand where exactly is the concave mesh shape.

NullJupiter commented 2 years ago

Thanks for the response. I looked through the testbed application and I can't find anything different from what I'm doing. So I tried using the DebugRenderer to visualize the COLLISION_AABB lines and the COLLISION_SHAPE triangles. Though the positions seem to be off. Here is a screenshot which show where the debug information is being rendered. The scene only uses box colliders which work so I'm doing something wrong with the positioning. image

Here is my shader code:

#version 460 core

layout(location = 0) in vec3 v_WorldPosition;

uniform mat4 u_ViewProjection;

void main(void)
{
    gl_Position = u_ViewProjection * vec4(v_WorldPosition, 1.0);
}
#version 460 core

out vec4 fs_color;

void main(void)
{
    fs_color = vec4(1.0, 0.1, 0.2, 1.0);
}

As I understand the documentation the positional data should be in world space already so I only used the view projection matrix from my camera class. Do you know why the positions could be off?

DanielChappuis commented 2 years ago

Yes, the points of the Debug Renderer at given in world-space.

I am not sure I really understand your last post. What position seem to be off? The ones of your boxes or only the position of your ConcaveMeshShape?

I don't really understand your screenshot. What is the strange red shape? Is it your ConcaveMeshShape? Can you create a very simple concave mesh shape with only one or two large triangles?

Can you try to also display the collision shapes of your boxes using the DebugRenderer? This way you will know if your shader to display the DebugRenderer shapes is working correctly.

NullJupiter commented 2 years ago

Yeah the strange red shape is supposed to be the COLLISION_AABB and COLLISION_SHAPE lines and triangles. In the scene on the screenshot are only boxes with box colliders (which are working) and no concave colliders. So the lines and triangles should be at different positions. I tried only box colliders first because I know they work so there is something wrong with my DebugRenderer implementation. When using a simple shape with a concave collider the DebugRenderer data (the strange red shape) still looks the same. I can post the entire code of my short term DebugRenderer implementation when I'm at home.

DanielChappuis commented 2 years ago

Ok so if I understand correctly, the red shape is the lines and triangles of your box colliders not your concave mesh shape right? That's why you say that there is something wrong with your DebugRenderer implementation.

I don't know if that helps you but here is the place in the testbed application where the lines and triangles of the DebugRenderer are rendered.