🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Ray-triangle intersection on large meshes is very slow

Started by
3 comments, last by VanillaSnake21 4 years, 10 months ago

At the moment I'm trying to switch from height-mapped terrain to a model terrain that could be loaded from a file. I'm currently using height-map lookup table to get the height of the terrain at a specific position as a way for the character controller to be able to walk on the terrain, however I'd like to switch to a ray cast method. The current code goes like this>

Create a ray from player position and point it in down direction.

Iterate over every triangle and test if the ray is intersection

Get the height of the triangle at that position

 

The issue is that I have 30,000 triangles in the mesh. I was wondering if there is anything I can do to speed it up, or how is such a system usually implemented (I mean walkable terrain) maybe I shouldn't even be using raycasting? Thanksl.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement

The typical thing to do is to use a spatial partitioning structure to quickly skip over most of the triangles.

One of the most common structures for a triangle mesh is a BVH, but other options include octrees, BSP trees, k-d trees, etc...

The simplest optimization would be to use a 2D grid, similar to your heightmap, and register triangles to every grid entry they overlap. When you do the ray-cast, first find the 2D grid entry, like you've done with the heightmap, and only iterate through these triangles.

You might be faster than with generic space partitioning, as you don't have to traverse a hierarchy (which usually causes tons of cache misses if you just try to do one raycast).

@Hodgman thanks, I'll be looking into it!

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

This topic is closed to new replies.

Advertisement