Box2DSprite: easily draw Sprites on Box2D Bodies and Fixtures

I wrote a little subclass of Sprite to make it easier to draw my Sprites at the correct position using Box2D and LibGDX. You can find it on Bitbucket.

Just create a Box2DSprite and put it in the user data of a Body or Fixture and it will draw positioned, sized and rotated according to what you put it in.

Code example:

Body body = world.createBody(bodyDef);
Fixture fixture = body.createFixture(fixtureDef);
Box2DSprite box2DSprite = new Box2DSprite(someSpriteOrTextureYouHave);
body.setUserData(box2DSprite); // will draw on whole body
fixture.setUserData(box2DSprite); // will only draw on the Fixture

To render, simply do:

batch.begin();
// renders all the Box2DSprites in the given Box2D World
Box2DSprite.draw(batch, world);
// renders just this Box2DSprite, but you have to give in the Body or Fixture to render on (this way, it doesn't have to be in the user data)
box2DSprite.draw(batch, body);
box2DSprite.draw(batch, fixture);
batch.end();

Here is a little example Screen:
example

NOTE: To use this, you also need my Box2DUtils class which is dependent on GeometryUtils.


[b]This is now part of [url=http://dermetfan.bplaced.net/libgdx-utils.php]libgdx-utils[/url][/b].

The last part of the code, in the render, what is “fixtureToRenderOn”? I don’t understand it…

I guess it’s pretty much simply the Fixture instance you want to render.
‘fixtureToRenderOn’ might be a little bit mis-named.

Yep, exactly this. Since a FixtureSprite doesn’t hold a reference to a Fixture, you have to provide one. I changed the name to something longer and more confusing (hopefully not ;D).

How about ‘fixture’? :smiley:

Why didn’t I get this idea in the beginning? :clue:

I’m having problems with put a image in a body, so, now I will try this method.

AnimatedFixtureSprite now also available. You need an AnimatedSprite to use it.
Updated original post.
You find basically everything in one place here.

Greetings.

I’m trying to get your code to work but was unable to.

First off: I had to change your draw method in your FixtureSprite.java because world.getBodies(tmpBodies) wouldn’t work (The method getBodies() in the type World is not applicable for the arguments (Array))

I replaced it with the following code:


public static void draw(SpriteBatch batch, World world) {
    Iterator<Body> wIt = world.getBodies();
        while(wIt.hasNext()) {
            Body body = wIt.next();
            for(Fixture fixture : body.getFixtureList())
                if(fixture.getUserData() instanceof FixtureSprite)
                    ((FixtureSprite) fixture.getUserData()).draw(batch, fixture);
         }
}

After this i created a SpriteFixture with a 64x64px Texture and added it to my circleBody (Radius 15F)

circleBody.addUserData(spriteFixture); 

and tried both render methods without success.

Do you have an simple example for us?

Kind regards,

Drabla

Thanks for your interest in this :slight_smile:

You changed the draw method to pretty much what it was before some LibGDX update. I suggest you update to the latest nightlies, it should work without changes then.

Note that the line

world.createBody(bodyDef).createFixture(fixtureDef).setUserData(new FixtureSprite(sprite));

adds the FixtureSprite to the user data of the Fixture, not the Body. This is because it allows to render on each Fixture of a Body seperately, which gives this the name FixtureSprite :wink:

Here’s an example (I also added it to the original post). If the link doesn’t work for some reason, use this one.

Oh my, thank you :slight_smile: Sorry for wasting your time ^^ It’s late it guess .>

Everything works perfectly now. Thanks for sharing!

Kind regards,
Drabla

Very nice. I’m also very interested in the AnimatedFixtureSprite. Thanks for putting in the time and effort to make these utilities!

I am going to be “that guy”.

How do I actually download the source in a zip, the branch gives a totally different commit, can’t see the files anywhere.

Also looks great, new to game development and drawing sprites to the bodies is just cumbersome hehe.

Sorry, the links were a bit outdated.
This is now included in my mini library which grew a bit so it’s in its own repository now. You can download a jar there and put it in your project like you’re used to from other libraries.

Thanks! I am looking forward to using this.

Currently practicing by making games from the past, Pong, Pac-man etc etc, all these little libraries help me keep things neat and lower the confusion! :smiley: :smiley:

I have just one more question.

I will obviously be using this in my own projects, will I have to change the package declerations all the time or is there some sort of method to do this automatically, I mean at the top it has:

package net.dermetfan.libgdx.math;

Which is obviously different from my projects package, but even if I change it, when I go to reuse…well, repeats all over :smiley:

Any ideas?

Glad it helps you :slight_smile: The library also contains other classes that might be useful for you, like the AnimatedSprite (there’s a Box2D version as well).

[quote=“Gibbo3771,post:16,topic:42439”]
Just don’t copy and paste the source in your game :wink:

[quote=“dermetfan,post:14,topic:42439”]
This means:

  • download this
  • move it into the folder called “libs” in your core project
  • right-click the project and go to Properties -> Java Build Path -> Libraries
  • click “Add JARs…”
  • select libgdx-utils.jar from the libs folder

- optional: if you’d like to be able to click the class names with ctrl + click to see the source: download this, click the arrow left to libgdx-utils.jar (which is now in the list), double click “Source attachement” and select libgdx-utils-sources.jar (which you also have to put into your project)

  • click ok and ok again, done

Since you’re starting out with LibGDX, I’m also using this post to shamelessly point you to my tutorials ;D

Tutorials are always welcome :smiley:

I have no issue actually creating a game being honest, I can create side scrolling platformer games without any graphics…only problem I have is how to organize all the code, Java is new to me as well, I have an ok understanding of it but far from expert. Just struggle with some things, once I learn how to properly use classes my code will be more efficient I hope :D, by properly use I mean appropriate use :stuck_out_tongue:

Watching the second episode atm, looking forward to the rest. You sound crazy nervous :stuck_out_tongue:

Oh great, the organization and java knowledge will come over time :slight_smile: It’s basically just OO design.
Really? I actually wasn’t, it was just pretty new to me so I just had to concentrate very much ;D