## How to access a triangle from a GImpactMeshShape?

segovia
Posts: 13
Joined: Fri Oct 17, 2014 5:43 pm

### How to access a triangle from a GImpactMeshShape?

Here is the code I use in my callback function (which I set gContactAddedCallback equal to):

Code: Select all

``````bool callbackFunc(btManifoldPoint &cp, const btCollisionObjectWrapper *obj1, int id1, int index1, const btCollisionObjectWrapper *obj2, int id2, int index2)
{
const btCollisionObject *o1 = obj1->getCollisionObject();
const btCollisionObject *o2 = obj2->getCollisionObject();
int index = -1;

if(o1 == femur8->getBody())
{
std::cout << "o1 == femur " << id1 << " " << index1 << std::endl;
index=index1;
}
if(o2 == femur8->getBody())
{
std::cout << "o2 == femur" << id2 << " " << index2 << std::endl;
index=index2;
}

if(index == -1) return false;
btGImpactMeshShape *shape = static_cast<btGImpactMeshShape*>(femur8->getBody()->getCollisionShape());
btTriangleShapeEx triangle;
shape->getBulletTriangle(index, triangle);
btVector3 vert;
for(int i=0; i<3; ++i)
{
triangle.getVertex(i,vert);
std::cout << vert.getX() << " " << vert.getY() << " " << vert.getZ() << std::endl;
}
return false;
}``````
Now, no matter where the collision point is on the object of interest, all 3 vertices of the triangle have coordinates (0, 0, 0).

What am I doing wrong? Is the index passed to the callback function indeed the index of the triangle in the mesh or something else? Is there a way of doing what I am trying to do?

Thank you in advance for any help regarding this.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

### Re: How to access a triangle from a GImpactMeshShape?

I don't have an explicit answer to your question right now (I've not been using GImpactMeshShapes for a while ).

However I just wanted to say that I remember a (very old) demo with source code that detected the hit triangle of a GImpactMeshShape. It's still here: http://www.bulletphysics.org/Bullet/php ... it=gimpact (please look at HitTriangleDemo.zip; the other link is, unluckily, gone ).

I'm not sure it can help you (maybe it just used a raytest to detect the hit triangle, please check the code) and for sure it needs some adjustment (the btCollisionObjectWrapper did not exist at that time, I guess), but I suggest you check it. It might turn useful .
segovia
Posts: 13
Joined: Fri Oct 17, 2014 5:43 pm

### Re: How to access a triangle from a GImpactMeshShape?

Thanks, I will have a look.

Can anybody answer my original question, please? I would be very grateful!
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

### Re: How to access a triangle from a GImpactMeshShape?

Actually I think you should'n call shape->getBulletTriangle(index, triangle); in the first place.

I think you should handle GImpactMeshShapes simply like btBvhTriangleMeshShapes: you must still have the (persistent) btStridingMeshInterface (or derived class) you have used to create the shape: it's this structure that partId and indexId refer to (at least this is what I've always done).

 Please note that on these shapes there must be a method called: getMeshInterface() that returns the striding mesh interface. However that interface is simply a pointer to the one you have used to create the mesh (that class must be persistent, you're the owner). I've seen people locking/unlocking the interface before reading/writing to it... I've never done it. I've always used my original class without any problem (maybe I can do that because I usually don't share meshes between physic and graphic engines).
segovia
Posts: 13
Joined: Fri Oct 17, 2014 5:43 pm

### Re: How to access a triangle from a GImpactMeshShape?

I guess you're right. Looking at the code, I just found out that getBulletTriangle is not even implemented!

Now I just have to figure out how to use the index with the mesh interface...
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

### Re: How to access a triangle from a GImpactMeshShape?

segovia wrote:Now I just have to figure out how to use the index with the mesh interface...
In HitTriangleDemo.cpp (from the link I posted above), the relevant code is present in the last method HitTriangleDemo::process_triangle(...) (takes a hitTriangleIndex, a collision shape that can be GImpactMeshShape or btBvhTriangleMeshShape, and a float pointer that should return the three vertices (using a btVector3* would have been probably clearer) when true is returned. There's an additional TEST_DEFORM definition that can be enabled to deform the mesh.
Hope it helps.
segovia
Posts: 13
Joined: Fri Oct 17, 2014 5:43 pm

### Re: How to access a triangle from a GImpactMeshShape?

Yes, I had a look at that. And looking at the code for btTriangleIndexVertexArray::getLockedVertexIndexBase, I think I can figure out how to use the original mesh directly, as you were saying in the edit to your other message.

Thanks a lot Flix, you've been a great help!