# ofxBullet cdata

Hello there,

I’ve been messing around with Bullet and I’m in the middle of creating a simple game with it. There’s a few things about the ofxBulletCollisionData that interest me. Let’s say I have two vectors of ofxBulletBaseShapes: Circles and Squares and I only care whether a square is colliding with a circle, and not when squares collide with each other. I’m wondering what would be the most efficient way of checking for this

In the ofxBulletCollisionData class, I see a body1 and body2 which I imagine holds the two Bullet rigid bodies that collide. If so, for every collision that ever happens, I could take body2 and iterate through my vector of Circles to see if any of them matches but then this would be done regardless of whether it’s a Circle and a Square colliding or just two Squares with each other. Is this really the best way to do it?

Let me know if anything I said was senseless and I’d really appreciate it if anyone could point me in the right direction

You could store the shape type in the user data of the objects that you create. This will allow you to skip any unnecessary for loops. Make a CustomBulletData that extends ofxBulletUserData that stores the type of the bullet shape. So in your onCollision function.

``````// if you have a ground, return
if(*ground == cdata) return;

CustomBulletData* ud1 = (CustomBulletData*)cdata->userData1;
CustomBulletData* ud2 = (CustomBulletData*)cdata->userData2;
// check if box and box or sphere and sphere
if( ud1 != NULL && ud2 != NULL ) {
if(ud1->type == BOX && ud2->type == BOX) return;
if( ud1->type == SPHERE && ud2->type == SPHERE) return;
if(ud1->type == SPHERE && ud2->type == BOX) {
// sphere and box
}
if(ud1->type == BOX && ud2->type == SPHERE) {
// box and sphere
}
}``````

Ah, the ofxBullet dude himself! Thanks for your work and thanks for dropping by to help out a newbie. That makes sense to me and setting the data seems as simple as calling someShape->setData(pointerToCustomBulletData) upon creation of an object, right? I’ll try and get it working with that line of thinking and see how it goes

Exactly. Make sure to set the user data on the object after you call someShape->create()

Hi NoodleCandy!

I have a similar problem. Did you manage to make it work?
It doesn’t work for me. The strange thing is that the following condition is always true:

Event if I don’t store user data in any object, the pointers to user data are never NULL … why is that?

Here is my code:

``````void collisionExample::onCollision(ofxBulletCollisionData& cdata) {

// read customBulletData from colliding shapes
CustomBulletData* ud1 = (CustomBulletData*)cdata.userData1;
CustomBulletData* ud2 = (CustomBulletData*)cdata.userData2;
if ( ud1 != NULL && ud2 != NULL )
{
cout << "found CustomBulletData" << endl;
}
}``````

RIght now the ofxBulletBaseShape creates a user data object for each shape so that it can compare against other objects like collision data and mouse events.