Programming Games for Android

Programming Games for Android 21: Box 2D DebugRenderer

42 videos

238 minutes

We had left it running, and we had stayed on why we see nothing. Let's see how we can draw the world screen. For that, It depends on how we are using Box2D. In this case we assume that we use Box2D with libgdx, although it can also be used separately. Box2D is a library one uninteresting, because everything is made to work logically. Updated physical, but in the end with Box2D they coordinate positions are obtained,

for use with the objects we have. That is, in the case of normal use of Box2D when you integrate will make calculations and give them to the actors to be drawn. As now we have actors, we need a way to at least see little boxes. In Box2D or JBox2D done by testbeds, but here there libgdx testbeds, but there is a class fully made by libgdx called Box2DDebugRenderer, used to draw a world

per screen. The first thing I created is Box2DDebugRenderer instance. And now what I'll do is instantiate, it's easy because the Box2DDebugRenderer It has no parameters. What has, however, is a nice method called dispose, which I will not forget you for calling. And now I'm going to ask to renderer to draw the world, and this we do after Actualizad physical. Take my advice in this detail: we must update the physical before drawing,

because if not rare things can happen, because what the user sees on the screen does not correspond to the current logic of the world, and that can make it seems the best the character is dead before it can see. Always update before drawing, so we called above all to renderer.render () to be updated. We have two parameters, one is the World, and the other is ... a Matrix4? Where did it out? This is a projection matrix. OpenGL

and in the 3D world, because they have other libraries, to draw things on the screen and to know how big should be things that fit on screen, a projection matrix, which is a thing that tells the graphics card and large things should be drawn is used. This is theoretical because it is pure OpenGL so I will not spend too much time because it is complex to understand if you have never heard of this,

but I need to generate a matrix to tell OpenGL how big should see everything. And for this there are several options, one is to create a SpriteBatch as we have seen, already it brings its own Matrix. The other way is to create it on our own. And now someone will say do I get to create arrays? Maybe some do not even know still use matrices. Or is there someone who can not remember. No matter because libgdx has a class to do this for us.

And in OpenGL it is normal to use cameras. Not like the real world, but data structures with width, height, zoom ... and to our liking generate matrices for use with render, or other things of OpenGL. So first I'm going to create an Orthographic camera type for use in a 2D world. The Orthographic are orthographic projection so we need only for a 2D world. I simply believe, and if the builder should I tell the width and

high that you must have the camera. For now I'll give the dimensions of the screen with Gdx.graphics.getWidth () and Gdx.graphics.getHeight (). Once we have done, what we can do is tell the world render to render the world using the matrix camera, which I access with camera.combined. One more detail: Before working with the camera, the camera must be updated so that it can generate this matrix, because not generated instantly.

This is important if we change the dimensions of the camera when we make the resize, but must be generated somehow. Herein is everything: a Renderer, a camera, and call update. Let's see how the world is drawn, at least to see if it compiles and does not fail. But ... someone has forgotten to light? No, what happens is that there are still bodies that represent. So let's see how to represent the bodies as an actor, enemies,

floor ... Let's see how they do, for example, a character.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.