Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Box2DSprite: easily draw Sprites on Box2D Bodies and Fixtures  (Read 7885 times)
0 Members and 1 Guest are viewing this topic.
Offline dermetfan
« Posted 2013-06-09 16:14:16 »

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:
1  
2  
3  
4  
5  
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:
1  
2  
3  
4  
5  
6  
7  
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.


This is now part of libgdx-utils.


Offline SrFurias

Senior Newbie





« Reply #1 - Posted 2013-06-13 18:53:32 »

The last part of the code, in the render, what is "fixtureToRenderOn"? I don't understand it...
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2013-06-13 19:50:19 »

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.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dermetfan
« Reply #3 - Posted 2013-06-13 21:48:56 »

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 Grin).

Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2013-06-14 13:13:05 »

I changed the name to something longer and more confusing (hopefully not Grin).

How about 'fixture'? Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline dermetfan
« Reply #5 - Posted 2013-06-14 17:51:46 »

I changed the name to something longer and more confusing (hopefully not Grin).

How about 'fixture'? Cheesy

Why didn't I get this idea in the beginning? Clueless

Offline SrFurias

Senior Newbie





« Reply #6 - Posted 2013-06-15 14:32:12 »

I'm having problems with put a image in a body, so, now I will try this method.
Offline dermetfan
« Reply #7 - Posted 2013-06-18 15:40:08 »

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

Offline drabla

Junior Duke


Medals: 5



« Reply #8 - Posted 2013-07-24 22:34:22 »

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<Body>))

I replaced it with the following code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
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)
1  
circleBody.addUserData(spriteFixture); 

and tried both render methods without success.

Do you have an simple example for us?

Kind regards,

Drabla
Offline dermetfan
« Reply #9 - Posted 2013-07-24 23:44:03 »

Thanks for your interest in this Smiley

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.

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

and tried both render methods without success.

Note that the line
1  
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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline drabla

Junior Duke


Medals: 5



« Reply #10 - Posted 2013-07-24 23:55:38 »

Oh my, thank you Smiley Sorry for wasting your time ^^ It's late it guess .>

Everything works perfectly now. Thanks for sharing!

Kind regards,
Drabla
Offline Eamonn

Junior Newbie





« Reply #11 - Posted 2013-08-14 22:52:18 »

Very nice. I'm also very interested in the AnimatedFixtureSprite. Thanks for putting in the time and effort to make these utilities!
Offline Gibbo3771
« Reply #12 - Posted 2013-09-12 23:26:12 »

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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline dermetfan
« Reply #13 - Posted 2013-09-12 23:38:47 »

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.

Offline Gibbo3771
« Reply #14 - Posted 2013-09-12 23:40:09 »

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! Cheesy Cheesy

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Gibbo3771
« Reply #15 - Posted 2013-09-12 23:50:15 »

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.

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 Cheesy

Any ideas?

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline dermetfan
« Reply #16 - Posted 2013-09-13 00:03:35 »

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

will I have to change the package declerations all the time or is there some sort of method to do this automatically

Just don't copy and paste the source in your game Wink

download a jar there and put it in your project like you're used to from other libraries.
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 Grin

Offline Gibbo3771
« Reply #17 - Posted 2013-09-13 00:15:11 »

Tutorials are always welcome Cheesy

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 Cheesy, by properly use I mean appropriate use :p

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

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline dermetfan
« Reply #18 - Posted 2013-09-14 01:12:22 »

Oh great, the organization and java knowledge will come over time Smiley 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 Grin

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (30 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (39 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!