Drawing bodies and fixtures

General discussion about the R.U.B.E editor
rileyrg
Posts: 54
Joined: Sun Jul 19, 2015 11:42 am

Drawing bodies and fixtures

Post by rileyrg »

Rube does a nice job of pretending there is only one fixture for a complex shape. But when we load Rube obviously there are many fixtures created in box2d whose total make the complex fixture we see in RUBE. But whats the best way to work out where to draw my sprite/texture and at what angle for that fixture? Does someone have some Java or pseudo to explain it to me please. Before hand I simply had a body for most of characters and I simply drew the texture at the body position and angle but with complex fixtures I'm a little lost. (I'm using libgdx if that's any help). Possibly the snippet that renders bodies in the rube editor itself? e.g see body2 fixture2. How do I work out the position and angle of my watering can texture at fixture2. Image
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Drawing bodies and fixtures

Post by iforce2d »

The exported .json contains the position and angle for each image. If the image is not attached to a body this will be an absolute position and angle in the world. For images attached to a body, it will be the position and angle relative to the body it's attached to, so when drawing the image you'll need to continuously update it by checking the body position and angle.

Typically I think you would first set the position and angle of the image as if it was not attached to a body (you could also think of it as being attached to a body with position 0,0 and angle 0). Then rotate and move it from that location using the body position and angle. Doesn't the RubeLoader handle this stuff, or are you using a different method now? Maybe the RubeLoader source has something useful.

For fixtures, you should be able to use the regular debug draw of Box2D to display them - this is the best way to see what Box2D is really doing.
rileyrg
Posts: 54
Joined: Sun Jul 19, 2015 11:42 am

Re: Drawing bodies and fixtures

Post by rileyrg »

Thanks but this hasn't really addressed the question *I think* :D fixture2 in the diagram (in rube editing) maps to multiple fixtures in box2d. The position of the image for that fixture (if I were to bother to add one which I did here for clarity) in rube is one thing but at runtime when the box2d world is running the position of the multiple fixtures which make up "fixture2" in rube changes and I cant see how to get the correct position for the texture or sprite I wish to use "in game" for fixture2 (in rube) which corresponds to about 16 fixtures in box2d.

Hopefully that is clearer or possibly I am just confusing myself here.
rileyrg
Posts: 54
Joined: Sun Jul 19, 2015 11:42 am

Re: Drawing bodies and fixtures

Post by rileyrg »

Aha ! Wait. I think I have it. What you're saying is I *should* attach an image and position it over the fixture in order to then read that at load time and get the relative position to the body center. Even if I dont actually use the same image in game. And I need to do that because the "virtual" fixture we see in rube isn't there at rube loader time. I need to query the images and find it by name and then store its position to get the fixture position thats really modelled by more than one polygon shape in any complex fixture shape.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Drawing bodies and fixtures

Post by iforce2d »

No, what I'm saying is that Box2D has a feature called "debug draw" which will draw the fixtures for you with a few lines of code: http://www.iforce2d.net/b2dtut/debug-draw
I would be surprised if libgdx does not have some way to use this because it's the most fundamental tool you have when working with Box2D - without it you are pretty much blindfolded.
Incidentally, the screenshot you posted above of the player view in RUBE was drawn with the debug draw.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Drawing bodies and fixtures

Post by iforce2d »

For libgdx, perhaps this might be useful: http://rotatingcanvas.com/using-box2d-d ... in-libgdx/
rileyrg
Posts: 54
Joined: Sun Jul 19, 2015 11:42 am

Re: Drawing bodies and fixtures

Post by rileyrg »

I already know how to see my shapes - libgdx has its own debug tracing of b2d fixtures. Thats not an issue. My issue is how I find the positions of the "virtual" fixtures in rube (made up of N smaller ones in real b2d : the watering can in my OP) so that I can draw my own textures over them. I think the solution seems to be what I mentioned above : connect an image in rube even though I wont use that image in my in game rendering since I'll be using animated textures from a spritesheet. Am I making any sense at all? :)
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Drawing bodies and fixtures

Post by iforce2d »

Well that's quite different. First thing to take a look at would be the source code of whatever is doing the debug draw for you already.
I still don't quite understand why all the vertices of the fixtures are necessary to draw an animated texture though. Don't you just draw the frames of the animated texture where the teapot image is in your screenshot?
rileyrg
Posts: 54
Joined: Sun Jul 19, 2015 11:42 am

Re: Drawing bodies and fixtures

Post by rileyrg »

This is still my question. Where is the watering can? Its a watering can not a tea pot... ;) That watering can is not a body. It has no position in b2d. In rube its represented as a virtual or logical fixture. In a b2d world its about 16 different fixtures. How do I know where to draw my textures for that "logical fixture"? Its seems what you're saying is I must add an image in rube and use the position of that and then retranslate that pos to the position and rotation of the current body to which that fixture belongs. But I have zero idea how to do that. I'm kind of at a loss on how to explain this now - my "b2d vocabulary" is weak.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Drawing bodies and fixtures

Post by iforce2d »

Did you look at the debug draw code of libgdx?
Could you give an example of what kind of textures you are trying to draw? I don't understand why you need to know where the vertices of the underlying physics shapes are, to draw an image over the top of it.
For a single rectangular texture ie. the typical case, the image position can be found by looking at the current position and angle of the body that the image is attached to, and offsetting the image from there. ah, but now we're rehashing our earlier posts...

I'm assuming you have some idea of how to draw things in libgdx because I have no idea whatsoever, but I was under the impression that it uses a canvas similar to Javascript (at least for Android I think that's how the underlying implementation would be). Perhaps this little psuedo-code will help. To draw an image that is NOT attached to a body, we use just the position and rotation of the image only:

Code: Select all

context.save();
        context.translate( img.x, img.y );
        context.rotate( img.angle );
        ... draw the image ...
context.restore();
To draw an image that IS attached to a body, you would first apply a translate and rotate to where the body is:

Code: Select all

context.save();
        context.translate( body.x, body.y );
        context.rotate( body.angle );
        context.translate( img.x, img.y );
        context.rotate( img.angle );
        ... draw the image ...
context.restore();
If you are drawing some other kind of texture, for example you might want to repeat textures for a ground body using automatic texture coordinate generation, then yes you would need to know where the vertices of the fixtures are. Perhaps that is what you're doing?
Post Reply