42 videos
238 minutes
We have already seen how to create a Minijoe, how to create a floor, and some will be thinking if we are to start collecting and Box2D with Scene2D. But no, because there is one thing you must teach, and that is sometimes not so easy to create Fixtures. I mean, so far we have created boxes, and it is easy to create. But how do we do if we want to create not boxy like a triangle? Let's see how to create a skewer so you can see it should not be so easy,
but it can be if we do it right. Let's create a PinchoBody and PinchoFixture. Then we have several skewers, so this variable is not We will make us useful unless metamos in a list, to save all the skewers. But for now we'll use just one. In fact we will do as we have done bodies so far. We create a body. We create the function. Initially we can pass like any other. We will give the skewer a size
1 meter x 1 meter. But as for the position, although as you step height 0.5 to center, wide I'll leave depending on a parameter called X to use it and create as many spikes as you need. Now what I do is define a function that will pass the Body, as createPinchoFixture, and from the fixture to use PinchoBody is returned. This is where the problems begin, because there is no TriangleShape. Ni serves PolygonShape
For this purpose, there is no setTriangle, or anything like that. We need to use vertices. And someone will say ... oh no! Quiet, nothing happens, is simple. I tell you. So far we have created a skewer, which we have given a position, knowing only the center. I want to give a triangle. As well. We basically have 1 meter high, 1 meter wide, knowing that this is placed at the point (x, 0.5). So this is 1,
this is 0.5, and this is 0. And while this is x + 0.5, this is x, and this is x-0.5. What should I tell Box2D to create this triangle? Well, I give you this vertex position, of this, and this. The order is important because the vertices are defined in counter-clockwise to clockwise. I will say that the fixture has an apex here, one here and one here. In Box2D, I do it from shape, which has a method called set, which receives an array of
Vector2 shaped vertices []. I create the array of vertices, and associate PolygonShape. I Vector2 vector, which has vertices. And I think an array of Size 3 because it has 3 vertices. Important notice: for strange reasons, Box2d a body can only have 8 vertices as much. If you want to increase the number Vertex you have to do odd jobs, so as much 8. To define the vertices, we first define the first vertex, which will be. We have to use
local center coordinates, so I say I want a vertex in the position (-0.5, -0.5) because is 0.5 to the left of center and 0.5 down the middle. The second point is the same, only with the x in +0.5, and the third point is on the same x, but with the Y at +0.5 m high. And that's it really. Now I call set (vertices). I think the Fixture from the shape, do the dispose, and return the fixture. And that's it. I think my pinchoFixture
from pinchoBody, and to compile should give you a position, for example 1. Finally, before I forget not again, I must ask pinchoBody destroy the fixture, and must destroy the world body. When I run, what will happen is that we will see the triangle there. In fact, to give you a joy I'll show you what happens if I put 0.5 here, so you can see how beautiful this physical. [Coughs] Sorry. See what grace.
This is what mola of physical, which are very realistic, is not only a bounding box is more realistic. The skewers where we will be interested, as far right. The aim is that the jump box. So this increasingly looks better. Now let's see how we can do the jumps and collisions.
About the Author
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.