For my bodies I set the userdata to a object holding information about said object linking it to it's body and sprite. This seems to be common practice but looking at my code i'm not sure if I am misusing it somehow as it seems like it could get really inefficient later as I add more object types when I check collisions I have to check the userdata using instanceof and a large set of if else statements to cast it to the appropriate class. Here's a simplified example:
As is this works perfectly however I would imagine as I add more object types this if check would become very large. And even more of an issue when I check for every body colliding with every other body using world.getContactList() where there could be hundreds of combinations. Maybe this is just a case of senseless preoptimization though and I shouldn't worry? What do you think? Am I implementing this effectively?
I doubt it changes anything but I am specifically using the version of box2d that is included in the LibGDX framework.
Code: Select all
world.getBodies(Bodies);
for(Body body: Bodies)
{
if(body.getUserData() instanceof PlayerShip)
{
PlayerShip objectsClass = (PlayerShip) body.getUserData();
objectsClass.Loop();
}
else if(body.getUserData() instanceof EnemyShip)
{
EnemyShip objectsClass = (EnemyShip) body.getUserData();
objectsClass.Loop();
}
else if(body.getUserData() instanceof BasicBeam)
{
BasicBeam objectsClass = (BasicBeam) body.getUserData();
objectsClass.Loop();
}
else if(body.getUserData() instanceof BasicBullet)
{
BasicBullet objectsClass = (BasicBullet) body.getUserData();
objectsClass.Loop();
}
}
As is this works perfectly however I would imagine as I add more object types this if check would become very large. And even more of an issue when I check for every body colliding with every other body using world.getContactList() where there could be hundreds of combinations. Maybe this is just a case of senseless preoptimization though and I shouldn't worry? What do you think? Am I implementing this effectively?
I doubt it changes anything but I am specifically using the version of box2d that is included in the LibGDX framework.