Get UVs from collisionResult

hello,

from a collision result, I can retreive the triangle vertex and indice
but how do I get the uvs from each triangle point ?

thanks

The UVs are another buffer on the mesh.

On another note, please start using topic categories.

I know but how do I index these ?
I keep getting index out of bounds exception

int index=collision.getTriangleIndex();
                        VertexBuffer texCoords = collision.getGeometry().getMesh().getBuffer(VertexBuffer.Type.TexCoord);
                        FloatBuffer buf=(FloatBuffer) texCoords.getData();
                            float ua=buf.get((index*6)+0);
                            float va=buf.get((index*6)+1);
                            float ub=buf.get((index*6)+2);
                            float vb=buf.get((index*6)+3);
                            float uc=buf.get((index*6)+4);
                            float vc=buf.get((index*6)+5);

....

nobody ?
it’s loaded from a blender file so I am not sure how data are organized…

Where?

You are using the index of a 3 sided figure to for some reason index into a 6 sided figure.

yes 3 vertex => 3 uvs => 6 floats

float ua=buf.get((index*6)+0); <— here

Ah, yeah, I was being dumb.

Anyway, you are not using the index correctly. If the mesh has an index buffer then that means you need to use it to find the real vertex index.

Looking at the code for http://javadoc.jmonkeyengine.org/com/jme3/scene/Mesh.html#getTriangle(int,%20com.jme3.math.Triangle)
…may or may not be helpful.

ah ok that makes sense :slight_smile:

[edit: ok, stupid of me I forgot x2 :slight_smile: ]

got rid of the exceptiion, but the uvs I get are all messed up

CollisionResult collision = results.getClosestCollision();
                        int triangleIndex=collision.getTriangleIndex();
                        
                        VertexBuffer texCoords = collision.getGeometry().getMesh().getBuffer(VertexBuffer.Type.TexCoord);
                        FloatBuffer uvs=(FloatBuffer) texCoords.getData();
                        
                        VertexBuffer _indexes = collision.getGeometry().getMesh().getBuffer(VertexBuffer.Type.Index);
                        ShortBuffer indexes=(ShortBuffer) _indexes.getData();
                        
                        int a=indexes.get((triangleIndex*3)+0);
                        int b=indexes.get((triangleIndex*3)+1);
                        int c=indexes.get((triangleIndex*3)+2);
                        
                        float ua=uvs.get(a+0);
                        float va=uvs.get(a+1);
                        float ub=uvs.get(b+0);
                        float vb=uvs.get(b+1);
                        float uc=uvs.get(c+0);
                        float vc=uvs.get(c+1);

am I misunderstanding how uv and indexes are related ?

How do you know they are messed up?

Also, the fact that you are casting directly to ShortBuffer makes me wonder if you looked at the code I referenced. You can’t always count on the index buffer being a short buffer but JME already provides a way to handle all index buffers… which the method I referenced uses.

I solved the problem (as I edited m last post)

but I first tried with integer , I had a class cast exception so I used shorts
I dont see the link with the getTriangle method since I use shorts for uv’s, not for the triangle indexes …

It’s not about your UVs it’s about how you are accessing the index buffer. You are currently assuming it is a ShortBuffer because you had a class cast exception… the code for getTriangle() shows how to access the index buffer without being concerned about whether it’s short or int or whatever.

my bad I misread you once again ha ha ha :smile:

            IndexBuffer indexes = me.getIndicesAsList();
            int vertIndex = triangleIndex * 3;
            int a = indexes.get(vertIndex+0);
            int b = indexes.get(vertIndex+1);
            int c = indexes.get(vertIndex+2);

nice indeed