Question

I'm a beginner Java/LibGDX developer so I'm sorry if I got something terribly wrong here but here's my question:

How could a Box2d body have some object associated with it?

For example a Box2d ball body associated with object that describes how much energy the ball has, what's the charge of the ball, and such.

This might be terribly wrong but here's what I've been trying to do:

I tried to set userdata of the ball to my object like this:

Body.setUserData(MyObject); 

and then when I need it, I would retreat it with:

Body.getUserDate();

which returns an object and I have no idea what to do with it because I don't know how to access it's fields or methods..

I hope you understood the question, if not ill try to modify it.

Thank you.

Was it helpful?

Solution

You have to cast it to your type so you can access it like you're expecting..

MyObject myObject = (MyObject) Body.getUserData();
myObject.property = value;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top