Draw on intersection point on 3D mesh

For my second game, part of the core mechanics is drawing one object onto another upon collision.

This is a very useful technique, and there doesn’t appear to be much concise info about it.

Basically all you have to do is find the UV coordinate of the intersection point on a mesh, then of course just use those UV’s to draw or print something on the texture. Naturally the drawable mesh has to have all unique UV space, not tiled UV’s etc.

You could use this method to unproject the mouse coordinates to 3D space and draw directly on the mesh with the mouse, however I’m using it between 2 objects in pure 3D space. Which would commonly be used for something like a bullet hole etc.

First things first you’ll need a solid ray to triangle intersection test in order to find the intersection point of the mesh, and importantly, the triangle that is intersected.

The fastest method I’ve found to determine if the point is in the triangle for the ray triangle test is to use barycentric coordinates. Which is basically the UV (0 to 1 range) of the point in the triangle. If both points are within the 0 to 1 range, you know the point is in the triangle.

This page outlines the barycentric method and how to use it for a point in triangle test - http://www.blackpawn.com/texts/pointinpoly/default.html

Once you’ve determined the point is in the triangle, all the hard work and necessary calculations are done, all you need to do now is interpolate your local triangle UV with the existing 3 UV points of the triangle to determine the actual mesh level UV coordinate of the intersection point.

Which is simply this

var uv0:UV = poly.UV1;
var uv1:UV = poly.UV2;
var uv2:UV = poly.UV3;
intersectionUV.u = uv0.u+(uv2.u-uv0.u)*u + (uv1.u-uv0.u)*v;
intersectionUV.v = uv0.v+(uv2.v-uv0.v)*u + (uv1.v-uv0.v)*v;

And that’s it, the intersectionUV can then be used to do whatever you want to the given mesh, paint on it etc :slight_smile:

I just build my print matrix like this.

printMatrix.tx = uv.u * sanchezTex.width;
printMatrix.ty = uv.v * sanchezTex.height;

Cheers,
RumbleSushi