[v2] heightmap

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
yazZ3va
Posts: 1
Joined: Fri Nov 27, 2020 8:54 am

[v2] heightmap

Post by yazZ3va »

Hello.
I have a 129-by-129 height map. Tell me how to add it to the simulation. thanks

Code: Select all

using BepuPhysics.Collidables;
using BepuUtilities.Memory;
using System.Numerics;

namespace Godless_Lands_Game.Terrain
{
    class TerrainCollider
    {
        public Mesh Mesh { get; private set; }
        public TerrainCollider(int width, int height, float[] heightMap, BufferPool pool)
        {
            Mesh = CreateDeformedPlane(width, height, heightMap, pool);
        }

        private Mesh CreateDeformedPlane(int width, int height, float[] heightMap, BufferPool pool)
        {
            pool.Take<Vector3>(width * height, out var vertices);
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    vertices[height * i + j] = new Vector3(width, heightMap[i * width + j], height);
                }
            }

            var quadWidth = width - 1;
            var quadHeight = height - 1;
            var triangleCount = quadWidth * quadHeight * 2;
            pool.Take<Triangle>(triangleCount, out var triangles);

            for (int i = 0; i < quadHeight; ++i)
            {
                for (int j = 0; j < quadWidth; ++j)
                {
                    var triangleIndex = (i * quadWidth + j) * 2;
                    ref var triangle0 = ref triangles[triangleIndex];
                    ref var v00 = ref vertices[width * i + j];
                    ref var v01 = ref vertices[width * i + j + 1];
                    ref var v10 = ref vertices[width * (i + 1) + j];
                    ref var v11 = ref vertices[width * (i + 1) + j + 1];
                    triangle0.A = v00;
                    triangle0.B = v01;
                    triangle0.C = v10;
                    ref var triangle1 = ref triangles[triangleIndex + 1];
                    triangle1.A = v01;
                    triangle1.B = v11;
                    triangle1.C = v10;
                }
            }
            pool.Return(ref vertices);
            return new Mesh(triangles, Vector3.One, pool);
        }
    }
}
With this method, it crashes with an System.StackOverflowException: "Exception_WasThrown"
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: [v2] heightmap

Post by Norbo »

I don't see a stack overflow locally; are you using latest master or the latest nuget prerelease package (2.3.0-beta10)? If I remember correctly, there was a semirecent fix to crashes with degenerate mesh input.

The underlying problem is that the mesh is degenerate. All vertices have the same x and z coordinates:

Code: Select all

new Vector3(width, heightMap[i * width + j], height);
Post Reply