Programming Games for Android

Programming Games for Android 20: Box 2D

42 videos

238 minutes

I have already taught how to do this using bounding boxes collisions, but I know that is not appropriate. I know that just is not the prettiest of writing and most manageable, so we will see a better system than you will like more and that is what we will use, which is Box2D. That library that serves to simulate physical 2D, like gravity, collisions, actions, forces, things that jump, moving at speeds ... all that we let him do Box2d

and we have to do ourselves, and that's fine. As its name suggests, it serves only to physical Box2D 2D. With this is true, because we make the player right forward left, but in a 3D world does not look good. Why it works well here but not always. As Box2D has its own system code, I'll switch to a new screen will be created now. And then we will see how to combine what we do with Box2D with what we have done with Scene2D,

which is where the beauty is. For now I create an empty screen BaseScreen guy, and I will create the builder directly. This display needs, like any display, we load things, now we will see what needs to be charged. It will also have to remove things ... that we will in the dispose (). We'll have to render things, obviously. And I would say nothing more, but we'll see how the thing evolves as. We start making a clear,

because we want to prove that this works: you know that if we do not put the clear, very strange things can happen. Let's see if this looks black, and if it looks black, super. How we would begin working with Box2D? You will sound strange but the first thing we do is create a world so, who does not want the thing. Do not believe worlds every day. I create an instance of the World, called world. Fortunately, the JDK does not bring anything called World

so to make an import to the class of badlogic enough. Let's instantiate the world, and I'll pass it to the constructor. Become the world! It requires two parameters: gravity and a boolean called doSleep. I go with gravity. I do not know when was the last time you just caught a physics book, I hope not long ago, but know that in the world, and throughout the universe, no gravity. On our planet, j -9.81 m / s. This is the force of attraction

exerted by the earth on the bodies, which makes them fall down ... the case is that Box2D has its own gravity, and also lets us put gravity we are interested in, even allowing gravity up, down, borders, those things that would go against the physics of our world, is possible because Box2D It is our center of creativity. However, creativity as we left it at home, I'll have a table that has a gravity as that of the earth.

X = 0, Y = -9.81. Not to do so much calculation with floating I'll tell -10 and will not notice the difference. The second parameter'm going to tell you a true. Why? Because doSleep is a parameter used to tell the system when there is nothing to simulate, do nothing, which is fine because saves CPU time. No reason not to give true, but for what we have, we should pass true. World class is a disposeable so do not forget

call dispose. Now, once we have created the world, we will simulate. To simulate a world, we must call on the renderer that for that is the method which will run 60 times a second, a step. It has two parameters: TimeStep, used to indicate how long we have to simulate. By the time I will spend delta, but then you talk about why this is not a good idea. And finally, velocityIterations and positionIterations,

It used to say how many times you have to iterate through frame. The higher the value, the better it will be simulated but more CPU time that we will use. Here I will provide the parameters 6 and 2. Why? Partly because the documentation says. But what and why? Because no one explains what it does, so I did not know. Box2D documentation supposedly explained but trust me, it does not explain. Let's use 6 and 2 because we will believe, but all that

know is that the higher, the worse performance although better it will iterate. We have created our world. Let's see, to see what he does. What dark everything is, right? Well, because something is missing to make it look the world. And besides, have yet to put things. So let's solve these problems one to one.

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.