I'm having problems with when getting the user data for fixtures: it works just fine for polygon shapes, but it's returning NULL for circle shapes... I know I must be doing something really stupid, but I just can't see it. This is how I'm creating the shapes:
For a polygon/box:
and for the circle:
I just can't see the problem, here... And in my step function, this is how I'm searching for the user data:
This code works when I create a box or polygon, but when creating a circle, it never enters the inner if (w != nil)...
Any ideas on what I could be doing wrong?
thanks!
rolando.-
For a polygon/box:
Code:
b2PolygonShape box;
CGSize h_size = CGSizeMake(rect.size.width / 2.0f, rect.size.height / 2.0f);
CGPoint center = CGPointMake(rect.origin.x + h_size.width, rect.origin.y - h_size.height);
b2BodyDef bodyDef;
bodyDef.position.Set(center.x/ptm_ratio, center.y/ptm_ratio);
bodyDef.fixedRotation = props.fixedRotation;
b2Body *body = world->CreateBody(&bodyDef);
if (props.numVerts > 0) {
box.Set(props.verts, props.numVerts);
} else {
box.SetAsBox(h_size.width/ptm_ratio, h_size.height/ptm_ratio);
}
// wrapper is just a struct holding some relevant info
b2FixtureDef fixtureDef;
fixtureDef.shape = &box;
fixtureDef.userData = (void *)wrapper;
body->CreateFixture(&fixtureDef);
CGSize h_size = CGSizeMake(rect.size.width / 2.0f, rect.size.height / 2.0f);
CGPoint center = CGPointMake(rect.origin.x + h_size.width, rect.origin.y - h_size.height);
b2BodyDef bodyDef;
bodyDef.position.Set(center.x/ptm_ratio, center.y/ptm_ratio);
bodyDef.fixedRotation = props.fixedRotation;
b2Body *body = world->CreateBody(&bodyDef);
if (props.numVerts > 0) {
box.Set(props.verts, props.numVerts);
} else {
box.SetAsBox(h_size.width/ptm_ratio, h_size.height/ptm_ratio);
}
// wrapper is just a struct holding some relevant info
b2FixtureDef fixtureDef;
fixtureDef.shape = &box;
fixtureDef.userData = (void *)wrapper;
body->CreateFixture(&fixtureDef);
and for the circle:
Code:
b2CircleShape circle;
CGSize h_size = CGSizeMake(rect.size.width / 2.0f, rect.size.height / 2.0f);
CGPoint center = CGPointMake(rect.origin.x + h_size.width, rect.origin.y - h_size.height);
b2BodyDef bodyDef;
bodyDef.position.Set(center.x/ptm_ratio, center.y/ptm_ratio);
bodyDef.fixedRotation = props.fixedRotation;
b2Body *body = world->CreateBody(&bodyDef);
circle.m_radius = h_size.width/ptm_ratio;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.userData = (void *)wrapper;
body->CreateFixture(&fixtureDef);
CGSize h_size = CGSizeMake(rect.size.width / 2.0f, rect.size.height / 2.0f);
CGPoint center = CGPointMake(rect.origin.x + h_size.width, rect.origin.y - h_size.height);
b2BodyDef bodyDef;
bodyDef.position.Set(center.x/ptm_ratio, center.y/ptm_ratio);
bodyDef.fixedRotation = props.fixedRotation;
b2Body *body = world->CreateBody(&bodyDef);
circle.m_radius = h_size.width/ptm_ratio;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.userData = (void *)wrapper;
body->CreateFixture(&fixtureDef);
I just can't see the problem, here... And in my step function, this is how I'm searching for the user data:
Code:
for (b2Body* b = world_->GetBodyList(); b; b = b->GetNext())
{
box2Dwrapper *w = nil;
// search for user data in all fixtures
for (b2Fixture *f = b->GetFixtureList();f;f = f->GetNext()) {
w = (box2Dwrapper *)(f->GetUserData());
if (w)
break;
}
if (w != nil) {
} // has UserData?
} // for each body
{
box2Dwrapper *w = nil;
// search for user data in all fixtures
for (b2Fixture *f = b->GetFixtureList();f;f = f->GetNext()) {
w = (box2Dwrapper *)(f->GetUserData());
if (w)
break;
}
if (w != nil) {
} // has UserData?
} // for each body
This code works when I create a box or polygon, but when creating a circle, it never enters the inner if (w != nil)...
Any ideas on what I could be doing wrong?
thanks!
rolando.-