btTriangleIndexVertexArray crashing when object collides

Tiago
Posts: 4
Joined: Fri Jan 11, 2013 1:43 pm

btTriangleIndexVertexArray crashing when object collides

Post by Tiago »

Hello, I'm trying to build a terrain (in this case I tried it with only 2 triangles forming a quad) using the btTriangleIndexVertexArray, but it's crashing everytime I collide with it (access violation). Here is the code:

Code: Select all

   int index[] = {0, 1, 2, 2, 1, 3};
   float vertices[] = {0.0, 10.0, 0.0, 20.0, 10.0, 0.0, 0.0, 10.0, 20.0, 20.0, 10.0, 20.0};

	mesh.m_numTriangles = 2;
	mesh.m_triangleIndexBase = (unsigned char*)index;
	mesh.m_triangleIndexStride = 3*sizeof(int);
	mesh.m_vertexBase = (unsigned char*)vertices;
	mesh.m_vertexStride = 3*sizeof(float);
	mesh.m_numVertices = 4;

	btTriangleIndexVertexArray* terrain = new btTriangleIndexVertexArray();
	terrain->addIndexedMesh(mesh);
	btTriangleMeshShape* tmesh  = new btBvhTriangleMeshShape(terrain, true);

	btMotionState* motion=new btDefaultMotionState(trans);
	btRigidBody* body=new btRigidBody(0.0, motion, tmesh);
	world->addRigidBody(body);
user64
Posts: 14
Joined: Fri Jan 28, 2011 9:03 pm

Re: btTriangleIndexVertexArray crashing when object collides

Post by user64 »

use btAlignedAlloc()-btAlignedFree() instead of new-delete
Bullet entities require alignment
"new" may return a pointer to an unaligned memory
check it
Tiago
Posts: 4
Joined: Fri Jan 11, 2013 1:43 pm

Re: btTriangleIndexVertexArray crashing when object collides

Post by Tiago »

Ok, but how should I use it?

Let's say that I want to write this using your method. How should it look like?

Code: Select all

btTriangleMeshShape* tmesh  = new btBvhTriangleMeshShape(terrain, true);
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: btTriangleIndexVertexArray crashing when object collides

Post by xexuxjy »

Have you got your exact example code, as it's a bit hard to tell from that which objects are in and out of scope (e.g. your index and vertices arrays look like they're declared locally...)
Tiago
Posts: 4
Joined: Fri Jan 11, 2013 1:43 pm

Re: btTriangleIndexVertexArray crashing when object collides

Post by Tiago »

Yes, this are all local variables and its inside a function.
PS: this is all the code.
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: btTriangleIndexVertexArray crashing when object collides

Post by xexuxjy »

I'm pretty sure that none of the shapes or mesh objects do a deep copy of the data so you'll probably want to allocate them on the heap and keep a reference to them then because otherwise the pointers to them will be trashed as soon as your function exits...

the Demos/ConcaveDemo/ConcavePhysicsDemo.cpp

has a good example on how to set it up (key point is that gIndices and gVertices are allocated on the heap)
user64
Posts: 14
Joined: Fri Jan 28, 2011 9:03 pm

Re: btTriangleIndexVertexArray crashing when object collides

Post by user64 »

xexuxjy is absolutely right!
I checked the code of Bullet entities from your example.
All of these entities contain BT_DECLARE_ALIGNED_ALLOCATOR() macro that overload new-delete. So you can not worry about alignment.
Tiago
Posts: 4
Joined: Fri Jan 11, 2013 1:43 pm

Re: btTriangleIndexVertexArray crashing when object collides

Post by Tiago »

Yes, that worked!!

Thank you very much! :D