raycastting to static mesh (polygon soup)

pognibene
Posts: 6
Joined: Sat Dec 16, 2006 2:53 pm

raycastting to static mesh (polygon soup)

Post by pognibene »

Hi all,

I'm trying to use bullet to implement static objects picking
using raycasts (like a house or a tree). This is using the Ogre engine.
Unfortunately, I can't find any intersections with bullet,
while I find them with a simplistic test-all-triangles
approach.

There's probably something wrong with my code (maybe
in the collision mesh construction), and I'd
be very grateful if somebody looking the following snippets
could point me in the right direction.

I'm sorry for the length of this post, I've put more code
to clarify the context. You can just ignore the Ogre bits.


1) Bullet Collision mesh/shapes creation for my world
===============================================
I know that the mesh triangle information is correct
because I use it succesfully with my second mouse
picking implementation. I'm not sure though that addTriangle
takes the vertices in this order, just a guess.


void LoadGameState::createCollisionWorld()
{
btVector3 worldAabbMin(-2000,-2000,-2000);
btVector3 worldAabbMax(2000,2000,2000);

btCollisionDispatcher* dispatcher = new btCollisionDispatcher;
btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax);
mColWorld = new btCollisionWorld(dispatcher,broadphase);



Ogre::Node::ChildNodeIterator child_i = mSceneMgr->getRootSceneNode()->getChildIterator();
while (child_i.hasMoreElements()) {
SceneNode* node = static_cast<SceneNode*>(child_i.getNext());
Ogre::String nodename = node->getName();
LogManager::getSingleton().logMessage(String("Found Scene sub node : ") + nodename);

// if the node name starts with H this is a house,
// so create a static mesh collision shape for it
if(nodename[0] == 'H') {
LogManager::getSingleton().logMessage(String("node : ") + nodename + String(" is a HOUSE, creating mesh collision"));

// Get the entity attached to this node
// By convention, I'll attach a single entity to a node so get indice 0
Ogre::Entity *ent = static_cast<Ogre::Entity *> (node->getAttachedObject(0));

btTriangleMesh * myTriMesh = new btTriangleMesh();

size_t vertex_count;
size_t index_count;
Ogre::Vector3 *vertices;
unsigned long *indices;

getMeshInformation(ent->getMesh().getPointer(),
vertex_count,
vertices,
index_count,
indices,
ent->getParentNode()->getWorldPosition(),
ent->getParentNode()->getWorldOrientation(),
ent->getParentNode()->getScale());

// I have the mesh info, now browse it to create the collision mesh
for (int i = 0; i < static_cast<int>(index_count); i += 3) {
Ogre::Vector3 o1, o2, o3;
o1 = vertices[indices];
o2 = vertices[indices[i+1]];
o3 = vertices[indices[i+2]];
btVector3 b1, b2, b3;
b1.setX(o1.x);
b1.setY(o1.y);
b1.setZ(o1.z);
b2.setX(o2.x);
b2.setY(o2.y);
b2.setZ(o2.z);
b3.setX(o3.x);
b3.setY(o3.y);
b3.setZ(o3.z);

myTriMesh->addTriangle(b1,
b2,
b3);
}

delete [] vertices;
delete [] indices;

btCollisionShape* myShape = new btBvhTriangleMeshShape(myTriMesh);
btCollisionObject *myObject = new btCollisionObject();
myObject->setCollisionShape(myShape);
myObject->setUserPointer(static_cast<void *> (ent));
mColWorld->addCollisionObject(myObject);

// is there anything to do here with BULLET to get it working?

} // nodename[0] = 'H'
}
}
...

