# Not even sure how to title this... contact point within a triangle

@Empire Phoenix said: Can you give us an example? Like the 3 triangle your contactpoint and your actual vs your excpected result?

Well… I can at least tell you what I DON’T expect to see! I don’t expect the a number that should be linear when moving along the relative x/y axis to jump from 0.065etc to 1.632etc, back to 0.015etc,

To see this, just use get1(), get2(), get3() from the getTriangle() method of the closest contact and call the above mentioned method.

And, aren’t barycentric coords a set of 3 numbers? This method returns distance, u, v (and u, v appear to be wrong).

EDIT: stores not returns =)

I can put together a small test case of course, I was just explaining the quickest way of seeing it so you could just see it happen if you didn’t want to wait =)>

The only thing I can think of that, might make this happen, is if the ray direction/origin is being changed to local as it iterates through possible collisions, but unfortunately, I can’t tell if this is the case.

Then, calling this with the triangles 3 vert coords, might produce the wrong results using the original origin/direction? Just a guess /shrug

Oh, one more relatively HUGE question. Does JME use this for anything other than returning a contact point? It doesn’t appear that it does, so can anyone verify that this is actually doing what it should do?

Ok… let me ask a different question to make sure the end result is being calculated properly:

I have the uv texture coords relating to all 3 verts. ‘uv1’, ‘uv2’, ‘uv3’
I (supposedly) have the Barycentric coords of the contact point, but in this case it seems they have been changed to uv for me… this is good! we’ll call this ‘d’

To get the weighted uv coords, I would:

uv1 * d + uv2 * d + uv3 * d

This should give me the proper texture coords, yes?

EDIT: If this is the case, then the following should give me the proper texture coords:

[java]
float[] indices = new float[3];
Geometry geom = closest.getGeometry();
int triIndex = closest.getTriangleIndex();
Triangle tri = closest.getTriangle(null);
geom.getMesh().getTriangle(triIndex, indices);
cp.set(closest.getContactPoint());

``````// Grab the UV coords for each vert
FloatBuffer tc = geom.getMesh().getFloatBuffer(VertexBuffer.Type.TexCoord);
uv1.set(tc.get(indices[0]*2), tc.get(indices[0]*2+1));
uv2.set(tc.get(indices[1]*2), tc.get(indices[1]*2+1));
uv3.set(tc.get(indices[2]*2), tc.get(indices[2]*2+1));

// Grab the world coords of each vert
v1.set(tri.get1());
v2.set(tri.get2());
v3.set(tri.get3());

// Get the UV coords of of the contact point
pickRay.intersectWherePlanar(v1, v2, v3, dists);
Vector2f xy = new Vector2f(dists.y, dists.z);

System.out.println(uv);
``````

[/java]

If I use this to get the barycentric coords, I can get the proper results:

[java]
Vector3f ret = new Vector3f(), v1 = new Vector3f(), v2 = new Vector3f(), v3 = new Vector3f();
public Vector3f getBarycentricCoords(Triangle tri, Vector3f cp) {
v1.set(tri.get2()).subtractLocal(tri.get1());
v2.set(tri.get3()).subtractLocal(tri.get1());
v3.set(cp).subtractLocal(tri.get1());
float d00 = v1.dot(v1);
float d01 = v1.dot(v2);
float d11 = v2.dot(v2);
float d20 = v3.dot(v1);
float d21 = v3.dot(v2);
float denom = d00 * d11 - d01 * d01;
float v = (d11 * d20 - d01 * d21) / denom;
float w = (d00 * d21 - d01 * d20) / denom;
float u = 1.0f - v - w;
return ret.set(u,v,w);
}
[/java]

But, I would really like to learn what I did wrong (or am misunderstanding) about the methods in Ray