# [need Help] Collision with texture

Hey jme Com!
I started with the Irrlicht engine and now I am happy that I switched to JME.
My First Project is something like a paint game, atm I try to create a blank texture who is then editable by “shooting (aiming)” at it.
So I seperated a plane into 16 pieces (I know coordinates in 2D) and I cast a Ray from Player to this Plane.
But how can I now cast this 3D Point into the 2D Format from the texture ?
Would be awesome if you can give me some Hints Thx Sero

I’m now trying to solve exactly same problem (mentioned here http://hub.jmonkeyengine.org/forum/topic/javafx-embedded-in-jme3/page/11/#post-293190 ). Basically, convert collision point into barycentric coordinates for collission triangle and use them to interpolate texture coordinates for same triangle. I should have some code available in few hours.

Cut, pasted, and cleaned up a bit from code I have to do similar for passing events through to a UI based on clicks on an object:

[java]
Geometry geom = cr.getGeometry();
Mesh mesh = geom.getMesh();
IndexBuffer ib = mesh.getIndexBuffer();

``````    // Get the infor for the triangle we are interested in
int triIndex = cr.getTriangleIndex();
int[] indexes = new int;
indexes = ib.get(triIndex * 3);
indexes = ib.get(triIndex * 3 + 1);
indexes = ib.get(triIndex * 3 + 2);

Vector3f[] verts = new Vector3f;
Vector2f[] texes = new Vector2f;
FloatBuffer pb = mesh.getFloatBuffer(VertexBuffer.Type.Position);
FloatBuffer tb = mesh.getFloatBuffer(VertexBuffer.Type.TexCoord);

// The vertexes and UVs for the three triangle corners
for( int i = 0; i < 3; i++ ) {
int j = indexes[i];
verts[i] = new Vector3f(pb.get(j*3), pb.get(j*3+1), pb.get(j*3+2));
texes[i] = new Vector2f(tb.get(j*2), tb.get(j*2+1));
}

// Find the point in mesh space
Vector3f p = cr.getContactPoint();
p = geom.worldToLocal(p, null);

// Now use this:
// http://www.blackpawn.com/texts/pointinpoly/default.html
// ...to get the barycentric coordinates of the triangle
// this I think goes all the way:
// http://www.gamedev.net/topic/569449-texture-coordinates-at-a-point-on-triangle/
//
//
Vector3f v0 = verts.subtract(verts);
Vector3f v1 = verts.subtract(verts);
Vector3f v2 = p.subtract(verts);

float dot00 = v0.dot(v0);
float dot01 = v0.dot(v1);
float dot02 = v0.dot(v2);
float dot11 = v1.dot(v1);
float dot12 = v1.dot(v2);

float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
float v = (dot00 * dot12 - dot01 * dot02) * invDenom;

// Use the barycentric uv to interpolate a new real 'texture coordinate'
Vector2f t2 = texes.subtract(texes);
Vector2f t1 = texes.subtract(texes);

``````

[/java]

1 Like

My code at the moment for getting hit tex coordinates is (assuming:
tmp being temporary variables
p0,p1,p2 - triangle vertex positions in model space
t0,t1,t2 - corresponding texture coordinates for vertices
cp - contact point from collision results
geom - geometry in question

``````geom.worldToLocal(cp,cp);

Vector3f vn = p2.subtract(p1, tmp.vect4).crossLocal(p1.subtract(p0,tmp.vect5));
float A = vn.length();
Vector3f n = tmp.vect6.set(vn).divideLocal(A);
float u = FastMath.abs((p2.subtract(p1,tmp.vect7).crossLocal(cp.subtract(p1,tmp.vect8))).dot(n) / A);
float v = FastMath.abs((p0.subtract(p2,tmp.vect7).crossLocal(cp.subtract(p2,tmp.vect8))).dot(n) / A);
float w = 1 - u - v;

float s = t0.x * u + t1.x * v + t2.x * w;
float t = t0.y * u + t1.y * v + t2.y * w;``````

I don’t like abs around u and v, but I think it has something to do with computed normal pointing into wrong direction. Probably can be fixed by switching order of vectors, but I don’t feel too confident playing with this formula.