RecastSharp icon indicating copy to clipboard operation
RecastSharp copied to clipboard

Missing ref in dtCreateNavMeshData

Open nikita-one opened this issue 2 years ago • 4 comments

Hi. You might want to add by-ref access to dtPoly's and dtPolyDetail's in dtCreateNavMeshData method. Currently you are writing data to stack instead of native memory.

            // Store polygons
            // Mesh polys
            var src = @params->polys;
            for (int i = 0; i < @params->polyCount; ++i)
            {
                ref dtPoly p = ref navPolys[i]; // <-- here and in some other places, you need to use ref or *
                p.vertCount = 0;
                p.flags = @params->polyFlags[i];
                p.setArea(@params->polyAreas[i]);

nikita-one avatar May 23 '23 23:05 nikita-one

You also probably need to multiply by 3 in dtPointInPolygon method and in other similar methods:

        public static bool dtPointInPolygon(float* pt, float* verts, int nverts)
        {
                                                                                                                                                                            
            return dtPointInPolygon(new ReadOnlySpan<float>(pt, 3), new ReadOnlySpan<float>(verts, nverts * 3), nverts);
                                                                                                    //^here
        }

nikita-one avatar May 23 '23 23:05 nikita-one

Another major bug is in connectIntLinks method:

                    // Skip hard and non-internal edges.
                    if (poly->neis[j] == 0 || (poly->neis[j] & DT_EXT_LINK) != 0) //<-- Here you need to check for 0 instead of 1.
                        continue;

nikita-one avatar May 24 '23 01:05 nikita-one

Those fixes were enough to make basic pathfinding work, but at this point I am not sure what else is broken. Clearly the port is untested, would be nice to have this info in a readme. Hope this helps fix things up, cheers.

nikita-one avatar May 24 '23 01:05 nikita-one

Hey @nikita-one. I'm trying to port this over to Unity using the data oriented technology stack and jobs. I've got Recast converted, but Detour is giving me hard crashes, even after the modifications you advised here, which were very helpful. Did you make any additional changes other than the ones you listed?

Sovogal avatar Jul 04 '23 00:07 Sovogal