Adding Custom user data to objects (names etc)

Karrok
Posts: 65
Joined: Fri May 13, 2011 1:11 pm

Adding Custom user data to objects (names etc)

Post by Karrok »

Hello Bullet,

I was wondering whether it was possible to simple add names (strings) or structs to collision objects.
As an example, ODE has userData that can point to any userconcieved struct/class to contain any additional data the user wants.

This way I can add a name like "sphere_one" to a btSphereCollisionShape or a "this_is_a_cow" to a btBoxShape or whichever.

Is there an easy way to add this semantic information to objects?

Thanks in advance.
XMight
Posts: 32
Joined: Tue Feb 22, 2011 1:00 pm

Re: Adding Custom user data to objects (names etc)

Post by XMight »

There is a method, for example in the case of rigidBodies:

body->setUserPointer(p_userPointer);

I think you need this one.
Karrok
Posts: 65
Joined: Fri May 13, 2011 1:11 pm

Re: Adding Custom user data to objects (names etc)

Post by Karrok »

So, something like this: (?)

Code: Select all

struct SomeData {
   std::string something;
}

btCollisionShape * collshape = ....someshape
btCollisionObject * collobj = new btCollisionObject()

collobj->setCollisionShape(collshape);

SomeData sd = new SomeData();
sd->something = "hello";

collobj->setUserPointer(*uod);
And then I could search through all objects in the collision world, use getUserPointer() to get a pointer to that struct and retrieve the name.

Are my assumptions correct?
XMight
Posts: 32
Joined: Tue Feb 22, 2011 1:00 pm

Re: Adding Custom user data to objects (names etc)

Post by XMight »

Nearly :) :

Code: Select all

struct SomeData {
   std::string something;
}

btCollisionShape * collshape = ....someshape
btCollisionObject * collobj = new btCollisionObject()

collobj->setCollisionShape(collshape);

SomeData* sd = new SomeData();
sd->something = "hello";

collobj->setUserPointer(uod);
I should work like that. But to be sure, I do this directly on my rigid or soft object.
Karrok
Posts: 65
Joined: Fri May 13, 2011 1:11 pm

Re: Adding Custom user data to objects (names etc)

Post by Karrok »

Ok, and how can I then iterate over all objects in the world to find an object that has (for example) "somestring" as user data?
XMight
Posts: 32
Joined: Tue Feb 22, 2011 1:00 pm

Re: Adding Custom user data to objects (names etc)

Post by XMight »

Well, these questions that you post are not so hard to find an answer for in the bullet demos :). I think that you must study them at least a bit :)

Anyway:
In the rendering method you do (or where you want):

Code: Select all

const btCollisionObjectArray* cOA = m_dynamicsWorld->getCollisionObjectArray();
	btCollisionObject* colObj;
	btRigidBody* body;
	btSoftBody* softBody;
	YourStruct* str;

	for(int i = 0; i < cOA->size(); i++)
	{
		colObj = cOA->at(i);
		body = btRigidBody::upcast(colObj);

		if(body != NULL)
		{
			str = (YourStruct*)body->getUserPointer();
                        ...
                        break;
                }
                softBody = btSoftBody::upcast(colObj);
                //same thing here as above
        }