2) Mouse picking
=================
bool
LoadGameState::raycastWorldBullet(Real x, Real y, Entity **ptr, Ogre::Vector3 &intersection)
{
// raycast to see if we're in an object or a NPC,
btVector3 rayFromWorld;
btVector3 rayToWorld;
Vector3 ray3;

// Fill the 2 vectors
// have to convert to and fro Ogre and bullet vectors
Ray mouseRay = mCamera->getCameraToViewportRay(x,y);
ray3 = mouseRay.getOrigin();
rayFromWorld.setX(ray3.x);
rayFromWorld.setY(ray3.y);
rayFromWorld.setZ(ray3.z);

// I assume that the second vector is the destination point,
// not the direction vector.
//ray3 = mouseRay.getDirection();
ray3 = mouseRay.getPoint(5000.0);
rayToWorld.setX(ray3.x);
rayToWorld.setY(ray3.y);
rayToWorld.setZ(ray3.z);

btCollisionWorld::ClosestRayResultCallback rayCallback(rayFromWorld,
rayToWorld);

mColWorld->rayTest(rayFromWorld,
rayToWorld,
rayCallback);

if (rayCallback.HasHit())
{
btCollisionObject *myObject = rayCallback.m_collisionObject;
*ptr = static_cast<Ogre::Entity *>(myObject->getUserPointer());
intersection.x = rayCallback.m_hitPointWorld.getX();
intersection.y = rayCallback.m_hitPointWorld.getY();
intersection.z = rayCallback.m_hitPointWorld.getZ();
return true;
}
return false;
}

Again I know that the mouse coordinates are OK. But HasHit() always returns
false.

What am I doing wrong?

Thank's a lot for your help.

-pogn
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: raycastting to static mesh (polygon soup)

Post by Erwin Coumans »

Have you played with the Bullet demos? They do picking of objects using raycast. Usually, moving objects in Bullet are convex or compounds of convexes (sphere, box, cylinder, cone, convex hull, etc). However, the MovingConcaveDemo can do picking of moving concave meshes, using GIMPACT. You can do raycast against large triangle meshes, but it hasn't been optimized in current Bullet. It should use the BVH tree, but it doesn't yet.

- Most important: did you hookup btIDebugDrawer to Ogre3D wireframe? This should give clues wether the data is correctly added to Bullet. It displays AABB bounding boxes, and wireframe of Bullet collision shapes.
- Did you try picking simple boxes or spheres first?
- It's a good idea to keep the worldspace bounds large enough, to include your raycast coordinate. Right now, you use 2000, and start the raycast outside of this (5000).
- which version of Bullet?
- Where do you set the position of the static mesh?

Code: Select all

 btVector3 worldAabbMin(-2000,-2000,-2000);
btVector3 worldAabbMax(2000,2000,2000); 
 btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax); 

//later:
ay3 = mouseRay.getPoint(5000.0); 
Hope this helps,
Good luck,
Erwin

PS: to better support such case, it would be useful to have a COLLADA Physics snapshot. I plan to provide such util for Bullet, so you can just send us a COLLADA .dae file.
pognibene wrote:Hi all,

I'm trying to use bullet to implement static objects picking
using raycasts (like a house or a tree). This is using the Ogre engine.
Unfortunately, I can't find any intersections with bullet,
while I find them with a simplistic test-all-triangles
approach.

There's probably something wrong with my code (maybe
in the collision mesh construction), and I'd
be very grateful if somebody looking the following snippets
could point me in the right direction.

[...]

What am I doing wrong?

Thank's a lot for your help.

-pogn
pognibene
Posts: 6
Joined: Sat Dec 16, 2006 2:53 pm

Re: raycastting to static mesh (polygon soup)

Post by pognibene »

Erwin Coumans wrote:Have you played with the Bullet demos? They do picking of objects using raycast. Usually, moving objects in Bullet are convex or compounds of convexes (sphere, box, cylinder, cone, convex hull, etc). However, the MovingConcaveDemo can do picking of moving concave meshes, using GIMPACT. You can do raycast against large triangle meshes, but it hasn't been optimized in current Bullet. It should use the BVH tree, but it doesn't yet.
I had a look at the demos of course, but they all use dynamic world while
I'm just using collisions. I was wondering if the Collision objects had to be
initialized differently in this case. Also, my objects don't move -excepted
the player, but I'll use a cylinder approximation for his/her shape.
- Most important: did you hookup btIDebugDrawer to Ogre3D wireframe? This should give clues wether the data is correctly added to Bullet. It displays AABB bounding boxes, and wireframe of Bullet collision shapes.
Not yet, but that's what I'm going to implement now ;-)
- Did you try picking simple boxes or spheres first?
- It's a good idea to keep the worldspace bounds large enough, to include your raycast coordinate. Right now, you use 2000, and start the raycast outside of this (5000).
- which version of Bullet?
- Where do you set the position of the static mesh?
I did not try picking simple objects first, because I don't have this
kind of objects in my scene. Will try with a cube maybe for a simpler
test. As for the raycast bounds, good idea, I'll change it. Bullet is version
2.38. The mesh is loaded, translated, scaled and rotated before
the collision meshes are built, and does not change afterwards.
Everything is static in the scene but the player and the NPCs.

