In several box2d code samples I've seen this line of code:
body->SetUserData(self);
In my search I have not found any explanation for this. What is the main purpose for setting the userdata to self in box2d?
In several box2d code samples I've seen this line of code:
body->SetUserData(self);
In my search I have not found any explanation for this. What is the main purpose for setting the userdata to self in box2d?
Usually you assign the visual object (ie a sprite) to the userdata object of the Box2D body for contact listeners.
In the case of a contact callback, you only receive the box2d objects. Therefore you get the contact's bodies and from the body the userdata, in order to send messages to the sprite that represents the body visually.
For example if you want to run an animation on the sprite when it collides.