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.
Adding Custom user data to objects (names etc)
-
- Posts: 65
- Joined: Fri May 13, 2011 1:11 pm
-
- Posts: 32
- Joined: Tue Feb 22, 2011 1:00 pm
Re: Adding Custom user data to objects (names etc)
There is a method, for example in the case of rigidBodies:
body->setUserPointer(p_userPointer);
I think you need this one.
body->setUserPointer(p_userPointer);
I think you need this one.
-
- Posts: 65
- Joined: Fri May 13, 2011 1:11 pm
Re: Adding Custom user data to objects (names etc)
So, something like this: (?)
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?
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);
Are my assumptions correct?
-
- Posts: 32
- Joined: Tue Feb 22, 2011 1:00 pm
Re: Adding Custom user data to objects (names etc)
Nearly :
I should work like that. But to be sure, I do this directly on my rigid or soft object.
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);
-
- Posts: 65
- Joined: Fri May 13, 2011 1:11 pm
Re: Adding Custom user data to objects (names etc)
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?
-
- Posts: 32
- Joined: Tue Feb 22, 2011 1:00 pm
Re: Adding Custom user data to objects (names etc)
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):
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
}