Anyway, thank you for your help, I'll dig deeper in the code tonight.

Pascal
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Post by Erwin Coumans »

I had a look at the demos of course, but they all use dynamic world while
I'm just using collisions. I was wondering if the Collision objects had to be
initialized differently in this case.
Bullet btRigidBody is derived from btCollisionObject. And btDiscreteDynamicsWorld is derived from btCollisionWorld. This should just work. The VehicleDemo performs 4 raycasts on a landscape, for the wheels/tyres.

Please let us know if you find out more about the issue,
Erwin
User avatar
jacmoe
Posts: 9
Joined: Fri Aug 05, 2005 2:59 pm

Post by jacmoe »

It doesn't work because the debug drawing is only called by the dynamic world through the call to dynamicWorld->update.
The debug drawer is not used/called anywhere else.
So I tried to just update the simulation - knowing that I didn't have any bodies to simulate - but to see if the debug interface was being called.
And the world is crashing when you are adding collision objects to it without physical bodies/properties.
So, the only way to have debug visualization is to roll your own.
Which is not difficult: just look at what the dynamic world is doing, and do the same. :wink:

Note: All this is off the top of my head, as I am on Christmas *leave*. :wink:

By the way: Merry Christmas! :)
pognibene
Posts: 6
Joined: Sat Dec 16, 2006 2:53 pm

Post by pognibene »

Hi,
I've made some progress on the subject. Unfortunately I'm
still having blocking issues but here's the story:

-First, my AABB were wrong. I was creating
the bt shapes with the transformed world coordinates
so there was no need to set a transformation matrix
using setWorldTransform. However, this was resulting
in null sized bbox because the default transformation
matrix seems to be filled with 0 :-(

Once this was fixed, I had correct AABBs for my
collision objects. When ray casting, I get an intersection with
the mesh AABB (I'm using a simple cube with 12 triangles),
but I then get a crash. Here's the gdb stacktrace (on Linux):

==============================================

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1240217920 (LWP 18732)]
0x0807bbb4 in btBvhTriangleMeshShape::processAllTriangles(btTriangleCallback*, btVector3 const&, btVector3 const&) const::MyNodeOverlapCallback::processNode ()
at src/BulletCollision/NarrowPhaseCollision/../../LinearMath/btScalar.h:86
86 SIMD_FORCE_INLINE btScalar btSqrt(btScalar x) { return sqrtf(x); }
(gdb) backtrace
#0 0x0807bbb4 in btBvhTriangleMeshShape::processAllTriangles(btTriangleCallback*, btVector3 const&, btVector3 const&) const::MyNodeOverlapCallback::processNode ()
at src/BulletCollision/NarrowPhaseCollision/../../LinearMath/btScalar.h:86
#1 0x0807ee81 in btOptimizedBvh::walkStacklessTree (this=0x8dbef40, rootNode=0xa949f54, nodeCallback=0xbfb478e8,
aabbMin=@0xbfb47b8c, aabbMax=@0xbfb47b7c) at src/BulletCollision/CollisionShapes/btOptimizedBvh.cpp:253
#2 0x0807ef22 in btOptimizedBvh::reportAabbOverlappingNodex (this=0x8dbef40, nodeCallback=0xbfb478e8,
aabbMin=@0xbfb47b8c, aabbMax=@0xbfb47b7c) at src/BulletCollision/CollisionShapes/btOptimizedBvh.cpp:213
#3 0x0807be5a in btBvhTriangleMeshShape::processAllTriangles (this=0x93c4b48, callback=0xbfb47ab4,
aabbMin=@0xbfb47b8c, aabbMax=@0xbfb47b7c)
at src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp:118
#4 0x080717df in btCollisionWorld::rayTestSingle (this=0x8b3e2d8, rayFromTrans=@0xbfb47c6c,
rayToTrans=@0xbfb47c2c, collisionObject=0xa947ec0, collisionShape=0x93c4b48, colObjWorldTransform=@0xa947ec0,
resultCallback=@0xbfb4812c) at src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp:283
#5 0x08071a7d in btCollisionWorld::rayTest (this=0x8b3e2d8, rayFromWorld=@0xbfb481a0, rayToWorld=@0xbfb48190,
resultCallback=@0xbfb4812c) at src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp:344
#6 0x0806930e in CollisionManager::raycastWorldBullet (this=0x8d9eef0, x=0.767499983, y=0.423333347,
ptr=0xbfb48248, intersection=@0xbfb48224) at CollisionUtils.cpp:552
#7 0x0805ada2 in LoadGameState::update (this=0x80f4b00, lTimeElapsed=3) at LoadGameState.cpp:198
#8 0x0804fe26 in GameManager::startGame (this=0x80bb610, gameState=0x80bb648) at GameManager.cpp:93
#9 0x0804ec01 in main () at main.cc:17

