Programming Games for Android

Programming Games for Android 22: Body and BodyDef

42 videos

238 minutes

Let's see how to create a character in Box2D. Our MiniJoe. To create objects in Box2D need, first create a body, then we need to create a fixture. The documentation speaks in depth of a lot of things: what a body. A body is an object representing an entity in the world. They have speed, position ... but lack of form, which is defined by a fixture, which is the second thing we need. A fixture gives it the way we want to have,

according to the documentation says. Not that I'm inventing anything. But, to represent a character as to minijoe need to create, on the one hand, a Body the Minijoe, and Fixture Minijoe. As you see I'm leaving in this class. But if I get to do this with each of the actors that I have in the system, this will eliminate 40,000 bodies, so we'll see what normal is embutirlo in a data structure as an entity. For now I'll leave it here

because this serves to explain. How to create a Body? It's not as easy as making new Body. Instead, you use the createBody method of the World class, to associate the world. We need one more thing: create the BodyDef. Because the Body has many properties such as position, speed, type ... but someone You should know this, and BodyDef is the class that tells how libgdx has the body. First, I'll create a BodyDef, as minijoeDef. To not fill code,

I better do a feature called createJoeBodyDef. It will complain that it does not exist, so it does not matter, we believe, that's what IDE are so fast and powerful. Let's create a BodyDef, and for this I have a number of parameters to fill. Not all, some have default values, but we have to give position. As can be (0, 10), so that it is above. I call the set method, because the Vector BodyDef is final,

so I can not replace it with another Vector2, but must use the set method. The second thing I will do is tell you what kind of object it is, because Box2D we have several types of Body. We DynamicBody, the StaticBody and KinematicBody. We'll see three, but for now, StaticBody is what we use to bodies that are not going to move, such as floors, enemies, things that can not be moved. For now we are interested DynamicBody, because you can move:

You can have speed, strength ... obviously have the character is going to move, so we'll use dynamic forces. Once we have done We do you a return. And with the definition of the body created, we created the real Minijoe, with that definition. By the way, BodyDef not capable disposearse, because it is a basic data structure. Disappears at the end of the show (method). But yes Body should disposearse. The only thing not dispose () method,

but you have to ask the world to destroy the Body. With that being disposea it is removed and all that, but you can not do with a call. The problem with this is that it gives way, because it lacks the Fixture. That is, if I try to run this continue without seeing anything, because it is to create the Fixture so that we can visualize the body.

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.