Skip to content

Add barycentric coordinates to ray vs. triangle collision results#2663

Open
Copilot wants to merge 3 commits intomasterfrom
copilot/get-barycentric-coords-collision
Open

Add barycentric coordinates to ray vs. triangle collision results#2663
Copilot wants to merge 3 commits intomasterfrom
copilot/get-barycentric-coords-collision

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 28, 2026

Ray-triangle collision results (CollisionResult) had no way to retrieve barycentric coordinates of the contact point, making texture-coord interpolation and surface-parameter lookups impossible without redundant re-computation.

Changes

  • CollisionResult: Added contactBaryCoords (Vector2f) field with getter/setter. Convention: (u, v) where u = weight of v1, v = weight of v2, 1-u-v = weight of v0.

  • CollisionResults: Added requiresBaryCoords boolean flag (default false) with setRequiresBaryCoords(boolean) / isRequiresBaryCoords(). Barycentric coordinates are only computed and stored when this flag is enabled, avoiding unnecessary allocation and math for callers that don't need them.

  • Ray: Added intersects(Vector3f v0, Vector3f v1, Vector3f v2, Vector2f baryCoords) overload that computes barycentric coordinates alongside the hit distance in a single pass. Updated collideWith(AbstractTriangle) to populate contactBaryCoords on the result only when the flag is set.

  • BIHNode: Updated intersectWhere() leaf loop (mesh collision path) to use the new overload and store barycentric coords on each CollisionResult only when the flag is set.

Usage

CollisionResults results = new CollisionResults();
results.setRequiresBaryCoords(true); // opt in to barycentric computation
ray.collideWith(geometry, results);

CollisionResult hit = results.getClosestCollision();
Vector2f bary = hit.getContactBaryCoords(); // (u, v); w0 = 1 - u - v

// Interpolate a vertex attribute (e.g. texture coords) at the hit point
Vector2f texCoord = texCoord0.mult(1 - bary.x - bary.y)
                 .add(texCoord1.mult(bary.x))
                 .add(texCoord2.mult(bary.y));

getContactBaryCoords() returns null when the flag is disabled (default) or for collision types that don't involve a triangle (e.g. bounding volume hits).


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add barycentric coordinates calculation for ray-triangle collision Add barycentric coordinates to ray vs. triangle collision results Mar 28, 2026
Copilot AI requested a review from riccardobl March 28, 2026 09:58
@riccardobl
Copy link
Copy Markdown
Member

Can you add a boolean flag to CollisionResults to enable barycentric coordinate computation? So that it is done only when the developer need them? @copilot

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 28, 2026

Can you add a boolean flag to CollisionResults to enable barycentric coordinate computation? So that it is done only when the developer need them? @copilot

Done in commit 16a97ce. Added requiresBaryCoords boolean flag (default false) to CollisionResults. Barycentric coordinates are only computed and stored when results.setRequiresBaryCoords(true) is called before the collision query. No Vector2f is allocated and no extra math is done when the flag is off.

@riccardobl riccardobl marked this pull request as ready for review March 28, 2026 10:07
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.

Get barycentric coords for ray vs. triangle collision

2 participants