==============================================

I've tried also to do mesh collisions. I have one mesh (this cube)
and a cylinder. This crashes as well as soon as there's a contact,
with a very similar error:

==============================================
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1240287552 (LWP 18466)]
0x0807ba74 in btBvhTriangleMeshShape::processAllTriangles(btTriangleCallback*, btVector3 const& , btVector3 const&) const::MyNodeOverlapCallback::processNode ()
at src/BulletCollision/NarrowPhaseCollision/../../LinearMath/btScalar.h:86
86 SIMD_FORCE_INLINE btScalar btSqrt(btScalar x) { return sqrtf(x); }
(gdb) backtrace
#0 0x0807ba74 in btBvhTriangleMeshShape::processAllTriangles(btTriangleCallback*, btVector3 const&, btVector3 const&) const::MyNodeOverlapCallback::processNode ()
at src/BulletCollision/NarrowPhaseCollision/../../LinearMath/btScalar.h:86
#1 0x0807ed41 in btOptimizedBvh::walkStacklessTree (this=0xa949738, rootNode=0xa949f6c,
nodeCallback=0xbf8fdb28, aabbMin=@0xa9f9620, aabbMax=@0xa9f9630)
at src/BulletCollision/CollisionShapes/btOptimizedBvh.cpp:253
#2 0x0807ede2 in btOptimizedBvh::reportAabbOverlappingNodex (this=0xa949738,
nodeCallback=0xbf8fdb28, aabbMin=@0xa9f9620, aabbMax=@0xa9f9630)
at src/BulletCollision/CollisionShapes/btOptimizedBvh.cpp:213
#3 0x0807bd1a in btBvhTriangleMeshShape::processAllTriangles (this=0x82bbc20,
callback=0xa9f9614, aabbMin=@0xa9f9620, aabbMax=@0xa9f9630)
at src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp:118
#4 0x08078b33 in btConvexConcaveCollisionAlgorithm::processCollision (this=0xa9f9608,
body0=0x93c5dc0, body1=0xa92a740, dispatchInfo=@0x8c9e59c, resultOut=0xbf8fdbd8)
at src/BulletCollision/CollisionDispatch/btConvexConcaveCollisionAlgorithm.cpp:189
#5 0x0806e294 in btCollisionDispatcher::defaultNearCallback (collisionPair=@0xa9f95f8,
dispatcher=@0x93c1ed0, dispatchInfo=@0x8c9e59c)
at src/BulletCollision/CollisionDispatch/btCollisionDispatcher.cpp:346
#6 0x0806f6c8 in btCollisionPairCallback::processOverlap (this=0xbf8fdd08, pair=@0xa9f95f8)
at src/BulletCollision/CollisionDispatch/btCollisionDispatcher.cpp:303
#7 0x0806ccc6 in btOverlappingPairCache::processAllOverlappingPairs (this=0x92bac90,
callback=0xbf8fdd08)
at src/BulletCollision/BroadphaseCollision/btOverlappingPairCache.cpp:178
#8 0x0806e341 in btCollisionDispatcher::dispatchAllCollisionPairs (this=0x93c1ed0,
pairCache=0x92bac90, dispatchInfo=@0x8c9e59c)
at src/BulletCollision/CollisionDispatch/btCollisionDispatcher.cpp:316
#9 0x08071b93 in btCollisionWorld::performDiscreteCollisionDetection (this=0x8c9e588)
at src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp:144
#10 0x0806957a in CollisionManager::computeCollisions (this=0x8f26ef8)
at CollisionUtils.cpp:707
#11 0x08065e76 in Player::collideHuman (this=0x8b7ec60, startPos=@0xbf8fe6d0,
endPos=@0xbf8fe6dc, radius=0.300000012) at Player.cpp:143
#12 0x08066030 in Player::move (this=0x8b7ec60, ltime=4) at Player.cpp:94
#13 0x0805aef3 in LoadGameState::update (this=0x80f4b00, lTimeElapsed=4)
at LoadGameState.cpp:241
#14 0x0804fe26 in GameManager::startGame (this=0x80bb610, gameState=0x80bb648)
#15 0x0804ec01 in main () at main.cc:17

