Finding the triangle of collision between ray mesh collision

I am trying to find the triangle a ray has collided with on the mesh. CollisionEntry does not seem to hold anything other then the objects involved and the surfaceNormal and collisionPoint. To do this internally it must have found the specific triangle, but I can’t find a way to access that.

I am trying to find the exact triangle so that I can interpolate my contact point across the smoothed normals of each vertex so my character is less jerky when moving around.

Any pointers would be appreciated.

CollisionEntry.getInto() will return the specific triangle of the collision.


Thanks for the quick reply. I guess I assumed that was an entire node but it looks like that should work. Thanks again.

Actually I am still stuck. Looking at the code getInto() is returning a CollisionSolid, this has no triangle specific code.

I experimented to see if perhaps I could still access attributes from CollisionPolygon but I don’t seem to be able to.

When I print the into node the output seems to imply it is a CollsionGeom which is a base of CollisionPolygon. At least this was the only reference to “cgeom” I found when grepping the source.

CollisionGeom is actually a subclass of CollisionPolygon, not a parent class. You should be able to directly access the methods of CollisionPolygon on objects of this type.

But, wait, I see the problem. It’s because CollisionGeom’s init_type() method wasn’t called, so its pointer comes out as the base class CollisionSolid instead. Shoot, that’s a bug.

Well, I’ll check in a fix. But it occurs to me that even with this fix, it still won’t give you the information you’re looking for: the CollisionGeom no longer has a record of the smoothed normals that went into it. It only has the computed surface normal of that particular triangle. The smoothed normals are only used for lighting calculations, and aren’t involved in the collision intersection, so there’s no record of them. (The “triangle” object itself doesn’t actually exist anywhere as an object; it’s just a few numbers in the middle of a big table, so there’s no way to store a back pointer to the actual “triangle” that you see onscreen.)

Still, if you are referring to a generated mesh e.g. via GeoMipTerrain, you should be able to figure out the point within the mesh with the (x, y) coordinates of the collision, and that will give you the appropriate smoothed normal. If it’s some other kind of mesh, you may need to find some other solution; for instance, to store all of the appropriate normals in a big lookup table.