Hi, I am baking Unity terrain into the ECS world. This causes inaccurate raycasts and collisions.
If you look at the raycast results, they follow the rotated triangles (which I drew as green lines) instead of the terrain mesh triangles. This issue also affects collisions.
I managed to fix the raycasting issue by modifying Raycast.RayTerrain, but I am struggling to modify the collision code located in Manifold.ConvexTerrain as well as TerrainCollider.GetHeightAndGradient.
This is very interesting. I know going back years people have complained about having inaccuracies in TerrainCollider.Create(). But no one could ever pinpoint exactly what was going wrong.
Are the edges always flipped? I assume you are using TerrainCollider.CollisionMethod.Triangles. This really should be reported as a bug.
The best solution would be to fix the height map collider code or at least have it match PhysX.
This happens both on CollisionMethod.Triangles and CollisionMethod.VertexSamples.
if i use CollisionMethod.VertexSamples algorithm ends up in Manifold.ConvexTerrain → TerrainCollider.GetHeightAndGradient.
when using CollisionMethod.Triangles it ends up in OverlapQueries.AabbTerrain → ManifoldQueries.ConvexCompositeOverlapCollector.AddColliderKeys → ManifoldQueries.ConvexConvex → ConvexConvexManifoldQueries.BoxTriangle (when terrain and box collides)
They both act same in terms of trianglulation, and yes as far i tested, triangles are always rotated along all terrain. This is much more noticable if you use very low res terrain.
isn’t those triangles the exact thing what supposed to happen with mesh convex hull colliders? (Colliding with air in visually empty places, but giving ability to work in RigidBodies).
If it is convex hulled, you can’t make it differently, but I’m not a pro here.
(edited)
As far as i understood from the source of Terrain collider, there is no hull cache or something, triangles are generated from height information dynamically.
If I remember correctly originally ECS did not have a height map collider, and a Convex Hull was suggested for this use case. I do not believe that PhysX uses a Convex Hull. Having a height map collider that is a Convex Hull will cause issues.
I am not sure it is creating a convex shape. Plenty of people report the height map collider causing ground mesh clipping. The height map collider needs to match up with the ground mesh fairly closely.
I think i managed to fix it. This fix is only for TerrainCollider.CollisionMethod.Triangles
Here are videos from Before and After
Changed triangles located in Terrain.GetTriangle
public ChildCollider GetTriangle(int2 index, int triangle, CollisionFilter filter, Material material)
{
int2 index0 = index + new int2(0, 0);
int2 index1 = index + new int2(1, triangle);
int2 index2 = index + new int2(1 - triangle, 1);
return new ChildCollider(
new float3(index0.x, GetHeight(index0), index0.y) * Scale,
new float3(index1.x, GetHeight(index1), index1.y) * Scale,
new float3(index2.x, GetHeight(index2), index2.y) * Scale,
filter, material);
}
//From
int2 index0 = index + new int2(0, triangle);
int2 index1 = index + new int2(1, 0);
int2 index2 = index + new int2(triangle, 1);
//To
int2 index0 = index + new int2(0, 0);
int2 index1 = index + new int2(1, triangle);
int2 index2 = index + new int2(1 - triangle, 1);