==============================================

Maybe that this crash is what Jacmoe is talking of in then previous post.
I create my collision world this way:

btVector3 worldAabbMin(-10000,-10000,-10000);
btVector3 worldAabbMax(10000,10000,10000);

btCollisionDispatcher* dispatcher = new btCollisionDispatcher;
btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax);
mColWorld = new btCollisionWorld(dispatcher,broadphase);

Then add collision objects this way:
(build first the vertex array)
...
btCollisionShape *newshape = new btBvhTriangleMeshShape( index_vert_arrays );
...
Vector3 pos = node->getWorldPosition();
Quaternion q = node->getWorldOrientation();
btTransform trans;
trans.setRotation( btQuaternion( q.x, q.y, q.z, q.w ) );
trans.setOrigin( btVector3( pos.x, pos.y, pos.z ) );

// Create and add collision object
btCollisionObject *myObject = new btCollisionObject();
myObject->setCollisionShape(newshape);
myObject->setUserPointer(static_cast<void *> (ent));
myObject->setCollisionFlags(btCollisionObject::CF_STATIC_OBJECT);
myObject->setWorldTransform(trans);
mColWorld->addCollisionObject(myObject);

So I just use bare CollisionObject.

Hope you'll have a few minutes to have a look and see
if the problem is this code or in Bullet ;-)

Thank you for your help.

Pascal
DeusFacticius
Posts: 1
Joined: Wed Mar 04, 2009 6:38 am

Re: raycastting to static mesh (polygon soup)

Post by DeusFacticius »

I know this post is old, but has a solution been found to the crash described above? Even now, 3 years later, I've come across almost this exact crash and haven't been able to find any solution for it. Conditions are similar, using btBvhTriangleMeshShape created from an Ogre mesh, and crashes on the rigid body's first collision.

Also using linux, particularly 64bit, affects both Fedora 9 and 10. Has not been tested on windows.

This code is particularly frustrating to debug because btBvhTriangleMeshShape::processTriangles declares and uses a struct inside the method body, which apparently seems to confuse GDB and is unable to locate the source file for the internal struct (MyNodeOverlapCallback), thus I have no debugging information at the site of the actual crash.

So has anyone else experienced this and found a solution?
User avatar
PoV
Posts: 3
Joined: Sun Nov 13, 2011 1:40 am
Location: London, Ontario, Canadia

Re: raycastting to static mesh (polygon soup)

Post by PoV »

DeusFacticius wrote:I know this post is old, but has a solution been found to the crash described above? Even now, 3 years later, I've come across almost this exact crash and haven't been able to find any solution for it. Conditions are similar, using btBvhTriangleMeshShape created from an Ogre mesh, and crashes on the rigid body's first collision.
Like above, I get a segmentation fault (SIGSEGV) on the first collision. *HOWEVER*, it's only when I'm running GDB. If I run without GDB, there's no segfault. The code runs totally fine on both Windows and Android. It's only when GDB is brought in to the picture things get nasty (that's not to say there isn't a problem though, just one that's recoverable).

My other object is a btConvexHullShape. Also, I am not using Ogre.

Error as reported by GDB is as follows:

Code: Select all

Program received signal SIGSEGV, Segmentation fault.
0x00450ce7 in btBvhTriangleMeshShape::processAllTriangles(btTriangleCallback*, btVector3 const&, btVector3 const&) const::MyNodeOverlapCallback::processNode(int, int) ()

btBvhTriangleMeshShape::MyNodeOverlapCallback::processNode (this=0x28fa64, nodeSubPart=0, nodeTriangleIndex=3093)
    at src/External/Bullet/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp:299

graphicsbase[2]*meshScaling.getZ());
The mesh has 4275 vertices, 8192 faces, which makes the index noted above an unusual place to die.

