Page 1 of 1

b2Body SetUserData() error

Posted: Fri Aug 23, 2013 9:47 pm
by msm1982
Hi,

when loading in my RUBE scene I am trying to do some extra processing afterwards by creating some specific game objects and attaching them to the appropriate b2Body objects via body->SetUserData(myObj);

When running a world physics step the app crashes with a segmentation fault. This only happens if I try to attach to the body. If attach my user data to one of its fixtures it runs ok.

Referring to the sample loaders, I am doing this in the afterLoadProcessing() method
e.g)

Code: Select all

afterLoadProcessing(b2dJson* json) {
    // this will result in a crash when world->Step() is called first time
    b2Body* body json->getBodyByName("mybody");
    body->SetUserData(myObj);
    // this works
    b2Fixture* fixture json->getFixtureByName("myfixture");
    fixture->SetUserData(myObj);
}
Is there something wrong with trying to attach to the body?

Thanks for the help

Re: b2Body SetUserData() error

Posted: Sat Aug 24, 2013 1:01 am
by iforce2d
Whereabouts does it crash? If you call world->Step immediately after the body->SetUserData line, does it crash then? Box2D does not actually use the user data in any way, so I'm guessing you must be doing something with it yourself when you're stepping (otherwise there is no point to having it), and that is the location where the crash occurs.

Re: b2Body SetUserData() error

Posted: Sat Aug 24, 2013 12:04 pm
by msm1982
ugh,

thanks I assumed it was something stupid I was doing, but just wanted to verify the body userdata wasnt reserved by the scene. Definitely my problem, thanks for the reply