Cysharp/MagicPhysX

Setting up a triangle mesh

Opened this issue · 2 comments

Hello,
I have the following problem:
I am trying to set up a triangle mesh using the NVIDIA documentation: NVIDIA Docs. When I run the code, I get a warning that the triangles are too large. However, this is not true (max. 1.5). I also get the following error:

C:\Users\runneradmin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\physx-sys-0.11.1\physx/physx\source/geomutils/src/cooking\GuCookingTriangleMesh.cpp (202) : warning : TriangleMesh: triangles are too big, reduce their size to increase simulation stability!

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
   at MagicPhysX.NativeMethods.phys_PxCookTriangleMesh(MagicPhysX.PxCookingParams*, MagicPhysX.PxTriangleMeshDesc*, MagicPhysX.PxOutputStream*, MagicPhysX.PxTriangleMeshCookingResult*)
--------------------------------

And here is my code:

PxTriangleMeshDesc meshDesc = new();

meshDesc.points.count = (uint)subMesh.Vertices.Length;
meshDesc.points.stride = (uint)sizeof(PxVec3);
meshDesc.points.data = (void*)Marshal.UnsafeAddrOfPinnedArrayElement(subMesh.Vertices.ToPxVector3(), 0);

meshDesc.triangles.count = (uint)subMesh.Indices.Length / 3;
meshDesc.triangles.stride = 3 * sizeof(uint);
meshDesc.triangles.data = (void*)Marshal.UnsafeAddrOfPinnedArrayElement(subMesh.Indices, 0);

PxCookingParams cookingParams = new();

PxDefaultMemoryOutputStream writeBuffer = new();
PxTriangleMeshCookingResult result = PxTriangleMeshCookingResult.Success;
bool status = phys_PxCookTriangleMesh(&cookingParams, &meshDesc, (PxOutputStream*)(&writeBuffer), &result);
if (!status)
     return;

Another problem is in the code after that:

PxDefaultMemoryInputData readData = new(writeBuffer.GetData(), writeBuffer.GetSize());    // ???
PxPhysics_createTriangleMesh_mut(_physics, &writeBuffer);

According to the documentation, you can create PxDefaultMemoryInputData with two parameters writeBuffer.getData(), writeBuffer.getSize(), but in C# the struct only declares the field structgen_pad0.

Thank you very much for your help and work on this project!

I met the same problem, did you solve it?

I met the same problem, did you solve it?

I switched to PhysX.NET eventually.