DanielChappuis / reactphysics3d

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

Exeption thrown at not null pointer #108

Closed lp64ace closed 4 years ago

lp64ace commented 5 years ago

I get an Exeption thrown here "rp3d::PolyhedronMesh *polyhedronMesh = new rp3d::PolyhedronMesh ( polygonVertexArray ) ;" without polygonVertexArray being NULL

` for ( auto mesh : model->meshes ) {

    std::vector<float> v ;

    for ( int i = 0 ; i < mesh.vertices.size ( ) ; i++ ) {
        v.push_back ( mesh.vertices [ i ].Position.x ) ;
        v.push_back ( mesh.vertices [ i ].Position.y ) ;
        v.push_back ( mesh.vertices [ i ].Position.z ) ;
    }

    std::vector<int> idx ;

    for ( int i = 0 ; i < mesh.indices.size ( ) ; i++ ) {
        idx.push_back ( mesh.indices [ i ] ) ;
    }

    rp3d::PolygonVertexArray::PolygonFace *polygonFaces = new rp3d::PolygonVertexArray::PolygonFace [ idx.size ( ) / 3 ];

    for ( int f = 0; f < ( idx.size ( ) / 3 ) ; f++ ) {
        polygonFaces [ f ].indexBase = f * 3;
        polygonFaces [ f ].nbVertices = 3;
    }

    // Create the polygon vertex array
    rp3d::PolygonVertexArray *polygonVertexArray = new rp3d::PolygonVertexArray ( v.size ( ) / 3 , &v [ 0 ] , 3 * sizeof ( float ) ,
                                              &idx [ 0 ] , sizeof ( int ) , idx.size ( ) / 3 , polygonFaces ,
                                              rp3d::PolygonVertexArray::VertexDataType::VERTEX_FLOAT_TYPE ,
                                              rp3d::PolygonVertexArray::IndexDataType::INDEX_INTEGER_TYPE ) ;

    assert ( polygonVertexArray ) ;

    // Create the polyhedron mesh 
    rp3d::PolyhedronMesh *polyhedronMesh = new rp3d::PolyhedronMesh ( polygonVertexArray ) ;
    // Create the convex mesh collision shape
    rp3d::ConvexMeshShape *convexMeshShape = new rp3d::ConvexMeshShape ( polyhedronMesh ) ;

    rp3d::Transform transform = rp3d::Transform::identity ( ) ;

    auto proxy = body->addCollisionShape ( convexMeshShape , transform , body->getMass ( ) ) ;
    assert ( proxy ) ;

    collisionShapes.push_back ( proxy ) ;

}

`

DanielChappuis commented 5 years ago

Hello. Thanks a lot for reporting this and sorry for my late answer.

Can I ask you to compile the ReactPhysics3D in DEBUG mode and tell me exactly at which line the exception is thrown inside the PolyhedronMesh() call ? The exception is probably not thrown at the line you mentioned but from inside the code of the library itself.

That would help me a lot. Best regards.

lp64ace commented 4 years ago

I am so sorry about my late answer as well . A couple of months ago I quit this project after moving from vs 2017 to vs 2019 due to some libraries incompatabilities , I am currently working on version 2.0 of the project and I have build reactphysics3d for RlsWithDbgInfo but I have no idea where I can read those messages from so I can give you some feedback base on this problem.

P.S. please update the user manual.

DanielChappuis commented 4 years ago

You should build the Reactphysics3D library with the Debug flag instead of RlsWithDebgInfo so that when the exception is thrown your debugger will stop at the exact line where the issue is located.

DanielChappuis commented 4 years ago

Have you been able to find the exact line where the exception is thrown ?

saucecode commented 4 years ago

Hello, I believe I am getting this error as well!

I have a model created with Blender, imported into my C++ project with assimp (which explicitly says it winds the faces CCW, so I don't think that's the issue). The assimp-loaded data is then fed into a rp3d::PolygonVertexArray much like in OP's code snippet above.

Here's what valgrind reports:

terminate called after throwing an instance of 'std::runtime_error'
  what():  No item with given key has been found in the map
==10021== 
==10021== Process terminating with default action of signal 6 (SIGABRT)
==10021==    at 0x6AE7E97: raise (raise.c:51)
==10021==    by 0x6AE9800: abort (abort.c:79)
==10021==    by 0x61F6956: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==10021==    by 0x61FCAB5: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==10021==    by 0x61FCAF0: std::terminate() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==10021==    by 0x61FCD23: __cxa_throw (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==10021==    by 0x241B73: reactphysics3d::Map<reactphysics3d::Pair<unsigned int, unsigned int>, unsigned int>::operator[](reactphysics3d::Pair<unsigned int, unsigned int> const&) (Map.h:652)
==10021==    by 0x23FA2E: reactphysics3d::HalfEdgeStructure::init() (HalfEdgeStructure.cpp:121)
==10021==    by 0x23EB9C: reactphysics3d::PolyhedronMesh::createHalfEdgeStructure() (PolyhedronMesh.cpp:93)
==10021==    by 0x23E90D: reactphysics3d::PolyhedronMesh::PolyhedronMesh(reactphysics3d::PolygonVertexArray*) (PolyhedronMesh.cpp:48)
==10021==    by 0x1AF801: Redacted::createCollisionMeshFromOBJFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (Redacted.cpp:121)
==10021==    by 0x1AF092: Redacted::Redacted(Pterodacted, glm::vec<3, float, (glm::qualifier)0>, reactphysics3d::DynamicsWorld&) (Redacted.cpp:76)

This corresponds to line 116 of this file from the 0.7.1 release (I modified the file to figure out exactly which Map access caused it, hence the mismatching line numbers).

From the valgrind output I determine that the Map in No item with given key has been found in the map is in reference to mapEdgeToIndex.

I haven't gotten any further than this, so hopefully this is useful for you. Find attached the model I'm using (OBJ file, a single elongated and triangulated rectangular prism).

I was able to get the sample code from the User Manual working, so I suspect it's related to the specifically to file-loaded models.

collision_mesh_obj.zip

saucecode commented 4 years ago

Scratch that. Under further investigation I believe this is not being caused by rp3d, but by my own code (incorrect indices). It appears that Assimp produces duplicate vertices, which is causing identical edges to appear unique.


I was able to tell assimp to merge duplicate vertices by discarding *all* vertex data except position (normals, tangets, UVs, etc), which has solved the issue for me.

DanielChappuis commented 4 years ago

I was able to tell assimp to merge duplicate vertices by discarding all vertex data except position (normals, tangets, UVs, etc), which has solved the issue for me.

Ok perfect. That's good news.