The backtrace is:

Code: Select all

#0  0x00450ce7 in btBvhTriangleMeshShape::processAllTriangles(btTriangleCallback*, btVector3 const&, btVector3 const&) const::MyNodeOverlapCallback::processNode(int, int) ()
#1  0x0042fdf0 in btQuantizedBvh::walkStacklessQuantizedTree(btNodeOverlapCallback*, unsigned short*, unsigned short*, int, int) const ()
#2  0x0043007c in btQuantizedBvh::reportAabbOverlappingNodex(btNodeOverlapCallback*, btVector3 const&, btVector3 const&) const ()
#3  0x004510d0 in btBvhTriangleMeshShape::processAllTriangles(btTriangleCallback*, btVector3 const&, btVector3 const&) const ()
#4  0x00442626 in btConvexConcaveCollisionAlgorithm::processCollision(btCollisionObject*, btCollisionObject*, btDispatcherInfo const&, btManifoldResult*) ()
#5  0x00437c78 in btCollisionDispatcher::defaultNearCallback(btBroadphasePair&,btCollisionDispatcher&, btDispatcherInfo const&) ()
#6  0x005de8fb in btCollisionPairCallback::processOverlap(btBroadphasePair&)    ()
#7  0x0042b259 in btHashedOverlappingPairCache::processAllOverlappingPairs(btOverlapCallback*, btDispatcher*) ()
#8  0x004377f2 in btCollisionDispatcher::dispatchAllCollisionPairs(btOverlappingPairCache*, btDispatcherInfo const&, btDispatcher*) ()
#9  0x00438ed1 in btCollisionWorld::performDiscreteCollisionDetection() ()
#10 0x004a9dce in btDiscreteDynamicsWorld::internalSingleStepSimulation(float)    ()
#11 0x004a8726 in btDiscreteDynamicsWorld::stepSimulation(float, int, float)    ()
#12 0x00537bc5 in cPhysics::Step() ()
#13 0x0052d2b4 in cGame::Step() ()
#14 0x00547b5d in SDL_main ()
#15 0x00597938 in console_main ()
#16 0x005979b7 in WinMain@16 ()
#17 0x005980f6 in main ()
User avatar
PoV
Posts: 3
Joined: Sun Nov 13, 2011 1:40 am
Location: London, Ontario, Canadia

Re: raycastting to static mesh (polygon soup)

Post by PoV »

Tracing in to the code, graphicsindex is coming up as -17,891,602 (FEEE FEEE). Yep, there's certainly no vertex -17,891,602. :)

According to this, 0xFEEEFEEE marks free'd memory.

Doesn't seem to matter what hits the btBvhTriangleMeshShape, graphicsindex comes up as 0xFEEEFEEE.

* * *

EDIT: The generated address of gfxbase is fine. Just the data that indexbase used to point to is no longer there.

The data inside m_meshInterface (a btStridingMeshInterfaceData) is being deleted and not reassigned somewhere.

* * *

EDIT2: Disabling the BVH (DISABLE_BVH) doesn't help. It's something at a level higher than BVH that is deleting the data m_meshInterface points to.
User avatar
PoV
Posts: 3
Joined: Sun Nov 13, 2011 1:40 am
Location: London, Ontario, Canadia

Re: raycastting to static mesh (polygon soup)

Post by PoV »

Okay solved. My fault.

btTriangleIndexVertexArray was giving me trouble back when I was first starting out. My meshes are all under 32k verts, so I store the indexes as shorts. btTriangleIndexVertexArray doesn't like non-ints for indexes. So I added a temporary hack upon creation, a dummy index array that held all the vertices as integers instead of shorts.

Internally, btBvhTriangleMeshShape stores this as a btStridingMeshInterfaceData. The btStridingMeshInterfaceData contains no data, but simply instructions for how to read it, and where it can be found. In my case, it was created locally, then as the function dropped through braces {} the data was gone. Well, not entirely gone, but the memory was officially released. I just lucked out and the data was never overwritten. *phew*

So, to anyone that gets a segfault when dealing with btBvhTriangleMeshShape: Make sure the original data passed to btBvhTriangleMeshShape still exists, at the EXACT SAME address! For example, an std::vector.push_back could bite you if you're not careful.

Signing off.