Serialization and user pointer

Peter_B
Posts: 2
Joined: Fri May 25, 2012 2:34 am

Serialization and user pointer

Post by Peter_B »

Hi guys,
I'm pretty new to bullet and I'm writing a simple serializer/deserializer. I'm finding that I can serialize/deserialize all my rigidbodies and collision shapes but when I call SetUserPointer with some custom data ( a char* with a GUID in it ) on the object, this user data doesnt seem to serialize and I just get a null pointer on loading for the user pointer. Anyone used this functionality and seen similar results? Presumably, I doing something wrong?

Here is an extract of the code: (Notice I tried to set user pointer on the shape and the rigidbody to no success.
.......

btTriangleMesh* trimesh = new btTriangleMesh();

for ( unsigned int i=0;i<pMeshData->m_positions.size()/3;i+=3)
{
btVector3 vertex1 = btVector3(static_cast<float>(pMeshData->m_positions.mData[0]),static_cast<float>(pMeshData->m_positions.mData[1]),static_cast<float>(pMeshData->m_positions.mData[2]));
btVector3 vertex2 = btVector3(static_cast<float>(pMeshData->m_positions[i+1].mData[0]),static_cast<float>(pMeshData->m_positions[i+1].mData[1]),static_cast<float>(pMeshData->m_positions[i+2].mData[2]));
btVector3 vertex3 = btVector3(static_cast<float>(pMeshData->m_positions[i+2].mData[0]),static_cast<float>(pMeshData->m_positions[i+2].mData[1]),static_cast<float>(pMeshData->m_positions[i+2].mData[2]));

trimesh->addTriangle(vertex1,vertex2,vertex3);
}


bool useQuantizedAabbCompression = true;
btBvhTriangleMeshShape * physicShape = new btBvhTriangleMeshShape(trimesh, useQuantizedAabbCompression);

btTransform shapeTransform;
shapeTransform.setIdentity();

// Create MotionState and RigidBody object for the ground shape
btDefaultMotionState* motionState = new btDefaultMotionState(shapeTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(0, motionState, physicShape, btVector3(0, 0, 0));
btRigidBody* pRigidBody = new btRigidBody(rbInfo);

struct UserData
{
char mName[64];
};
UserData* userData = new UserData();

snprintf (userData->mName,64,"%s",stringGUID);

pRigidBody->getCollisionShape()->setUserPointer(userData);
pRigidBody->setUserPointer(userData);

m_dynamicsWorld->addRigidBody(pRigidBody);
m_collisionShapes.push_back(physicShape);


///serializing the data.

int maxSerializeBufferSize = 1024*1024*1;
btDefaultSerializer* serializer = new btDefaultSerializer(maxSerializeBufferSize);

for (int i=0;i<m_collisionShapes.size();i++)
{
char* name = new char[20];

sprintf(name,"name%d",i);
serializer->registerNameForPointer(m_collisionShapes,name);
}

m_dynamicsWorld->serialize(serializer);

const unsigned char* pBuffer = serializer->getBufferPointer());
const unsigned int worldBufferSize = serializer->getCurrentBufferSize();
MaxDZ8
Posts: 149
Joined: Fri Jun 24, 2011 8:53 am

Re: Serialization and user pointer

Post by MaxDZ8 »

Of course it does not serialize. How would you serialize an opaque blob which you know nothing about? Even supposing we just serialize the pointer value itself, how would we use it - it carries no information at all after loading, it would just be a random number.

You have to enumerate all the pointers yourself, serialize the pointed objects and build a way to restore those associations after loading. Or perhaps while loading, by modifying the provided deserializer code.

EDIT: I checked now and it seems there might be a function doing that for you, btDefaultSerializer provides a function registerNameForPointer , I'm not sure what it does exactly but I'd totally look in that direction!