Skip to content

[rMesh] Faster ray -> mesh collision check - #6330

Merged
raysan5 merged 5 commits into
raysan5:masterfrom
JeffM2501:faster_mesh_collision
Oct 25, 2026
Merged

raysan5 merged 5 commits into
raysan5:masterfrom
JeffM2501:faster_mesh_collision

Conversation

@JeffM2501

Copy link
Copy Markdown
Contributor

This PR adds a simple optimization to GetRayCollisionMesh that in my testing improves the raycast speed by about 50% in the mesh picking demos.

The current code transforms every vertex in the mesh into worldspace to compare it against the worldspace ray.
This means that we have to do 3 transforms per triangle, and that adds up, fast.

This PR instead transforms the ray into model space so that we can just test the vertex data without doing any math on them. The point is transformed by the inverse of the transform matrix once, and the direction by the rotation of that matrix, once.
This is much faster than transforming every vertex for every triangle.
There is a fix up step at the end that transforms the model space hit pos and normal, back into worldspace, but again this is one transform total.

With this change, we do a max of 4 transforms per raycast check, not Triangle count * 3.

@raysan5
raysan5 merged commit 83f32af into raysan5:master Oct 25, 2026
16 checks passed
@raysan5

raysan5 commented Oct 25, 2026

Copy link
Copy Markdown
Owner

@JeffM2501 thanks for the improvement!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants