Programming Games for Android

Programming Games for Android 32: Box 2D and 2D Scene Part 2

42 videos

238 minutes

Remember: we had established a PlayerEntity an Actor. We must now turn to several things. First, you draw it. I said we had a problem here, because Box2D and Scene2D work with different dimensions. Box2D works in meters, and Scene2D in pixels. I I will explain with a drawing. We have our little screen. When we talked about Scene2D I told you that the screen should have a height in pixels. For example, 360.

I can not remember how to erase Skitch. I'll tell 360 pixels high. And then, when we talked about Box2D I said that the height should be in meters, for example, 4 meters screen. So if I draw a box 1 meter x 1 meter must see ... like this is great but imagine that occupies one meter. The problem is that this should be high enough for this to be 360 ​​pixels. The screen should measure both 4 meters and 360 pixels.

The only way to do this is by making 4 meters = 360 pixels, so that is an equivalence. What compels us to establish a conversion between meters and pixels. This is easy and is solved by a division: if 4 meters is 360 pixels, 1 meter will be ... X pixels. 360/4 = 90 So the relationship to establish, It is that 1 meter is 90 pixels. We need to know this because we must convert meters to Box2D pixels Scene2D, to draw it.

Remember we can not create bodies of 90 x 90, because that would be so large objects that would not respect the rules of natural that we establish. But while we can not make players 1 pixel size, because we would not, so we use this equivalence to convert from meters to pixels, or pixels to meters. To create my equivalence relation, I create a class named Constants, where I create a variable called PIXELS_IN_METER,

to indicate that there is one meter 90 pixels. And now, whenever you need to position or resize the actor, I must change the meter read Box2d by this constant to pass to pixels. For example, I'll pass the size to Actor. If the box is 1 meter, it is obvious that its size in pixels it will be 1 * Constants.PIXELS_IN_METER. You could put that * 1, but it does not seem necessary. In fact I'm going to do a static import to use this constant.

Now the box is 90 x 90 pixels. Like, when we go to draw on screen our texture, we need to update the position so we know where It must be drawn from this moment the body. Here is the same. I must say that his position will be body.getPosition (). X, y body.getPosition (). Y. Do not, because we multiplied by the conversion factor to pass it to pixels, which is what interests us. And now we have everything,

we can actually draw on the screen, with the traditional draw method. Let's make one last method, as we are required to eliminate Fixture and the Body, because we have created but remember that must be eliminated. We will make a method called detach, to indicate that we are disengaging the object in the world. And this method will address both destroy Fixture as to destroy the Body. It is important that we call life at the end of the game,

to disappear. Now what I do is create a PlayerEntity, instantiate it ... we'll see what I put there, quiet. I add it to the Stage ... something I do on the show (), in fact, to always done. In the hide () I have to undock the world. I also call remove () to remove the actor Stage. And this is all. What remains to be done? Spend a world, a texture, and an initial position, it will be because I want to, X 1 meter and 2 meters position in the Y position

I still only charge the texture ... how I can load textures? There are easy ways to load textures as we have seen, with the builder. But it is hardly usable because every time I load a texture I want to instantiate. Is not there a better way to do this? Yes there is. We can use AssetManagers to centralize resource load.

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.