Is this efficient use of userdata?

Discuss issues specific the Java port of Box2D
ff7squirrelman
Posts: 2
Joined: Sat Mar 07, 2015 11:08 am

Is this efficient use of userdata?

Postby ff7squirrelman » Sat Mar 07, 2015 11:42 am

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:

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.

kilosi
Posts: 2
Joined: Sun Mar 15, 2015 11:37 am

Re: Is this efficient use of userdata?

Postby kilosi » Sun Mar 15, 2015 3:41 pm

You can cut down on the number of if statements to one if you use polymorphish. Either an abstract super class or interface that implements 'loop'. In this case, it looks like an interface would work best for you.

So it would look something like this:

Code: Select all

if(obj instanceof Looper)
{
    Looper looper = (Looper)obj;
    looper.loop();
}


All your other classes such as PlayerShip, BasicBeam, etc, would implement the Looper interface:

Code: Select all

public interface Looper
{
     public void loop();
}


This has the added benefit that you'll never need to touch this if statement piece of code again as you continually add new UserDatas that are Loopers :)

ff7squirrelman
Posts: 2
Joined: Sat Mar 07, 2015 11:08 am

Re: Is this efficient use of userdata?

Postby ff7squirrelman » Tue Mar 24, 2015 12:56 am

Thanks for the reply, actually they are all implementations of an abstract class since I was originally trying just that but it wasn't working. Maybe I did something wrong with my implementation or interfaces work better. I haven't gotten to work on it much lately but I'll look into interfaces more when I get a chance. ty


Return to “Java”



Who is online

Users browsing this forum: No registered users and 2 guests