Getting the vertexes of a triangle

Posted By: krial057

Getting the vertexes of a triangle - 11/26/11 17:37

Hey community,

I'm trying to get the Vertex numbers of the face a user clicks on. I have tried to get them with ent_buffers, here is my code:

Php Code:
if(c_trace (from,to, SCAN_TEXTURE|USE_POLYGON)>0)
{
				CONTACT* c = ent_getvertex(hit.entity, NULL, hit.vertex); 
		short* trianlgeBuffer;
		ent_buffers(hit.entity,0,0,NULL,&trianlgeBuffer,NULL);
		short sv1=trianlgeBuffer[hit.triangle*3];
		short sv2=trianlgeBuffer[hit.triangle*3+1];
		short sv3=trianlgeBuffer[hit.triangle*3+2];
		printf("clicked:%i / triangle vertexes:%i ; %i ; %i",(short)(int)c.vertex,sv1,sv2,sv3);
		if(hit.vertex==sv1||hit.vertex==sv2||hit.vertex==sv3) printf("This should always pop up :S");		   
} 




However the vertex numbers of the triangle are not the same as the vertex number of hit, but they are in the same range confused

regards, Alain smile

P.S: is here a welcome thread to present myself?
Posted By: Superku

Re: Getting the vertexes of a triangle - 12/11/11 20:51

Hi and welcome! wink
The problem is that hit.triangle and hit.vertex start at 1, not at 0:

ent_buffers(hit.entity,0,0,NULL,&trianlgeBuffer,NULL);
hit.triangle--;
hit.vertex--;
short sv1= ...
Posted By: krial057

Re: Getting the vertexes of a triangle - 12/12/11 18:04

Thank you, it works grin
I spend so many hours on this problem, and that -- was the whole thing xD

Just an other question out of curiosity (not important for my project, but something i don't understand):
Is 3dgs cheating with c_trace and getting the nearest vertex?
If I have a face and i trace it with c_trace, the hit.vertex index is always the same, even if I trace nearer on an other vertex of that face tongue
Posted By: Superku

Re: Getting the vertexes of a triangle - 12/12/11 18:15

Had to test the code myself to find the problem! wink
Yes it seems like hit.vertex is always the first vertex of the triangle, so you have to calculate it yourself:

D3DVERTEX* vbuffer;
ent_buffers(hit.entity,0,0,&vbuffer,&triangleBuffer,NULL);
...
vec_to_ent(target,hit.entity);
var max_dist = 999,j,k;
for(i = 0; i < 3; i++)
{
j = vec_dist(target,(vector(vbuffer[triangleBuffer[hit.triangle*3+i]].x,vbuffer[triangleBuffer[hit.triangle*3+i]].z,vbuffer[triangleBuffer[hit.triangle*3+i]].y));
if(j < max_dist)
{
max_dist = j;
k = i;
}
}

Now k should be your closest vertex!
Posted By: krial057

Re: Getting the vertexes of a triangle - 12/12/11 18:52

Thank you, nice snippet grin

Greetings from Luxembourg wink
© 2024 lite-C Forums